Github Actions 踩坑
简介
抱巨硬爸爸的大腿,把玩了几天 Github Actions 下来,感觉和 Gitlab CI 的使用方式差不多。都是通过编写 yaml 的方式在自动化 workflow 的世界里为所欲为。我觉得 Github Action 的优势在于不用写复杂的 CI/CD 脚本,只需在 marketplace 或是其他开源的仓库里寻找自己所需的工作流,然后任意组合即可实现强大功能。
案例
下面分享一个我自己的案例,我需要实现的 workflow 是当代码提交到 master
分支的时候,编译打包一个 Vue 项目,然后将打包后的文件 push 到 gh-pages
分支。代码如下:
name: Build
on:
push:
branches:
- master
jobs:
build:
if: github.repository == 'Vincent0700/awtrix-simulator'
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x]
steps:
- name: Checkout repos
uses: actions/[email protected]
- name: Setup NodeJs
uses: actions/[email protected]
with:
node-version: ${{ matrix.node-version }}
- name: Build codes
run: |
npm install
npm run build
- name: Push to gh-pages
uses: Vincent0700/[email protected]
env:
REPO: self
BRANCH: gh-pages
FOLDER: dist
GITHUB_TOKEN: ${{ secrets.REPO_ACTIONS_TOKEN }}
这里需要注意的是 [secrets.REPO_ACTIONS_TOKEN]
, 这里需要在 这里 申请一个 Access Token,然后在项目的 Settings > Secrets 中声明一个键值对 { REPO_ACTIONS_TOKEN: YOUR_TOKEN },这样在 workflow 里就能取到你的私有变量,而不必出现在仓库代码中了。