一、前置知识
CI\CD 即:持续集成(Continuous Integration)、持续交付(Continuous Delivery)、持续部署(Continuous Deployment)。GitHub Actions 是一个持续集成和持续交付 (CI/CD) 平台,可用于自动执行构建、测试和部署管道,换句话说就是通过 Actions 帮助我们去执行 hexo s & hexo g & hexo d 的操作并推到 xxx.github.io 仓库中
二、参考教程
以下将使用特定的常量名来指代一些名词。此处建议读者直接使用教程内容的常量名。在最后再逐一搜索替换。这样可以避免对各种常量名的混淆。
| 常量 | 解释 |
|---|
| [Blogroot] | 本地存放博客源码的文件夹路径 |
| [SourceRepo] | 存放博客源码的私有仓库名 |
| [SiteBlogRepo] | 存放编译好的博客页面的公有仓库名 |
| [Github Actions 执行自动化部署] | 执行 hexo clean & hexo g & hexo d |
三、解决方案
申请令牌
Settings->Developer->Personal access tokens->Generate new token

生成的令牌只会显示一次,记住令牌,如果忘记保存就删了重弄

新建仓库并上传
新建一个 Github 私有仓库
在博客根路径下,找到主题文件 [Blogroot]\themes\butterfly,将 .git 移出来
在博客根路径下的 .gitignore 加入忽略规则
1 2 3 4 5 6 7 8 9 10 11 12
| .DS_Store Thumbs.db db.json *.log node_modules/ public/ .deploy*/ .vscode/ /.idea/ .deploy_git*/ .idea themes/butterfly/.git
|
在博客根路径下执行
1 2 3 4 5 6
| git init git add . git commit -m "init" git branch -M main git remote add origin https://github.com/用户名/私有仓库名.git git push -u origin main
|
之后就可以在仓库中看到博客源码
配置刷新CDN
刷新 CDN 通过 Action 一并实现,没有 CDN 此项跳过
如果小白没跑通整个 Action 而放弃使用 Action 部署(滴滴小张也可以滴),类似插件推荐:腾讯云CDN主动刷新插件 张洪 Heo
为了保证安全性,使用子用户的 secretId、secretKey,打开用户 - 控制台新建用户

依次编辑用户名、访问方式、用户权限


在博客根目录下创建 cdn.py,填写 cred、params 变量
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://www.777nx.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)
|
配置GitHub Action
新建 [Blogroot]\.github\workflows\autodeploy.yml
- 第六步
hexo algolia 为 algolia 搜索,gulp 为文件压缩任务,按需删除(教程记录在博客搭建博文)
- 第七步 clean-exclude 变量为 Hexo-SEO-AutoPush SEO 自动提交插件的配置,按需删除(教程记录在博客搭建博文,原则上可以一起写到此工作流里通过 curl 请求提交,但是这个插件是真的好用!)
- 第九步刷新腾讯云 CDN 资源,按需删除
- 第十步备份源码至 Gitee,按需删除
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| name: Deploy
on: push: branches: - main
workflow_dispatch:
jobs: build: runs-on: ubuntu-latest
steps: - name: 1. 检出仓库 uses: actions/checkout@v2 with: ref: main
- name: 2. 安装 Node.js uses: actions/setup-node@v3 with: node-version: "16.x"
- name: 3. 安装 Hexo run: | npm install hexo-cli -g
- name: 4. 缓存 Node 插件 uses: actions/cache@v1 id: cache-modules with: path: node_modules key: ${{runner.OS}}-${{hashFiles('**/package-lock.json')}}
- name: 5. 安装依赖 if: steps.cache-modules.outputs.cache-hit != 'true' run: | npm ci
- name: 6. 生成静态文件 run: | hexo clean hexo generate hexo algolia gulp export TZ='Asia/Shanghai'
- name: 7. 部署到 github page uses: JamesIves/github-pages-deploy-action@v4 with: token: ${{ secrets.GITHUBTOKEN }} repository-name: ${{ secrets.GITHUBUSERNAME }}/${{ secrets.GITHUBUSERNAME }}.github.io branch: main folder: public clean-exclude: | public/.github/ commit-message: "${{ github.event.head_commit.message }} Updated By Github Actions"
- name: 8. 推送到服务器私有仓库 uses: easingthemes/ssh-deploy@main env: SSH_PRIVATE_KEY: ${{ secrets.SERVER_PRIVATE_KEY }} ARGS: "-avz --delete" SOURCE: "public/" REMOTE_HOST: ${{ secrets.SERVER_HOST }} REMOTE_USER: ${{ secrets.SERVER_USER }} TARGET: ${{ secrets.SERVER_PATH }} EXCLUDE: ".git/,.user.ini"
- name: 9. 刷新CDN run: | pip install --upgrade pip pip install tencentcloud-sdk-python pip install tencentcloud-sdk-python-cdn python3 cdn.py
- name: 10. 备份Gitee uses: wearerequired/git-mirror-action@master env: SSH_PRIVATE_KEY: ${{ secrets.GITEE_RSA_PRIVATE_KEY }} with: source-repo: git@github.com:GC-ZF/Blog-Backups.git destination-repo: git@gitee.com:gc-zhang/blog-backups.git
|
配置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 中即可

在博客根路径执行
1 2 3
| git add . git commit -m "add actions" git push
|
嫌麻烦可制作 bat 脚本,在博客根目录下新建 push.bat,复制以下内容即可:
1 2 3 4 5
| @echo off git add . git commit -m "推送私有仓库" git push origin master pause
|
使用时双击脚本即可自动执行命令。
查看 Action 执行情况:

静待片刻,即可看到文章已部署在博客。
四、总结
彩蛋?这样自动化部署的目的还有一个优点,你可以直接在 [SourceRepo] 即 Github 博客源码仓库中直接修改文章提交后自动触发 Github Ations 执行自动化部署到 [GithubBlogRepo]、GiteeBlogRepo、[服务器站点],有点 typecho 那感觉了吧嘿嘿,之后本地同步通过在博客路径下 git pull 同步仓库源码,是不是很方便!