一个计算机技术爱好者与学习者

0%

好好学Hexo:Hexo配置GitHub Actions自动构建发布

1. Travis CI必须付费了

2023年2月25日,使用travis ci部署hexo项目,发现并没有触发任务。

登录travis ci,发现提示:

Builds have been temporarily disabled for public repositories due to a negative credit balance. Please go to the Plan page to replenish your credit balance or alter your Consume paid credits for OSS setting.

travis ci plan页面关闭了Consume paid credits for OSS,但是依然提示:

Builds have been temporarily disabled for private and public repositories due to a negative credit balance. Please go to the Plan page to replenish your credit balance.

点击Change plan,发现已经没有免费的plan了,而最便宜的plan,也要$69/monthly,太贵了,放弃。
不过可以理解,毕竟跑CI是需要服务器的,服务器是需要花钱的。

那就换成GitHub Actions吧,有付费版,也有免费版。

2. Quickstart for GitHub Actions

参考文档:Quickstart for GitHub Actions

1、 创建新的分支(因为原分支是适用于travis ci的)

1
2
git checkout -b github-action
git push origin HEAD:github-action

2、 创建 github-actions-demo.yml 文件
voidking/hexo-deploy项目中执行

1
2
mkdir -p .github/workflows
vim .github/workflows/github-actions-demo.yml

github-actions-demo.yml 文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
name: GitHub Actions Demo
run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
on: [push]
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
- name: Check out repository code
uses: actions/checkout@v3
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ github.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."

3、 上传代码

1
2
3
git add .
git commit -m "github action test"
git push

报错:refusing to allow a Personal Access Token to create or update workflow .github/workflows/github-actions-demo.yml without workflow scope

这是因为我们的Access Token需要具有create or update workflow的权限,因此需要重新创建一个Access Token。
具体操作方法:访问Personal access tokens (classic)页面,Generate new token,生成token时一定要勾选repo和workflow。

然后,使用新的token再次push即可。修改token的方法参考文档《Git实用命令》

4、查看Actions
访问voidking/hexo-deploy Actions页面,即可看到CI workflows(相当于gitlab中的pipelines)。

3. Hexo配置GitHub Actions

参考文档:

3.1. CICD思路

  1. 拉取负责部署的hexo-deploy repo,里面是hexo的配置文件(关于站点配置和构建配置等,详情参考《Hexo配置Travis CI自动构建发布》
  2. 拉取hexo theme repo,里面是站点主题
  3. 拉取hexo-backup repo,里面是markdown文档
  4. hexo-backup中的文档放到hexo可以构建(编译)的位置
  5. 安装nodejs
  6. 安装hexo、gulp等依赖
  7. 执行构建(编译)
  8. 上传编译后的html等静态文件到两个pages repo
    a. github pages是国外流量的源站
    b. aliyun server是国内流量的源站
    c. aliyun server从gitee pages拉取最新版本
    d. gitee pages本身不对外提供服务,只是作为一个git仓库
  9. aliyun server从gitee pages拉取最新版本

3.2. 准备github-actions.yml

github-actions.yml内容如下:

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
name: Hexo CICD
run-name: ${{ github.actor }} build and deploy hexo!
on:
push:
branches:
- master
- github-actions

env:
GIT_USER: voidking
HEXO_BACKUP_REPO: voidking/hexo-backup
HEXO_BACKUP_REPO_BRANCH: master
HEXO_THEME_REPO: voidking/hexo-theme-next
HEXO_THEME_REPO_BRANCH: root
GITHUB_PAGES_REPO: github.com/voidking/voidking.github.io.git
GITEE_PAGES_REPO: gitee.com/voidking/voidking.git
GITHUB_PAGES_URL: "https://${GIT_USER}:${{ secrets.GH_TOKEN }}@${GITHUB_PAGES_REPO}"
GITEE_PAGES_URL: "https://${GIT_USER}:${{ secrets.GITEE_TOKEN }}@${GITEE_PAGES_REPO}"
ALI_IP: 8.136.13.58
ALI_USER: voidking

jobs:
build:
name: Build on node ${{ matrix.node_version }} and ${{ matrix.os }}
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest]
node_version: [12.22.5]
steps:
- name: Checkout repo
uses: actions/checkout@v2
- name: Checkout theme repo
uses: actions/checkout@v2
with:
repository: ${{ env.HEXO_THEME_REPO }}
ref: ${{ env.HEXO_THEME_REPO_BRANCH }}
path: themes/next
- name: Checkout hexo-backup repo
uses: actions/checkout@v2
with:
repository: ${{ env.HEXO_BACKUP_REPO }}
ref: ${{ env.HEXO_BACKUP_REPO_BRANCH }}
path: hexo-backup
token: ${{ secrets.GH_TOKEN }}
- name: Move markdown articles to current directory
run: mv hexo-backup/source . && rm -rf source/private
- name: Install nodejs ${{ matrix.node_version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node_version }}
- name: Install nodejs dependencies
run: npm install
- name: Build nodejs project
run: npm run build
- name: Push to voidking.github.io and gitee
run: |
git config --global user.name "voidking"
git config --global user.email "voidking@qq.com"
git clone https://${{ secrets.GH_TOKEN }}@${{ env.GITHUB_PAGES_REPO }} voidking
cd voidking
rm -rfv `ls -a | grep -vw '\.' | grep -vw '\.git' | xargs`
ls -al
# unalias cp
cp -rf ../public/. .
cp ../source/.travis.yml .
git add . && git commit -m "GitHub Actions Auto Builder"
git push --force --quiet ${{ env.GITHUB_PAGES_URL }} master:master
git push --force --quiet ${{ env.GITEE_PAGES_URL }} master:master

deploy:
name: Deploy to Aliyun Server
needs: build
runs-on: ubuntu-latest
steps:
- name: Configure id_rsa
run: |
mkdir -p ~/.ssh/
echo ${{ secrets.ID_RSA }} | base64 -d > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
- name: Execute ssh command
run: |
ssh -o StrictHostKeyChecking=no \
-o PubkeyAcceptedKeyTypes=ssh-rsa \
${{ env.ALI_USER }}@${{ env.ALI_IP }} \
"cd /opt/nginx/work/voidking/ && git pull --force --quiet ${{ env.GITEE_PAGES_URL }} master:master"

说明:

  • github action中定义变量有三种方式:vars、secrets和env。
  • vars/secrets在workflow触发前定义,env在workflow触发后定义。
  • ${{}}双花括号表示上下文引用,详情参考Context availability
  • ${}单花括号表示shell方式使用中env变量。
  • 使用env中的变量有两种方式:上下文引用和shell方式使用。

3.3. 配置加密变量

上面的workflow配置中,用到了一些敏感变量,GH_TOKEN、GITEE_TOKEN和ID_RSA,这些变量需要加密。

配置加密变量的方法:

  • repository级别变量:Project -> Settings -> Actions secrets and variables -> New repository secret
  • environments级别变量:Project -> Settings -> Actions secrets and variables -> Manage enviroments -> New enviroment -> Environment secrets -> Add secret

其中ID_RSA的获取方法为:

1
2
3
4
ssh-keygen
ALI_IP="8.136.13.58"
ssh-copy-id -i ~/.ssh/id_rsa.pub voidking@${ALI_IP}
cat .ssh/id_rsa | base64 | tr -d '\n'

3.4. CICD测试

1
2
3
git add .
git commit -m "github action test"
git push

访问voidking/hexo-deploy Actions页面,即可看到最新的workflow。
最开始难免出错,根据报错进行调整即可。

最终,实现了和travis ci上一样的功能,nice。

4. 踩坑记录

4.1. env不能用在job:if

当使用job:if时,如下定义:

1
2
3
4
5
6
7
env:
BUILD: "false"
jobs:
build:
name: Build
if: ${{ env.BUILD == "true" }}
runs-on: ubuntu-latest

报错: Unrecognized named-value: ‘env’. Located at position 1 within expression: env.BUILD == “true” .github/workflows/github-actions.yml

这是个github action的坑,参考文档:

4.2. workflow_dispatch无效

当使用job:if时,如下定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
on:
workflow_dispatch:
inputs:
build:
description: "build project"
required: true
default: true
type: boolean
jobs:
build:
name: Build
if: ${{ inputs.build }}
runs-on: ubuntu-latest

无法触发build job。