Warning
本文编写于 2021 年,文中工作流所使用的 Node.js 14.x(已停止维护)以及 actions/checkout@v2、actions/setup-node@v1、actions/cache@v2、peaceiris/actions-gh-pages@v3 等 Action 版本均已过时或废弃。如需在现行项目中使用,请升级至受支持的 Node.js LTS 版本及对应的最新版 Action。
我真傻,真的,单单受文档的推荐就选择了 Travis 作为部分项目的持续集成工具,没有料到它早已于 2020 年 12 月更换了免费政策,不再为开源项目提供免费的用于持续集成使用的 Credits 了。当赠送的 10000 个点数用完,就需要付费才能进行构建了。
当然,作为经济驱动的公司,近些天来又受 Github Actions 等其它持续集成工具打压了盈利空间,抛弃开源用户选择转型做起了商人事业也并非不可理解。感谢它曾为开发者提供的便利,不过作为一个佛系开发者,终于还是需要转投到别的免费工具上去了——Gihub Actions。
编写新的 workflow.yml
那么首先,我们就需要将为 Travis 编写的命名为 .travis.yml 的配 置文件,翻译成 Github Actions 能识别的 workflow.yml 配置文件。
以献给中文读者的设计模式教程这个项目为例,原有的 .travis.yml 内容如下:
language: node_js
node_js:
- lts/*
branches:
only:
- main
install:
- cd vuepress
- yarn install
script:
- yarn build
deploy:
provider: pages
skip_cleanup: true
local_dir: vuepress/docs/.vuepress/dist
github_token: $CI_DEPLOY_TOKEN
keep_history: true
on:
branch: main当检测到 main 分支代码更新后,启动持续集成工具。克隆项目,进入到项目的 vuepress 目录下执行安装依赖和生成静态文件操作,最后将 /vuepress/docs/.vuepress/dist 目录下的静态文件,上传到 gh-pages 分支,交给 Github 部署。
使用 Github Actions 实现上面的过程,首先在项目根目录创建 .github/workflows 文件夹,在文件夹内创建 workflow 配置文件,例如 deploy.yml,编写内容如下:
name: Vuepress Deployment
on:
push:
branches:
- main
jobs:
pages:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js 14.x
uses: actions/setup-node@v1
with:
node-version: "14.x"
- name: Cache NPM dependencies
uses: actions/cache@v2
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
restore-keys: |
${{ runner.OS }}-npm-cache
- name: Install Dependencies
run: |
cd vuepress
npm install
- name: Build
run: |
cd vuepress
npm run build
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
# github_token 与 deploy_key 二选一即可,不能同时配置
github_token: ${{ secrets.GITHUB_TOKEN }}
# deploy_key: ${{ secrets.ACCESS_TOKEN }}
publish_dir: vuepress/docs/.vuepress/dist
publish_branch: gh-pages创建 SSH Deploy Key
这一步是为了给 Github Actions 远程服务器访问我的 Github 账号提供凭证。如果您只想将构建产物推送到当前仓库的 gh-pages 分支,直接使用上面配置中的 github_token: ${{ secrets.GITHUB_TOKEN }} 即可(它由 GitHub Actions 自动生成,与账号是否开启双重验证无关),无需额外配置 deploy_key;但如果您需要将产物推送到其他仓库,或有更细粒度的权限需求,可以改用 deploy_key(注意移除或注释掉 github_token,两者互斥)来提供凭证。
您也可以生成 Personal access token 作为替代,不过前面的脚本中应将 deploy_key 替换为 personal_token。
启动命令行工具,创建 SSH 部署密钥:
# 进入到当前用户的 .ssh 目录下
cd ~/.ssh
# 创建 SSH 密钥
ssh-keygen -t rsa -b 4096 -C "$(git config user.email)" -f design-patterns-for-humans-zh-gh-pages其中,design-patterns-for-humans-zh-gh-pages.pub 为公钥,应上传到 Github 账户 SSH keys 设置中;不带后缀的为私钥,应作为 Github 项目仓库的 Secret,根据前面的配置,这里命名为 ACCESS_TOKEN。
最后一步
最后,移除 Github 仓库中用于 Travis 的删除原有部署密钥,例如 CI_DEPLOY_TOKEN,删除项目中的 .travis.yml 文件,提交代码到 Github 即可。