Github Action实现自动化部署

前言

Github Action是个非常好用的东西。之前不怎么写项目,也没怎么了解过,最近刚好有协同工作的需求,然后顺便看了下Github Action

Blog自动化部署

我的博客是用的hugo,部署的时候需要生成静态文件,然后上传到服务器上。因为每次都要自己手动去用sftp把文件传到服务器上面,所以这个过程稍微有那么一点繁琐。之前的方案是把这个过程写成了一个脚本,然后每次写完东西就执行这个脚本。

但是最近发现把东西放在Github私有仓库然后用Github Action自动部署会更方便,而且还能实现版本控制。

要实现这个操作,首先得在仓库的.github/workflows文件夹下面建一个hugo.yml,然后参考actions-hugo这个仓库,直接用这个Action

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
name: GitHub Pages

on:
  push:
    branches:
      - main  # Set a branch to deploy
  pull_request:

jobs:
  deploy:
    runs-on: ubuntu-22.04
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true  # Fetch Hugo themes (true OR recursive)
          fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: '0.119.0'
          # extended: true

      - name: Build
        run: hugo --minify

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        if: github.ref == 'refs/heads/main'
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./public

这个Action的触发条件是push操作。也就是main分支收到push事件的时候就会执行这个Action。然后这个Actiondeploy这个操作分成了4个步骤:

  1. 拉取代码
  2. 准备Hugo环境
  3. 生成静态文件
  4. 把生成的静态文件发布到Github Pages

由于我的博客是放在服务器上的,之后还需要将生成的静态文件拷贝到服务器上。我比较常用的方式是用SFTP,所以使用github-action-sftp把生成的静态文件上传到服务器上面:

完整的yaml文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
name: GitHub Pages

on:
  push:
    branches:
      - main  # Set a branch to deploy
  pull_request:

jobs:
  deploy:
    runs-on: ubuntu-latest
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true  # Fetch Hugo themes (true OR recursive)

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: 'latest'
          extended: true

      - name: Build
        run: |
          hugo --minify          

      - name: Transfer html files
        uses: Dylan700/sftp-upload-action@latest
        with:
          # 私有服务器公网IP地址
          server: ${{ secrets.HOST }}
          # 私有服务器用户名
          username: ${{ secrets.USERNAME }}
          port: 22
          # 私有服务器私钥
          key: ${{ secrets.PRIVATEKEY }}
          uploads: |
            ./public/ => ${{ secrets.REMOTEPATH }}            
          delete: 'false'

然后在仓库设置中的Secrets and variables添加对应的变量就能实现博客自动发布了

Springboot项目自动打包

也是类似的操作,我是直接拿Github官方给的例子改了改。多了一步用ssh-action重启服务:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
name: Java CI with Maven

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
      - name: Set up JDK 20
        uses: actions/setup-java@v3
        with:
          java-version: '20'
          distribution: 'temurin'
          cache: maven
      - name: Build with Maven
        run: mvn -B package --file pom.xml -DskipTests

      # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
      - name: Update dependency graph
        uses: advanced-security/maven-dependency-submission-action@fcd7eab6b6d22946badc98d1e62665cdee93e0ae

      - name: Transfer html files
        uses: Dylan700/sftp-upload-action@latest
        with:
          server: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          port: 22
          key: ${{ secrets.PRIVATEKEY }}
          uploads: |
            ./target/app.jar => ${{ secrets.REMOTEPATH }}            
          delete: 'true'

      - name: restart server throuth ssh
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          key: ${{ secrets.PRIVATEKEY }}
          script: |
            cd ${{ secrets.DEPLOYPATH }}
            ./restart.sh            
updatedupdated2023-11-302023-11-30