示例概述
This article uses an example workflow to demonstrate some of the main CI features of GitHub Actions. 触发此工作流程时,它将使用具有 npm test
的测试组合矩阵来测试代码。
The following diagram shows a high level view of the workflow's steps and how they run within the job:
此示例中使用的功能
The example workflow demonstrates the following capabilities of GitHub Actions:
功能 | 实现 |
---|---|
Manually running a workflow from the UI: | workflow_dispatch |
Triggering a workflow to run automatically: | pull_request |
Running a workflow at regular intervals: | schedule |
Setting permissions for the token: | permissions |
Controlling how many workflow runs or jobs can run at the same time: | concurrency |
在不同的运行器上运行作业,具体取决于存储库: | runs-on |
Preventing a job from running unless specific conditions are met: | if |
使用矩阵创建不同的测试配置: | matrix |
Cloning your repository to the runner: | actions/checkout |
Installing node on the runner: | actions/setup-node |
缓存依赖项: | actions/cache |
示例工作流程
The following workflow was created by the GitHub Docs Engineering team. To review the latest version of this file in the github/docs
repository, see test.yml
.
Note: Each line of this workflow is explained in the next section at "Understanding the example."
|
了解示例
The following table explains how each of these features are used when creating a GitHub Actions workflow.
代码 | 说明 |
---|---|
|
The name of the workflow as it will appear in the "Actions" tab of the GitHub repository. |
|
“on”关键字允许您定义在工作流程运行时触发的事件。 您可以在此处定义多个事件。 更多信息请参阅“触发工作流程”。 |
|
如果您希望能够在 UI 中手动运行此工作流程,请添加“workflow_dispatch”事件。 更多信息请参阅 |
|
添加“pull_request”事件,以便每次创建或更新拉取请求时工作流程自动运行。 更多信息请参阅 |
|
Add the |
|
修改授予“GITHUB_TOKEN”的默认权限。 这将因工作流程的需求而异。 更多信息请参阅“为作业分配权限”。 |
|
为特定事件创建并发组,并使用“||' 运算符来定义回退值。 更多信息请参阅“使用并发”。 |
|
取消同一并发组中任何当前正在运行的作业或工作流程。 |
|
将工作流程文件中运行的所有作业组合在一起。 |
|
定义 ID 为“test”的作业,该作业存储在“jobs”键中。 |
|
将作业配置为在 GitHub 托管的运行器或自托管运行器上运行,具体取决于运行工作流程的存储库。 在此示例中,如果存储库名为“docs-internal”并且位于“github”组织内,则作业将在自托管运行器上运行。 如果存储库与此路径不匹配,则它将在 GitHub 托管的“ubuntu-latest”运行器上运行。 有关这些选项的更多信息,请参阅“为作业选择运行器”。 |
|
设置在自动取消作业之前让作业运行的最大分钟数。 更多信息请参阅 |
|
本节定义作业的生成矩阵。 |
|
将“fail-fast”设置为“false”可防止 GitHub 在任何矩阵作业失败时取消所有正在进行的作业。 |
|
创建一个名为“test-group”的矩阵,其中包含一个测试组数组。 这些值匹配将由“npm test”运行的测试组名称。 |
|
组合将作为“test”作业一部分运行的所有步骤。 工作流程中的每个作业都有自己的“steps”部分。 |
|
“uses”关键字告诉作业检索名为“actions/checkout”的操作。 这是检出仓库并将其下载到运行器的操作,允许针对您的代码运行操作(例如测试工具)。 只要工作流程针对仓库的代码运行,或者您使用仓库中定义的操作,您都必须使用检出操作。 使用“with”键为操作提供了一些额外的选项。 |
|
如果当前存储库是“github/docs-internal”存储库,则此步骤使用“actions/github-script”操作来运行脚本以检查是否存在名为“docs-early-access”的分支。 |
|
如果当前存储库是“github/docs-internal”存储库,则此步骤将从上一步中标识的“github/docs-early-access”中签出分支。 |
|
如果当前存储库是“github/docs-internal”存储库,则此步骤使用“run”关键字执行shell命令,以将“docs-early-access”存储库的文件夹移动到主存储库的文件夹中。 |
|
此步骤运行命令以从存储库中签出 LFS 对象。 |
|
此步骤使用“trilom/file-changes-action”操作来收集在拉取请求中更改的文件,以便可以在下一步中分析它们。 此示例使用 'a6ca26c14274c33b15e6499323aac178af06ad4b' SHA固定到操作的特定版本。 |
|
此步骤运行 shell 命令,以使用上一步的输出来创建文件,其中包含在拉取请求中更改的文件列表。 |
|
此步骤使用“actions/setup-node”操作在运行器上安装指定版本的“node”软件包,这使您可以访问“npm”命令。 |
|
此步骤运行 |
|
此步骤使用“actions/cache”操作来缓存 Next.js 构建,以便工作流程尝试检索构建的缓存,而不是每次都从头开始重新构建。 更多信息请参阅“缓存依赖关系以加快工作流程”。 |
|
此步骤运行构建脚本。 |
|
此步骤使用“npm test”运行测试,并且测试矩阵为矩阵中的每个作业提供不同的 |
后续步骤
- To learn about GitHub Actions concepts, see "Understanding GitHub Actions."
- For more step-by-step guide for creating a basic workflow, see "Quickstart for GitHub Actions."
- If you're comfortable with the basics of GitHub Actions, you can learn about workflows and their features at "About workflows."