检查工作流文件
GitHub Actions 使用 YAML 语法定义工作流。 每个工作流都作为单独的 YAML 文件存储在代码存储库中,位于名为 .github/workflows的目录中。 每当将代码推送到存储库时,就会触发以下工作流示例。 工作流执行以下步骤:
- 签出推送的代码。
- 安装 Node.js。
- 安装 Bash 自动测试系统 (Bats) 测试框架。
- 运行命令以输出 Bats 版本:
bats -v。
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@v4
with:
node-version: '20'
- run: npm install -g bats
- run: bats -v
了解工作流文件
为了帮助你了解 YAML 语法如何用于创建工作流文件,本部分介绍上一个示例的每一行:
# Optional - The name of the workflow as it will appear in the "Actions" tab of the GitHub repository. If this field is omitted, the name of the workflow file will be used instead.
name: learn-github-actions
# Optional - The name for workflow runs generated that appear in the list of workflow runs on your repository's "Actions" tab. This example uses an expression with the 'github' context to display the username of the actor that triggered the workflow run.
run-name: ${{ github.actor }} is learning GitHub Actions
# Specifies the trigger for this workflow. This example uses the 'push' event, so a workflow run is triggered every time someone pushes a change to the repository or merges a pull request.
on: [push]
# Groups together all the jobs that run in the 'learn-github-actions' workflow.
jobs:
# Defines a job named 'check-bats-version'. The child keys will define properties of the job.
check-bats-version:
# Configures the job to run on the latest version of an Ubuntu Linux runner. This means that the job will execute on a fresh virtual machine hosted by GitHub.
runs-on: ubuntu-latest
# Groups together all the steps that run in the 'check-bats-version' job. Each item nested under this section is a separate action or shell script.
steps:
# The 'uses' keyword specifies that this step will run 'v4' of the 'actions/checkout' action. This is an action that checks out your repository onto the runner, allowing you to run scripts or other actions against your code (such as build and test tools). You should use the checkout action any time your workflow will use the repository's code.
- uses: actions/checkout@v4
# This step uses the 'actions/setup-node@v4' action to install the specified version of the Node.js. (This example uses version 20.) This puts both the 'node' and 'npm' commands in your PATH.
- uses: actions/setup-node@v4
with:
node-version: '20'
# The 'run' keyword tells the job to execute a command on the runner. In this case, you are using 'npm' to install the 'bats' software testing package.
- run: npm install -g bats
# The 'bats' command with a parameter that outputs the software version.
- run: bats -v
查看工作流运行的活动
当工作流程被触发时,将创建一个工作流程运行实例以执行该工作流程。 工作流运行启动后,可以看到运行进度的可视化图,并在 GitHub 上查看每个步骤的活动。 按照以下步骤查看活动:
导航到存储库的主页。
选择存储库名称下的“操作”。
在左侧栏中,选择要查看的工作流。
从工作流运行列表中,选择运行的名称以查看工作流运行摘要。
在左侧边栏或可视化图中,选择要查看的作业。
选择一个步骤以查看详细结果。
了解工作流文件组件后,即可了解开发人员如何为各种用例自定义文件。