GitHub Actions

精选 GitHub Actions 常用指令与核心速查备忘单,涵盖高频用法、配置参数与实用技巧。

#入门指南

#简介 (Introduction)

GitHub Actions 是一套 CI/CD 平台,可在 GitHub 仓库中直接自动化构建、测试与部署等工作流。


#工作流文件 (Workflow Files)

GitHub Actions 工作流使用特殊 YAML 文件定义,通常存放在仓库的 .github/workflows 目录下。

name: hello-world
on: push
jobs:
  hello-world-job:
    runs-on: ubuntu-latest
    steps:
      - name: Hello World
        run: echo "Hello World!"

Viewing your workflow runs

  • On GitHub.com, navigate to the main page of the repository.
  • Under your repository name, click Actions.
  • In the left sidebar, click the workflow you want to display, in this example "hello-world"

#工作流语法 (Workflow Syntax)

name: learn-github-actions
run-name: ${{ github.actor }} is learning GitHub Actions
on: [push]
jobs:
  check-bats-version:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v3
        with:
          node-version: '14'
      - run: npm install -g bats
      - run: bats -v

#工作流语法描述 (Workflow Syntax Descriptions)

描述
name: 设置 GitHub Actions 工作流的名称。它是用于在仓库中识别工作流的标签。
run-name: 为运行设置自定义名称,使用 GitHub 上下文 ${{ github.actor }} 包含启动运行的用户名称。
on: 指定触发工作流的事件。在这种情况下,工作流在对仓库的任何 push 事件时触发。
jobs: 定义将作为工作流的一部分执行的作业组。工作流中的每个作业独立运行。
check-bats-version: 工作流中特定作业的标识符。此作业名为 check-bats-version
runs-on: 指定运行作业的机器类型。这里设置为在最新版本的 Ubuntu 上运行。
steps: 包含将作为作业的一部分执行的任务(步骤)序列。
uses: 用于指定要包含为步骤一部分的操作。例如,actions/checkout@v4 检出仓库,actions/setup-node@v3 设置 Node 环境。
with: 指定操作的附加参数。与 uses 结合使用以配置操作。
node-version: 包含 with 下的参数,指定由 setup-node 操作设置的 Node.js 版本。在这种情况下,它设置为版本 '14'。

#事件 (Events)

name: Event-trigger-on-push-example

on: [push] # 在此处定义事件

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Run a script
        run: echo "This workflow runs on every push to the repository."

事件触发器

事件名称 描述
push 在推送到仓库时触发。
pull_request 在拉取请求事件时触发。
pull_request_review 在拉取请求审查事件时触发。
pull_request_review_comment 在拉取请求审查的评论时触发。
pull_request_target 用于分叉仓库中的工作流。
fork 在仓库被分叉时触发。
issue_comment 在问题和 PR 评论时触发。
issues 在问题事件时触发。
label 在标签事件时触发。
milestone 在里程碑事件时触发。
deployment 在部署时触发。
deployment_status 在部署状态更新时触发。
public 在仓库从私有变为公开时触发。
repository_dispatch 在自定义仓库事件时触发。
schedule 在定义的时间表上触发。
workflow_dispatch 允许手动触发工作流。
workflow_run 在另一个工作流完成时触发。
create 在创建分支或标签时触发。
delete 在删除分支或标签时触发。
page_build 在 GitHub Pages 构建事件时触发。
release 在发布事件时触发。
watch 在有人为仓库加星时触发。
registry_package 在注册表包事件时触发。
status 在 Git 提交的状态更新时触发。
project 在项目看板事件时触发。
project_card 在项目卡事件时触发。
project_column 在项目列事件时触发。
member 在协作者事件时触发。
gollum 在 wiki 页面更新时触发。

#作业 (Jobs)

单个作业:

name: Single Job
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Run a build script
        run: script/build

多个作业:

name: CI Workflow

on: [push]

jobs:
  job-1:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Runs job 1
        run: echo "Running Job 1"

  job-2:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Runs job 2
        run: echo "Running Job 2"

  job-3:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Runs job 3
        run: echo "Running Job 3"

#步骤 (Steps)

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
# 步骤 1
      - name: Check out repository
        uses: actions/checkout@v2

# 步骤 2
      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14'

# 步骤 3
      - name: Install dependencies
        run: npm install

# 步骤 4
      - name: Run tests
        run: npm test

#Github 运行器与自托管运行器

Github 托管运行器

name: Workflow
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest # 默认运行器
    steps:
      - uses: actions/checkout@v2
      - name: Run a script
        run: echo "Hello, world!"

自托管运行器

name: Workflow with Self-Hosted Runner
on: [push]
jobs:
  build:
    runs-on: self-hosted
    steps:
      - uses: actions/checkout@v2
      - name: Run a script
        run: echo "Hello from self-hosted runner!"

#环境变量 (Environment Variables)

使用环境定义的自定义变量。

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      CUSTOM_VARIABLE: 'Hello, World!' # 使用 env: 定义的自定义变量
    steps:
      - name: Check environment variable
        run: echo "Value of CUSTOM_VAR is $CUSTOM_VAR"

#密钥 (Secrets)

要在 github 仓库中添加新密钥,请导航到 Repository > Settings > Security > Secrets and Variables > Actions > New Repository Secret

密钥工作流示例:

name: Workflow with Secrets

on: [push]

jobs:
  example_job:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
      - name: Use a secret
        run: echo "The secret is ${{ secrets.MY_SECRET }}"

#构件 (Artifacts)

要访问你的构件,请导航到 Repository > Actions > Workflow Run > Artifacts

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Build project
        run: make build
      - name: Upload build artifact
        uses: actions/upload-artifact@v3 # 上传构件预构建操作
        with:
          name: my-artifact
          path: path/to/artifact

#缓存依赖项 (Caching Dependencies)

依赖项缓存存储工作流的下载包或编译二进制文件。

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Cache dependencies
        uses: actions/cache@v2 # 存储下载的包或编译的二进制文件
        with:
          path: |
            path/to/dependencies
            another/path
          key: ${{ runner.os }}-deps-${{ hashFiles('**/lockfile') }} # 在操作系统中生成依赖项锁定文件的哈希
      - name: Install dependencies
        run: install-command

#矩阵策略 (Matrix Strategies)

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [12.x, 14.x, 16.x]
# 矩阵策略运行使你能够跨多个环境和操作系统组合运行作业
        os: [ubuntu-latest, windows-latest, macOS-latest]
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm install
      - run: npm test
        env:
          CI: true

#条件和表达式 (Conditions and Expressions)

分支条件:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Run build
        if: github.ref == 'refs/heads/main' # "Run build" 步骤仅在当前分支为 main 时执行。
        run: make build

事件触发条件:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Run tests
        if: github.event_name == 'pull_request' # "Run tests" 步骤仅在工作流由拉取请求事件触发时执行
        run: npm test

#工作流命令 (Workflow Commands)

根据你的操作系统,如果你运行的是 ubuntu-latest,bash 命令应该可以工作

steps:
  - name: Set environment variable
    run: echo "NAME=value" >> $GITHUB_ENV

#并发 (Concurrency)

并发字段根据 github.head_ref 创建一个组。如果在同一并发组内启动新运行,它会取消任何正在进行的运行。

jobs:
  my_job:
    runs-on: ubuntu-latest
    concurrency:
      group: ${{ github.head_ref }}
      cancel-in-progress: true
    steps:
      - name: Run a script
        run: echo "Running script..."

#🔗 参考资源