示例概述
This article uses an example workflow to demonstrate some of the main CI features of GitHub Actions. 此工作流程被触发时,它会自动运行一个脚本,用于检查 GitHub 文档网站是否有任何断开的链接。 如果发现任何断开的链接,工作流程将使用 GitHub CLI 创建包含详细信息的 GitHub 议题。
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:
功能 | 实现 |
---|---|
Running a workflow at regular intervals: | schedule |
Setting permissions for the token: | permissions |
Preventing a job from running unless specific conditions are met: | if |
Referencing secrets in a workflow: | Secrets |
Cloning your repository to the runner: | actions/checkout |
Installing node on the runner: | actions/setup-node |
使用第三方操作: | peter-evans/create-issue-from-file |
示例工作流程
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 check-all-english-links.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. |
|
将“workflow_dispatch”和“scheduled”定义为工作流程的触发器:
|
|
修改授予“GITHUB_TOKEN”的默认权限。 这将因工作流程的需求而异。 更多信息请参阅“为作业分配权限”。 |
|
将工作流程文件中运行的所有作业组合在一起。 |
|
定义 ID 为“check_all_english_links”和名称为“Check all links”的作业,该作业存储在“jobs”键中。 |
|
仅当存储库名为“docs-internal”并且位于“github”组织内时,才运行“checkall_english_links”作业。 否则,作业将标记为 _skiped。 |
|
配置作业在 Ubuntu Linux 运行器上运行。 这意味着作业将在由 GitHub 托管的新虚拟机上执行。 有关使用其他运行器的语法示例,请参阅“GitHub Actions 的工作流语法”。 |
|
创建自定义环境变量,并重新定义内置的“GITHUB_TOKEN”变量以使用自定义 secret。 稍后将在工作流程中引用这些变量。 |
|
组合将作为“check_all_english_links”作业一部分运行的所有步骤。 工作流程中的每个作业都有自己的“steps”部分。 |
|
“uses”关键字告诉作业检索名为“actions/checkout”的操作。 这是检出仓库并将其下载到运行器的操作,允许针对您的代码运行操作(例如测试工具)。 只要工作流程针对仓库的代码运行,或者您使用仓库中定义的操作,您都必须使用检出操作。 |
|
此步骤使用“actions/setup-node”操作在运行器上安装指定版本的“node”软件包,这使您可以访问“npm”命令。 |
|
“run”关键字指示作业在运行器上执行命令。 在这种情况下,“npm ci”和“npm run build”命令作为单独的步骤运行,以在存储库中安装和构建 Node.js 应用程序。 |
|
此“run”命令执行存储在存储库中“script/check-english-links.js”的脚本,并将输出传递到名为“broken_links.md”的文件。 |
|
如果“check-english-links.js”脚本检测到断开的链接并返回非零(失败)退出状态,则使用 工作流程命令 来设置具有 “broken_links.md” 文件第一行值的输出(这是在下一步中使用)。 |
|
使用“peter-evans/create-issue-from-file”操作创建新的 GitHub 议题。 此示例使用 'b4f9ee0a9d4abbfc6986601d9b1a4f8f8e74c77e' SHA 固定到操作的特定版本。 |
|
使用
|
|
如果上一次运行中的议题已打开并分配给某人,请使用 |
|
如果上一次运行中的议题已打开且未分配给任何人,则:
|
后续步骤
- 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."