简介

随着博客文章数越来越多,hexo三连的时间越来越长,导致等待时间过长,加上可能发生的网络波动造成推送失败,为此,亲身试验Github Action自动部署博客,效果不错,值得一试。本教程改动博客较少,步骤简单,请放心食用。另外感谢大佬安知鱼提供的Action思路,本博客已按照本教程实现自动化部署。

原理

利用Github Action构建仓库代码的特性,可以将仓库代码构建出制品,将制品推送到公开仓库或者服务器,达到自动化部署的效果。

大概讲解一下本方法的网络拓扑图

网络拓扑图

前提

  • 需要提前删除.deploy_gitthemes/butterfly/.git文件夹,因为多个.git文件夹不会被识别成项目文件,从而无法上传仓库。

  • 本次教程使用的环境为:centos 7.6,root用户

添加屏蔽项

编辑.gitignore

1
2
3
4
5
6
7
8
9
10
11
12
13
.DS_Store
Thumbs.db
db.json
*.log
node_modules/
public/
.deploy*/
_multiconfig.yml

.deploy_git
*.bat
package-lock.json

添加屏蔽项

只需保证上传仓库的文件夹有.githubscaffoldssourcethemes_config.butterfly.yml_config.yml.gitinorepackage.jsoncdn.py是用来刷新腾讯云cdn的代码。

新建腾讯云刷新url代码

新建cdn.py,添加以下代码:

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
import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cdn.v20180606 import cdn_client, models
try:
# 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
# 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
cred = credential.Credential("SecretId", "SecretKey")
# 实例化一个http选项,可选的,没有特殊需求可以跳过
httpProfile = HttpProfile()
httpProfile.endpoint = "cdn.tencentcloudapi.com"

# 实例化一个client选项,可选的,没有特殊需求可以跳过
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
# 实例化要请求产品的client对象,clientProfile是可选的
client = cdn_client.CdnClient(cred, "", clientProfile)

# 实例化一个请求对象,每个接口都会对应一个request对象
req = models.PurgePathCacheRequest()
params = {
"Paths": [ "https://blog.aqcoder.cn" ],
"FlushType": "delete"
}
req.from_json_string(json.dumps(params))

# 返回的resp是一个PurgePathCacheResponse的实例,与请求对象对应
resp = client.PurgePathCache(req)
# 输出json格式的字符串回包
print(resp.to_json_string())

except TencentCloudSDKException as err:
print(err)

其中Paths里面填写cdn加速域名,我这里已经添加了cdn域名,添加CDN域名教程参考我的博客教程:使用CDN加速你的博客吧

FlushType可填写flush(刷新变更资源)、delete(刷新全部资源)

secretIdsecretKey可到腾讯云添加子用户,使用子用户的密钥,保证安全性:

https://console.cloud.tencent.com/cam/user/userType

腾讯云创建用户并授权CDN访问

快速创建用户

填写用户名

选择编程访问

选择CDN全读写访问权限

新建Action

博客根目录新建一个.github文件夹,在.github文件夹新建workflows文件夹,新建一个master.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
name: 自动任务

on:
push:
branches: [master]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: 1. 检出分支
uses: actions/checkout@v3

- name: 2. 安装 Node
uses: actions/setup-node@v3
with:
node-version: 16

- name: 3. 安装 Hexo
run: |
export TZ='Asia/Shanghai'
npm install hexo-cli -g

- name: 4. 缓存 Hexo
uses: actions/cache@v1
id: cache
with:
path: node_modules
key: ${{runner.OS}}-${{hashFiles('package.json')}}

- name: 5. 安装依赖
if: steps.cache.outputs.cache-hit != 'true'
run: |
npm install --save

- name: 6. 生成静态文件
run: |
hexo clean
hexo generate
hexo algolia

- name: 7. 推送到github公共仓库
working-directory: ./public
run: |
git init
git checkout -b master
git add .
git -c user.name='flow2000' -c user.email='1982989137@qq.com' commit -m '提交博客源码'
git remote add origin https://${{secrets.HUB_ACCESS_TOKEN}}@github.com/flow2000/flow2000.github.io.git
git push origin master -f -q

- name: 8. 推送到服务器私有仓库 #没有使用服务器可删除该步骤
uses: easingthemes/ssh-deploy@v2.0.7
env:
SSH_PRIVATE_KEY: ${{ secrets.SERVER_ACCESS_TOKEN }} #服务器生成的私钥,例如 -----BEGIN RSA PRIVATE KEY-----xxxx-----END RSA PRIVATE KEY-----
ARGS: "-avz --delete" # rsync参数
SOURCE: "public/"
REMOTE_HOST: ${{ secrets.SERVER_HOST }} #服务器ip地址,例如 1.2.3.4
REMOTE_USER: ${{ secrets.SERVER_USER }} #登录用户,例如 root
TARGET: ${{ secrets.SERVER_TARGET }} #服务器目录,例如 /www/blog.aqcoder.cn
EXCLUDE: ".git/"

- name: 9. 安装 Python #没有使用腾讯云cdn可删除该步骤
uses: actions/setup-python@v1
with:
python-version: 3.8

- name: 10. 安装依赖
run: |
pip install --upgrade pip
pip install tencentcloud-sdk-python
pip install tencentcloud-sdk-python-cdn

- name: 11. 刷新腾讯云cdn #没有使用腾讯云cdn可删除该步骤
run: |
python cdn.py

第7步注意修改成自己的公开博客仓库地址

第8、9、10步可根据需求使用

第8步的ARGS参数可参考Linux rsync命令用法详解

新建私有仓库

github新建一个Private仓库。

新建私有仓库

生成token

前往github settings生成token,权限需要有repoworkflow

生成token

添加secrets

服务器上执行:

1
ssh-keygen -t rsa -b 2048

则按提示按完回车后

如果是root用户执行该命令,则:

/root/.ssh/id_rsa.pub的内容复制到/root/.ssh/authorized_keys

服务器私钥则在/root/.ssh/id_rsa,将/root/.ssh/id_rsa内容复制到仓库的secrets环境变量SERVER_ACCESS_TOKEN中即可

如果是其他用户执行该命令(例如git用户)则:

/home/git/.ssh/id_rsa.pub的内容复制到/home/git/.ssh/authorized_keys

服务器私钥则在/home/git/.ssh/id_rsa,将/home/git/.ssh/id_rsa内容复制到仓库的secrets环境变量SERVER_ACCESS_TOKEN中即可

添加secrets

创建腾讯云CDN加速网站

访问:https://console.cloud.tencent.com/cdn/domains

输入已备案的博客域名,选择加速类型为CDN网页小文件,源站配置选择自有源,回源协议选择http,源站地址选择服务器ip,第二个框加上端口

添加域名

推荐配置一直选择下一步即可,到最后一步HTTPS配置需要添加一下证书,证书需要到证书管理那里申请一个

推荐配置

提交配置

配置好HTTPS访问和HTTPS证书后,点击提交所有配置

到域名解析那里填加CNAME记录即可验证CDN加速域名

提交配置

将文章推送到私有仓库

博客根目录执行命令:

1
2
git init
git remote add origin git@github.com:flow2000/hexo-butterfly-flow2000.git

每次写完博客,发布网站可执行以下命令:

1
2
3
git add .
git commit -m "推送私有仓库"
git push origin master

嫌麻烦可制作bat脚本,在博客根目录下新建push.bat,复制以下内容即可:

1
2
3
4
5
@echo off
git add .
git commit -m "推送私有仓库"
git push origin master
pause

使用时双击脚本即可自动执行命令。

查看Action执行情况:

Action详情

静待片刻,即可看到文章已部署在博客。

参考文章

使用GithubActions自动部署应用到自己的服务器

使用 Github Action 自动部署