Github Action 自动部署博客:推送到 Github 公共仓库 + 服务器私有仓库 + 刷新腾讯云 CDN
| 字数总计:1.7k|阅读时长:6 分钟 | 阅读量:440
简介
随着博客文章数越来越多,hexo 三连的时间越来越长,导致等待时间过长,加上可能发生的网络波动造成推送失败,为此,亲身试验 Github Action 自动部署博客,效果不错,值得一试。本教程改动博客较少,步骤简单,请放心食用。另外感谢大佬安知鱼提供的 Action 思路,本博客已按照本教程实现自动化部署。
原理
利用 Github Action 构建仓库代码的特性,可以将仓库代码构建出制品,将制品推送到公开仓库或者服务器,达到自动化部署的效果。
大概讲解一下本方法的网络拓扑图

前提
添加屏蔽项
编辑.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
|

只需保证上传仓库的文件夹有.github、scaffolds、source、themes、_config.butterfly.yml、_config.yml、.gitinore、package.json。cdn.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: cred = credential.Credential("SecretId", "SecretKey") httpProfile = HttpProfile() httpProfile.endpoint = "cdn.tencentcloudapi.com"
clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile client = cdn_client.CdnClient(cred, "", clientProfile)
req = models.PurgePathCacheRequest() params = { "Paths": [ "https://blog.aqcoder.cn" ], "FlushType": "delete" } req.from_json_string(json.dumps(params))
resp = client.PurgePathCache(req) print(resp.to_json_string())
except TencentCloudSDKException as err: print(err)
|
其中 Paths 里面填写 cdn 加速域名,我这里已经添加了 cdn 域名,添加 CDN 域名教程参考我的博客教程:使用 CDN 加速你的博客吧
FlushType 可填写 flush(刷新变更资源)、delete(刷新全部资源)
secretId、secretKey 可到腾讯云添加子用户,使用子用户的密钥,保证安全性:
到 https://console.cloud.tencent.com/cam/user/userType
腾讯云创建用户并授权 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 }} ARGS: "-avz --delete" SOURCE: "public/" REMOTE_HOST: ${{ secrets.SERVER_HOST }} REMOTE_USER: ${{ secrets.SERVER_USER }} TARGET: ${{ secrets.SERVER_TARGET }} EXCLUDE: ".git/"
- name: 9. 安装 Python 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 run: | python cdn.py
|
第 7 步注意修改成自己的公开博客仓库地址
第 8、9、10 步可根据需求使用
第 8 步的 ARGS 参数可参考 Linux rsync 命令用法详解
新建私有仓库
到 github 新建一个 Private 仓库。

生成 token
前往 github settings 生成 token,权限需要有 repo、workflow:

添加 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 中即可

创建腾讯云 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 执行情况:

静待片刻,即可看到文章已部署在博客。
参考文章
使用 GithubActions 自动部署应用到自己的服务器
使用 Github Action 自动部署