Github Actions研究

在折腾blog中,使用github actions遇到的问题和解决方案。

问题

问题描述

我想根据下面这两个链接:

  • 让blog能够自动从sec-yp-blog仓库部署到catcheryp.github.io仓库
  • 在自动发布的时候保证上传blog生成的index.json文件到Algolia中,以便于在blog上集成Algolia的搜索功能

参考链接:

博客名自动发布的github 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
name: deploy

on:
    push:
    workflow_dispatch:
    schedule:
        # Runs everyday at 8:00 AM
        - cron: "0 0 * * *"

jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout
              uses: actions/checkout@v2
              with:
                  submodules: true
                  fetch-depth: 0

            - name: Setup Hugo
              uses: peaceiris/actions-hugo@v2
              with:
                  hugo-version: "latest"

            - name: Build Web
              run: hugo

            - name: Deploy Web
              uses: peaceiris/actions-gh-pages@v3
              with:
                  PERSONAL_TOKEN: ${{ secrets.PERSONAL_TOKEN }}
                  EXTERNAL_REPOSITORY: Catcheryp/Catcheryp.github.io
                  PUBLISH_BRANCH: master
                  PUBLISH_DIR: ./public
                  commit_message: ${{ github.event.head_commit.message }}

上传Algolia的github action为:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
name: Algolia Upload Records
on:
  [push] #推送时执行
jobs:
  algolia:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        # 获取代码 Checkout
        uses: actions/checkout@v2
      - name: Upload Records
        # 使用 Action
        uses: iChochy/Algolia-Upload-Records@1.1
        # 设置环境变量
        env:
          APPLICATION_ID: ${{secrets.APPLICATION_ID}}
          ADMIN_API_KEY: ${{secrets.ADMIN_API_KEY}}
          INDEX_NAME: blog
          FILE_PATH: index.json

其实只要将两个action合并即可,很简单的一个问题,但是由于变量设置错误,让我搞了很长时间。

问题原因

https://image.sec-yp.cn/20231115085456.png

Github Actions常用

actions/checkout@v2

actions/checkout@v2:意思是在docker容器中拉取当前仓库内容。

needs

下面使用了needs,表示algolia操作会在build之后

 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
name: deploy

on:
    push:

jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout
              uses: actions/checkout@v2
              with:
                  submodules: true
                  fetch-depth: 0

            - name: Setup Hugo
              uses: peaceiris/actions-hugo@v2
              with:
                  hugo-version: "latest"

            - name: Build Web
              run: hugo

    algolia:
        needs: build
        runs-on: ubuntu-latest
        steps:
          - name: List files in public directory
            run: ls public/index.json

在工作流作业间传递数据

 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
name: deploy

on:
    push:
   
jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout
              uses: actions/checkout@v2
              with:
                  submodules: true
                  fetch-depth: 0

            - name: Setup Hugo
              uses: peaceiris/actions-hugo@v2
              with:
                  hugo-version: "latest"

            - name: Build Web
              run: hugo
              
            - name: Upload json
              uses: actions/upload-artifact@v3
              with:
                name: homework
                path: index.json

    job_2:
      name: read json
      needs: build
      runs-on: ubuntu-latest
      steps:
          - name: Download result for build
            uses: actions/download-artifact@v3
            with:
              name: homework
          
          - name: Read
            run: |
              cat index.json              

这里很奇怪,第一次运行的时候没有任何问题,后面就一直运行失败。

参考:

将工作流程数据存储为构件 - GitHub 文档

两种解决方案

方案一:目前使用的

 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
name: deploy

on:
    push:
    workflow_dispatch:
    schedule:
        # Runs everyday at 8:00 AM
        - cron: "0 0 * * *"

jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout
              uses: actions/checkout@v2
              with:
                  submodules: true
                  fetch-depth: 0

            - name: Setup Hugo
              uses: peaceiris/actions-hugo@v2
              with:
                  hugo-version: "latest"

            - name: Build Web
              run: hugo

            - name: Deploy Web
              uses: peaceiris/actions-gh-pages@v3
              with:
                  PERSONAL_TOKEN: ${{ secrets.PERSONAL_TOKEN }}
                  EXTERNAL_REPOSITORY: Catcheryp/Catcheryp.github.io
                  PUBLISH_BRANCH: master
                  PUBLISH_DIR: ./public
                  commit_message: ${{ github.event.head_commit.message }}

            - name: Upload Records
              # 使用 Action
              uses: iChochy/Algolia-Upload-Records@1.1
              # 设置环境变量
              env:
                APPLICATION_ID: ${{secrets.APPLICATION_ID}}
                ADMIN_API_KEY: ${{secrets.ADMIN_API_KEY}}
                INDEX_NAME: blog
                FILE_PATH: ./public/index.json

方案二:

 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
name: deploy

on:
    push:
    workflow_dispatch:
    schedule:
        # Runs everyday at 8:00 AM
        - cron: "0 0 * * *"

jobs:
    build:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout
              uses: actions/checkout@v2
              with:
                  submodules: true
                  fetch-depth: 0

            - name: Setup Hugo
              uses: peaceiris/actions-hugo@v2
              with:
                  hugo-version: "latest"

            - name: Build Web
              run: hugo

            - name: Deploy Web
              uses: peaceiris/actions-gh-pages@v3
              with:
                  PERSONAL_TOKEN: ${{ secrets.PERSONAL_TOKEN }}
                  EXTERNAL_REPOSITORY: Catcheryp/Catcheryp.github.io
                  PUBLISH_BRANCH: master
                  PUBLISH_DIR: ./public
                  commit_message: ${{ github.event.head_commit.message }}

            - name: upload
              uses: wangchucheng/algolia-uploader@master
              with:
                # Such as `Z0U0ACGBN8`
                app_id: ${{ secrets.APPLICATION_ID }}
                # You can store token in your project's 'Setting > Secrets' and reference the name here. Such as ${{ secrets.ALGOLIA_ADMIN_KEY }}
                admin_key: ${{ secrets.ADMIN_API_KEY }}
                # The index name. 
                index_name: blog
                # The index file path relative to repo root.
                index_file_path: public/index.json

参考

玩转 GitHub Actions | HeyFE

将工作流程数据存储为构件 - GitHub 文档

GitHub Actions 入门教程 - 阮一峰的网络日志