概览
This article describes some of the advanced features of GitHub Actions that help you create more complex workflows.
存储密码
如果您的工作流程使用敏感数据,例如密码或证书, 您可以将这些信息在 GitHub 中保存为 机密,然后在工作流中将它们用作环境变量。 这意味着您将能够创建和共享工作流程,而无需直接在 YAML 工作流程中嵌入敏感值。
此示例操作演示如何将现有机密引用为环境变量,并将其作为参数发送到示例命令。
jobs:
example-job:
runs-on: ubuntu-latest
steps:
- name: Retrieve secret
env:
super_secret: ${{ secrets.SUPERSECRET }}
run: |
example-command "$super_secret"
更多信息请参阅“创建和存储加密密码”。
创建依赖的作业
默认情况下,工作流程中的作业同时并行运行。 因此,如果您有一个作业必须在另一个作业完成后运行,可以使用 needs 关键字来创建此依赖项。 如果其中一个作业失败,则跳过所有从属作业;但如果您需要作业继续,可以使用条件语句 if 来定义。
在此示例中,setup、build 和 test 作业连续运行,build 和 test 取决于其前面的作业成功完成:
jobs:
setup:
runs-on: ubuntu-latest
steps:
- run: ./setup_server.sh
build:
needs: setup
runs-on: ubuntu-latest
steps:
- run: ./build_server.sh
test:
needs: build
runs-on: ubuntu-latest
steps:
- run: ./test_server.sh
更多信息请参阅 jobs.<job_id>.needs。
使用构建矩阵
如果您希望工作流程跨操作系统、平台和语言的多个组合运行测试,可以使用构建矩阵。 构建矩阵是使用 strategy 关键字创建的,它接收构建选项作为数组。 例如,此构建矩阵将使用不同版本的 Node.js 多次运行作业:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node: [6, 8, 10]
steps:
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
更多信息请参阅 jobs.<job_id>.strategy.matrix。
缓存依赖项
GitHub 托管的运行器启动为每个作业的新环境,如果您的作业定期重复使用依赖项,您可以考虑缓存这些文件以帮助提高性能。 缓存一旦创建,就可用于同一仓库中的所有工作流程。
此示例演示如何缓存 ~/.npm 目录:
jobs:
example-job:
steps:
- name: Cache node modules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
更多信息请参阅“缓存依赖项以加快工作流程”。
使用数据库和服务容器
如果作业需要数据库或缓存服务,可以使用 services 关键字创建临时容器来托管服务;生成的容器然后可用于该作业中的所有步骤,并在作业完成后删除。 此示例演示作业如何使用 services 创建 postgres 容器,然后使用 node 连接到服务。
jobs:
container-job:
runs-on: ubuntu-latest
container: node:10.18-jessie
services:
postgres:
image: postgres
steps:
- name: Check out repository code
uses: actions/checkout@v2
- name: Install dependencies
run: npm ci
- name: Connect to PostgreSQL
run: node client.js
env:
POSTGRES_HOST: postgres
POSTGRES_PORT: 5432
更多信息请参阅“使用数据库和服务容器”。
使用标签路由工作流程
此功能可帮助您将作业分配到特定的托管运行器。 如果要确保特定类型的运行器处理作业,可以使用标签来控制作业的执行位置。 You can assign labels to a self-hosted runner in addition to their default label of self-hosted. Then, you can refer to these labels in your YAML workflow, ensuring that the job is routed in a predictable way. GitHub-hosted runners have predefined labels assigned.
此示例显示工作流程如何使用标签来指定所需的运行器:
jobs:
example-job:
runs-on: [self-hosted, linux, x64, gpu]
A workflow will only run on a runner that has all the labels in the runs-on array. The job will preferentially go to an idle self-hosted runner with the specified labels. If none are available and a GitHub-hosted runner with the specified labels exists, the job will go to a GitHub-hosted runner.
To learn more about self-hosted runner labels, see "Using labels with self-hosted runners." To learn more about GitHub-hosted runner labels, see "Supported runners and hardware resources".
使用环境
您可以使用保护规则和机密配置环境。 工作流程中的每个作业都可以引用单个环境。 在将引用环境的作业发送到运行器之前,必须通过为环境配置的任何保护规则。 更多信息请参阅“环境”。
使用工作流程模板
GitHub 提供预配置的工作流程模板,您可以自定义以创建自己的持续集成工作流程。 GitHub 分析代码并显示可能适用于您的仓库的 CI 模板。 例如,如果仓库包含 Node.js 代码,您就会看到 Node.js 项目的建议。 您可以使用工作流程模板作为基础来构建自定义工作流程,或按原样使用模板。
您可以在 actions/starter-workflows 仓库中浏览工作流程模板的完整列表。
- 在 GitHub 上,导航到仓库的主页面。
- 在仓库名称下,单击 Actions(操作)。

- 如果您的仓库已经有工作流程:在左上角单击 New workflow(新工作流程)。

- 在您想要使用的模板名称下,单击 Set up this workflow(设置此工作流程)。

后续步骤
要继续了解 GitHub Actions,请参阅“与组织共享工作流程”。