name: learn-github-actions # (可选)将出现在 GitHub 仓库的 Actions 选项卡中的工作流程名称。
# 触发器
on: [push] # 指定此工作流程的触发器,更多细节:https://docs.github.com/cn/actions/using-workflows/workflow-syntax-for-github-actions#onpushpull_requestpull_request_targetpathspaths-ignore
# 作业
jobs: # 该工作流的所有作业
check-bats-version: # 子作业的名称(自定义)
runs-on: ubuntu-latest # 该作业运行的环境,更多细节:https://docs.github.com/cn/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on
steps: # 该子作业的所有步骤
- name: Checkout repository # 该步骤的名称
uses: actions/checkout@v3 # 复用 https://github.com/actions/checkout/tree/v3
- name: Setup nodejs
uses: actions/setup-node@v3 # 复用 https://github.com/actions/setup-node/tree/v3
with: # 附加选项
node-version: '14' # 设定 node 的版本
- name: Install bats
run: |
npm install -g bats # run 指示作业在运行器上执行命令
bats -v
build: # 子作业
runs-on: ubuntu-latest
# 策略矩阵:通过创建变量来自动构建基于变量组合的多个作业
strategy:
matrix:
node: [12, 14, 16]
steps:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
setup: # 子作业
runs-on: ubuntu-latest
# 自作业之间默认是并行执行的,可以通过 needs 来指定依赖,更多细节:https://docs.github.com/cn/actions/using-jobs/using-jobs-in-a-workflow#defining-prerequisite-jobs
needs: build # 依赖 build 作业
steps:
- run: ./build_server.sh # 相对于仓库主目录
on:
# 在 push 时被触发
push:
branches:
- '**' # 任意分支
- 'releases/**-alpha'
- '!releases/**' # 所有 releases/** 都不触发,除了 releases/**-alpha
# 使该 workflow 依赖于其他 workflow 触发
workflow_run:
workflows: [ "Build" ] # 当名为 Build 的 workflow 在满足以下条件的分支上执行时,本 workflow 才会被执行
types: [ requested ] # requested/completed
branches:
- 'releases/**'
- '!releases/**-alpha'
# 使该 workflow 能被其他 workflow 调用
workflow_call:
# 使该 workflow 能在 GitHub 上手动触发
workflow_dispatch:
# 执行 release 时触发(GitHub 项目页)
release:
types: [ published ] # published/created/edited/deleted/...
# 定时触发
schedule:
- cron: '0 0 * * *' # cron 语法,UTC 时间,+08:00 即北京时间
steps:
- name: Install dependencies on Windows
if: matrix.os == 'windows-latest'
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest coverage
if (Test-Path -Path 'requirements.txt' -PathType Leaf) {pip install -r requirements.txt}
矩阵策略 - jobs.<job_id>.strategy.matrix
strategy:
fail-fast: false # 当矩阵中有任何一个作业失败时,是否停止其他所有作业,默认为 true,建议设为 false
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
os: [ubuntu-latest, macos-latest, windows-latest]
复用流程 - jobs.<job_id>.uses
steps:
- name: Checkout
uses: actions/checkout@v3 # 仓库
- name: Build
uses: ./.github/workflows/build.yml # 文件
on:
workflow_run:
workflows: [ "Build" ] # 当名为 Build 的 workflow 完成时
types: [ completed ]
jobs:
build:
# ...
publish: # 依赖 build 完成时
needs: build
仓库主页 -> Settings -> Secrets -> Actions -> New repository secret
steps:
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
![workflow](https://github.com/<user>/<repo>/actions/workflows/$file_name.yml/badge.svg?branch=master)