GitHub Actions 由 GitHub 官方推出的CI/CD工具,同类型的工具有Travis CI和Netlify,前两个玩过自动部署的应该都或多或少的用过,Netlify或许用的人更多一点,支持私有仓库、自带静态服务器、自带CDN。不过既然GitHub也推出了自己的工具,那也是要折腾折腾的,毕竟是自己家的东西,可玩性应该要高于其他两个。
生成ssh-key
因为自动部署是从A仓库触发action将内容部署到B仓库,所以需要一个部署的秘钥,以避免无法访问仓库的问题。目前我是GitHub Pro Plan的用户,所以我的源码仓库和GitHub Pages仓库都是私有的,那就更得需要这个东西了。
生成ssh-key
1
| ssh-keygen -t rsa -f ~/.ssh/github-actions-deploy -C "github username"
|
按两下回车即可生成。其中-C参数是可选的,没有太多的ssh-key可以不加,rsa是加密方式,也可以改成其他的加密方式。
执行完成后会生成如下两个文件:
1 2
| ~/.ssh/github-actions-deploy ~/.ssh/github-actions-deploy.pub
|
将公钥添加到GitHub Pages Repo
在GitHub Pages Repo中,打开Settings -> Deploy keys -> Add deploy key,将~/.ssh/github-actions-deploy.pub的内容添加进去即可。
将私钥添加到源码仓库
在你托管博客内容的仓库中,假设是hexo-blog,打开Settings -> Secrets -> New secret,这里需要注意Name的值,这个将作为GitHub Action中的变量使用,推荐大写,驼峰或者下划线。我的名称是GH_ACTION_DEPLOY_KEY,另外,为了使用algolia,我还在该仓库新增了HEXO_ALGOLIA_INDEXING_KEY。
添加GitHub Action配置文件
在站点根目录新增文件.github/workflows/deployci.yml配置文件。
1 2
| mkdir -p .github/workflows touch .github/workflows/deployci.yml
|
以下内容是本站的action
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
| name: Deploy Blog
on: push: branches: - master
jobs: build: runs-on: ubuntu-latest steps: - name: Checkout source uses: actions/checkout@v2 with: path: master - name: Setup node uses: actions/setup-node@v1 with: node-version: 12.x - name: prepare build env env: GH_ACTION_DEPLOY_KEY: ${{ secrets.GH_ACTION_DEPLOY_KEY }} run: | mkdir -p ~/.ssh/ echo "$GH_ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa ssh-keyscan github.com >> ~/.ssh/known_hosts git config --global user.name 'github username' git config --global user.email 'username@users.noreply.github.com'
- name: Install hexo dependencies run: | npm i -g hexo-cli cd master npm i - name: Deploy Blog env: TZ: Asia/Shanghai HEXO_ALGOLIA_INDEXING_KEY: ${{ secrets.HEXO_ALGOLIA_INDEXING_KEY }} run: | cd master hexo generate && hexo algolia && hexo deploy
|
编写完成后将本次新增的内容提交到GitHub上去,即可完成新增文章,提交到GitHub,完成自动部署,再也不用在本地执行一堆命令,等待半天在执行hexo d去部署了。
2026年1月12日修改
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
| name: Deploy Blog
on: push: branches: - master
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: submodules: recursive - name: Use Node.js 20 uses: actions/setup-node@v4 with: node-version: "20" cache: 'npm'
- name: Install Dependencies run: npm install
- name: Build run: npm run build
- name: Upload artifact uses: actions/upload-artifact@v4 with: name: blog-public path: ./public
deploy: needs: build runs-on: ubuntu-latest steps: - name: Download artifact uses: actions/download-artifact@v4 with: name: blog-public path: ./public
- name: Deploy to Remote Repo uses: peaceiris/actions-gh-pages@v4 with: deploy_key: ${{ secrets.REMOTE_DEPLOY_KEY }} external_repository: yourname/yourname.github.io publish_branch: master publish_dir: ./public user_name: 'xx' user_email: 'xxx@users.noreply.github.com' force_orphan: true
- name: Deploy to Aliyun OSS uses: manyuanrong/setup-ossutil@v2.0 with: endpoint: "oss-cn-beijing.aliyuncs.com" access-key-id: ${{ secrets.ALIYUN_KEY }} access-key-secret: ${{ secrets.ALIYUN_KEY_SECRET }} - name: Sync File to OSS run: ossutil cp -rf ./public/ oss://改成你自己的桶/ --force --update
|
GitAction解读
直接丢一个配置好的出来,并不能完全懂里面的意思,虽然搞技术的基本上能看个八九不离十,但是对于很多新手来说,还是差点意思,所以就分段解释下这个配置文件。
很简单,就是定义GitHub Action的名字。
1 2 3 4
| on: push: branches: - master
|
监听master分支的push时间,一旦在该分支发生push即可触发GitHub Action的执行。
1 2 3
| jobs: build: runs-on: ubuntu-latest
|
定义Job,构建信息,runs-on: ubuntu-latest表示GitHub Action将会以ubuntu系统来运行。
steps代表执行步骤,按照定义的先后顺序来执行一系列的工作流。
1 2 3 4
| - name: Checkout source uses: actions/checkout@v2 with: path: master
|
拉取站点源码的步骤,如果不指定分支则从默认分支拉取,path表示将源码pull下来后的位置,不设置就是当前目录,目前我是将其放在master目录,所以后面执行hexo命令,安装依赖等都需要执行cd master。不然会出现错误。
1 2 3 4
| - name: Setup node uses: actions/setup-node@v1 with: node-version: 12.x
|
配置node运行环境,版本指定为12.x,也可以是14.x,只要没兼容问题就行。
1 2 3 4 5 6 7 8 9 10
| - name: prepare build env env: GH_ACTION_DEPLOY_KEY: ${{ secrets.GH_ACTION_DEPLOY_KEY }} run: | mkdir -p ~/.ssh/ echo "$GH_ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa ssh-keyscan github.com >> ~/.ssh/known_hosts git config --global user.name 'github username' git config --global user.email 'username@users.noreply.github.com'
|
准备构建环境,这里主要是为了配置ssh-key,有些人用的不是hexo d来部署的,而是直接通过ftp,特定的客户端等直接将其上传到指定的位置:又拍云、OSS等地方。那就不需要这一步。username@users.noreply.github.com是为了避免邮箱泄露GitHub推出的private mail,老用户都是username@但是2017年7月18日年以后的都是uid+username@
不过我账号14年就创建了,可惜我还是uid+username@的形式,可能是我更换了几次用户名的问题吧。具体请参考setting-your-commit-email-address说明。
1 2 3 4 5 6
| - name: Install hexo dependencies run: | npm i -g hexo-cli cd master npm i
|
安装hexo站点依赖,因为我在检出代码的时候设置了代码的位置master,所以这里需要先进入这个目录,再去安装hexo的依赖。
1 2 3 4 5 6 7
| - name: Deploy Blog env: TZ: Asia/Shanghai HEXO_ALGOLIA_INDEXING_KEY: ${{ secrets.HEXO_ALGOLIA_INDEXING_KEY }} run: | cd master hexo generate && hexo algolia && hexo deploy
|
最后一步,当然是将网站内容生成,部署到GitHub Pages中了,同样的如果你没有设置站点路径,则没必要cd site path。之前Hexo 5.0.0执行hexo clean会报错,其实这里也没必要执行这个命令,因为每次都是一个全新的虚拟环境,完全不用clean。当然我还是将Hexo 5.0.0升级到了5.0.2。
另外很多文章都会有一步处理主题的step,我的没有是因为我是通过npm安装的主题,既然高版本支持了这样的方式,那就用起来吧,毕竟用git submodule管理主题也麻烦,每次都要拉取主题,配置版本等,直接npm install hexo-theme-next --save即可安装主题,要更新也只是改下pageage.json中的版本即可,方便又快捷。
GitHub Action定价策略
目前GitHub Action采用免费时长的营销策略:
- 对于普通用户每个月可以免费使用
2000分钟
- 对于Pro用户每个月免费使用
3000分钟
目前我总共有80余篇,执行时间为33秒每次,所以这个额度按照每日提交两次来算,普通用户都绰绰有余。