利用 GitHub Actions 自动部署 Hexo 博客

前言

本文主要记录如何利用 Github Actions 自动部署 Hexo 博客,并且部署完成后将生成的静态页面上传到 Github Pages 和 Coding 仓库。

GitHub Actions 是什么?

大家知道,持续集成由很多操作组成,比如抓取代码、运行测试、登录远程服务器,发布到第三方服务等等。GitHub 把这些操作就称为 actions。

很多操作在不同项目里面是类似的,完全可以共享。GitHub 注意到了这一点,想出了一个很妙的点子,允许开发者把每个操作写成独立的脚本文件,存放到代码仓库,使得其他开发者可以引用。

如果你需要某个 action,不必自己写复杂的脚本,直接引用他人写好的 action 即可,整个持续集成过程,就变成了一个 actions 的组合。这就是 GitHub Actions 最特别的地方。

GitHub 做了一个官方市场,可以搜索到他人提交的 actions。另外,还有一个 awesome actions 的仓库,也可以找到不少 action。

上面说了,每个 action 就是一个独立脚本,因此可以做成代码仓库,使用userName/repoName的语法引用 action。比如,actions/setup-node就表示github.com/actions/setup-node这个仓库,它代表一个 action,作用是安装 Node.js。事实上,GitHub 官方的 actions 都放在 github.com/actions 里面。

既然 actions 是代码仓库,当然就有版本的概念,用户可以引用某个具体版本的 action。下面都是合法的 action 引用,用的就是 Git 的指针概念,详见官方文档

1
2
3
4

actions/setup-node@74bc508 # 指向一个 commit
actions/setup-node@v1.0 # 指向一个标签
actions/setup-node@master # 指向一个分支

安装 Hexo

请参考文章Hexo 搭建个人博客

创建 Github 仓库用于存放 Hexo 源文件

点击 New 创建新仓库

仓库名称任取,设置成私有仓库,然后点击创建

创建完成

将本地文件上传到远程仓库

在 Hexo 博客目录下使用 git 命令:git init . 初始化一个本地仓库

使用 git 命令 : git remote add origin [仓库SSH地址] 关联远程仓库

使用下面 git 命令将本地项目上传到 github 远程仓库

1
2
3
git add .
git commot -m "Update"
git push -u origin master

构建自动部署

创建 .yml 文件

在 Hexo 目录下的 .github 目录下创建一个 workflows 目录

在 workflows 目录创建一个 deploy_blog.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
name: Blog CI/CD

# 触发条件:在 push 到 master 分支后触发
on:
push:
branches:
- master

env:
TZ: Asia/Shanghai

jobs:
blog-cicd:
name: Hexo blog build & deploy
runs-on: ubuntu-latest # 使用最新的 Ubuntu 系统作为编译部署的环境

steps:
- name: Checkout codes
uses: actions/checkout@v2

- name: Setup node
# 设置 node.js 环境
uses: actions/setup-node@v1
with:
node-version: '12.x'

- name: Cache node modules
# 设置包缓存目录,避免每次下载
uses: actions/cache@v1
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}

- name: Install hexo dependencies
# 下载 hexo-cli 脚手架及相关安装包
run: |
npm install -g hexo-cli
npm install

- name: Generate files
# 编译 markdown 文件
run: |
hexo clean
hexo generate

- name: Deploy hexo blog
env:
# 设置时区
TZ: Asia/Shanghai
# 在 https://mingwolf.coding.net/user/account/setting/tokens 新建秘钥
coding_token: ${{ secrets.coding_token }}
# 团队中的某个人的用户名,一般默认是本人手机号码
coding_username: ${{ secrets.coding_USERNAME }}
# 格式为:e.coding.net/组织名/项目名/仓库名.git
coding_ref: ${{ secrets.coding_REF }}
# Github 仓库
GITHUB_REPO: github.com/oscar-yyc/oscar-yyc.github.io
# 将编译后的博客文件推送到指定仓库
run: |
cd ./public && git init && git add .
git config user.name "oscar-yyc"
git config user.email "iloveyichen@aliyun.com"
git add .
git commit -m "GitHub Actions Auto Builder at $(date +'%Y-%m-%d %H:%M:%S')"
git push --force --quiet "https://${{ secrets.access_tokens }}@$GITHUB_REPO" master:master
git push --force --quiet "https://${coding_username}:${coding_token}@${coding_ref}" master:master

TCB 上传

1
2
3
4
5
6
7
8
- name: Deploy static to Tencent CloudBase
id: deployStatic
uses: TencentCloudBase/cloudbase-action@v1.1.1
with:
secretId: ${{ secrets.SECRET_ID }}
secretKey: ${{ secrets.SECRET_KEY }}
envId: ${{ secrets.ENV_ID }}
staticSrcPath: public

将 Hexo 源文件备份到 Coding 仓库 backup 分支

backup_to_coding.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
name: Auto backup to coding

# 在push主分支时触发构建
on:
push:
branches:
- 'master'

jobs:
# job的名字:推送到coding
deploy:
name: Deploy to Coding
# job的运行平台
runs-on: ubuntu-18.04
# test任务的步骤
steps:
# 使用别人写好的指定版本的actions脚本,名称是checkout,下载本仓库
- uses: actions/checkout@v2
- name: 设置提交者的个人信息
# 这三个变量的值都放在 https://github.com/oscar-yyc/HexoSource/settings/secrets
env:
# 设置时区
TZ: Asia/Shanghai
# 在 https://mingwolf.coding.net/user/account/setting/tokens 新建秘钥
coding_token: ${{ secrets.coding_token }}
# 团队中的某个人的用户名,一般默认是本人手机号码
coding_username: ${{ secrets.coding_USERNAME }}
# 格式为:e.coding.net/组织名/项目名/仓库名.git
coding_ref: ${{ secrets.coding_REF }}
run: |
export message=$(git log --pretty=format:"%s" -1)
[ -f CNAME ] && rm CNAME || echo "CNAME doesn't exist"
rm -rf .github
rm -rf .git
git clone https://${coding_username}:${coding_token}@${coding_ref} coding_dir
cd coding_dir && mv .git ../ && cd ../ && rm -rf coding_dir
git config --local user.email "iloveyichen"
git config --local user.name "oscar-yyc"
git config core.filemode false
git remote set-url origin https://${coding_ref}
git add .
git commit -m "$message"
git push --force --quiet "https://${coding_username}:${coding_token}@${coding_ref}" master:backup

设置仓库 Actions secrets

点击这里 选择 Personal access tokens 进行创建

按照下图设置好,点击 Generate token

点击复制

找到刚刚创建好的仓库 点击 Settings –> Secrets –> New repository secret 创建一个 Secrets

先将刚刚复制的 token 粘贴到 Value 里面,然后 Name 处填写 access_tokens ,最后点击添加

接下来还要创建 3 个 Secret

名称如下:

1
2
3
4
5
coding_token # 在 https://mingwolf.coding.net/user/account/setting/tokens 新建秘钥

coding_USERNAME # 团队中的某个人的用户名,一般默认是本人手机号码

coding_REF # 格式为:e.coding.net/组织名/项目名/仓库名.git

开始自动部署

由于每次自动部署会删除掉 Github Pages 里的 CNAME 文件,所以自动部署前请将 CNAME 文件复制到 Hexo 博客目录下的 Source 文件夹

在 Hexo 博客目录使用以下 git 命令

1
2
3
git add .
git commit -m "Update"
git push

参考

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

git学习-如何将本地项目上传(同步)到github远程仓库

使用 GitHub Actions 自动部署 Hexo 博客到 GitHub Pages

为Github仓库添加Github Actions实现持续集成: Android apk自动编译发布以及github pages同步推送coding.net


利用 GitHub Actions 自动部署 Hexo 博客
https://blog.wainic.com/articles/hexo-auto-deploy/
作者
Wainic
发布于
2021年10月28日
许可协议