Skip to main content

了解 GitHub Actions

学习 GitHub Actions 的基础知识,包括核心概念和基本术语。

概述

GitHub Actions 是一种持续集成和持续交付 (CI/CD) 平台,可用于自动执行生成、测试和部署管道。 您可以创建工作流程来构建和测试存储库的每个拉取请求,或将合并的拉取请求部署到生产环境。

GitHub Actions 不仅仅是 DevOps,还允许您在存储库中发生其他事件时运行工作流程。 例如,您可以运行工作流程,以便在有人在您的存储库中创建新问题时自动添加相应的标签。

GitHub 提供 Linux、Windows 和 macOS 虚拟机来运行工作流程,或者您可以在自己的数据中心或云基础架构中托管自己的自托管运行器。

GitHub Actions 的组件

可配置 GitHub Actions 工作流,使其在存储库中发生事件(例如打开拉取请求或创建问题)时触发 。 工作流包含一个或多个可按顺序或并行运行的作业。 每个作业都将在其自己的虚拟机运行器中或在容器中运行,并具有一个或多个步骤,用于运行定义的脚本或运行动作。动作是一个可重用的扩展,可简化工作流 。

触发运行器 1(它触发运行器 2 以运行作业 2 )以运行作业 1 的事件的示意图。 每个作业都分为多个步骤。

工作流

工作流程是一个可配置的自动化过程,它将运行一个或多个作业。 工作流程由签入到存储库的 YAML 文件定义,并在存储库中的事件触发时运行,也可以手动触发,或按定义的时间表触发。

工作流程在存储库的 .github/workflows 目录中定义,存储库可以有多个工作流程,每个工作流程都可以执行不同的任务集。 例如,您可以有一个工作流程来构建和测试拉取请求,另一个工作流程用于在每次创建发布时部署应用程序,还有一个工作流程在每次有人打开新议题时添加标签。

可以在另一个工作流中引用工作流。 有关详细信息,请参阅“重新使用工作流”。

有关工作流的详细信息,请参阅“使用工作流”。

事件

事件是存储库中触发工作流程运行的特定活动。 例如,当有人创建拉取请求、打开议题或将提交推送到存储库时,活动可能源自 GitHub。 此外,还可以通过发布到 REST API 或者手动方式触发工作流按计划运行。

有关可用于触发工作流的事件的完整列表,请参阅触发工作流的事件

作业

作业是工作流中在同一运行器上执行的一组步骤。 每个步骤要么是一个将要执行的 shell 脚本,要么是一个将要运行的动作。 步骤按顺序执行,并且相互依赖。 由于每个步骤都在同一运行器上执行,因此您可以将数据从一个步骤共享到另一个步骤。 例如,可以有一个生成应用程序的步骤,后跟一个测试已生成应用程序的步骤。

您可以配置作业与其他作业的依赖关系;默认情况下,作业没有依赖关系,并且彼此并行运行。 当一个作业依赖于另一个作业时,它将等待从属作业完成,然后才能运行。 例如,对于没有依赖关系的不同体系结构,您可能有多个生成作业,以及一个依赖于这些作业的打包作业。 生成作业将并行运行,当它们全部成功完成后,打包作业将运行。

有关作业的详细信息,请参阅“使用作业”。

操作

操作是用于 GitHub Actions 平台的自定义应用程序,它执行复杂但经常重复的任务。 使用操作可帮助减少在工作流程文件中编写的重复代码量。 操作可以从 GitHub 拉取 git 存储库,为您的构建环境设置正确的工具链,或设置对云提供商的身份验证。

您可以编写自己的操作,也可以在 GitHub Marketplace 中找到要在工作流程中使用的操作。

有关详细信息,请参阅“创建操作”。

运行程序

运行程序是触发工作流时运行工作流的服务器。 每个运行器一次可以运行一个作业。 GitHub 提供 Ubuntu Linux、Microsoft Windows 和 macOS 运行器来运行您的工作流程;每个工作流程运行都在新预配的全新虚拟机中执行。 GitHub 还提供 大型运行器(适用于大型配置)。 有关详细信息,请参阅“关于较大的运行器”。 如果需要其他操作系统或特定硬件配置,可托管自己的运行器。有关自托管运行器的详细信息,请参阅“托管您自己的运行器”。

创建示例工作流程

GitHub Actions 使用 YAML 语法来定义工作流程。 每个工作流都作为单独的 YAML 文件存储在代码存储库中名为 .github/workflows 的目录中。

您可以在仓库中创建示例工作流程,只要推送代码,该工作流程就会自动触发一系列命令。 在此工作流中,GitHub Actions 签出推送的代码,安装 bats 测试框架,并运行基本命令来输出 bats 版本:bats -v

  1. 在存储库中,创建 .github/workflows/ 目录来存储工作流文件。

  2. .github/workflows/ 目录中,创建一个名为 learn-github-actions.yml 的新文件并添加以下代码。

    YAML
    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: '14'
          - run: npm install -g bats
          - run: bats -v
    
  3. 提交这些更改并将其推送到您的 GitHub 仓库。

您的新 GitHub Actions 工作流程文件现在安装在您的仓库中,每次有人推送更改到仓库时都会自动运行。 若要查看关于工作流执行历史记录的详细信息,请参阅“查看工作流运行的活动”。

了解工作流程文件

为帮助您了解如何使用 YAML 语法来创建工作流程文件,本节解释介绍示例的每一行:

YAML
name: learn-github-actions

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.

run-name: ${{ github.actor }} is learning GitHub Actions

Optional - The name for workflow runs generated from the workflow, which will 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. For more information, see "GitHub Actions 的工作流语法."

on: [push]

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. This is triggered by a push to every branch; for examples of syntax that runs only on pushes to specific branches, paths, or tags, see "GitHub Actions 的工作流语法."

jobs:

Groups together all the jobs that run in the learn-github-actions workflow.

  check-bats-version:

Defines a job named check-bats-version. The child keys will define properties of the job.

    runs-on: ubuntu-latest

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. For syntax examples using other runners, see "GitHub Actions 的工作流语法"

    steps:

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.

      - uses: actions/checkout@v4

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/setup-node@v4
        with:
          node-version: '14'

This step uses the actions/setup-node@v4 action to install the specified version of the Node.js. (This example uses version 14.) This puts both the node and npm commands in your PATH.

      - run: npm install -g bats

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: bats -v

Finally, you'll run the bats command with a parameter that outputs the software version.

# 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 from the workflow, which will 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. For more information, see "[AUTOTITLE](/actions/using-workflows/workflow-syntax-for-github-actions#run-name)."
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.  This is triggered by a push to every branch; for examples of syntax that runs only on pushes to specific branches, paths, or tags, see "[AUTOTITLE](/actions/reference/workflow-syntax-for-github-actions#onpushpull_requestpull_request_targetpathspaths-ignore)."
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. For syntax examples using other runners, see "[AUTOTITLE](/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on)"
    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 14.) This puts both the `node` and `npm` commands in your `PATH`.
      - uses: actions/setup-node@v4
        with:
          node-version: '14'

# 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

# Finally, you'll run the `bats` command with a parameter that outputs the software version.
      - run: bats -v

可视化工作流程文件

在此关系图中,您可以看到刚刚创建的工作流程文件,以及 GitHub Actions 组件在层次结构中的组织方式。 每个步骤执行单个操作或 shell 脚本。 步骤 1 和 2 运行操作,步骤 3 和 4 运行 shell 脚本。 若要为工作流查找更多预先创建的操作,请参阅“查找和自定义操作”。

显示工作流的触发器、运行器和作业的示意图。 作业分为 4 个步骤。

查看工作流运行的活动

触发工作流时,将创建执行工作流的“工作流运行”。 工作流运行开始后,可以查看运行进度的可视化图表,并在 GitHub 上查看每个步骤的活动。

  1. 在 GitHub.com 上,导航到存储库的主页。

  2. 在存储库名称下,单击 “操作”。

    “github/docs”存储库的选项卡的屏幕截图。 “操作”选项卡以橙色边框突出显示。

  3. 在左侧边栏中,单击您想要查看的工作流程。

    “操作”选项卡的左侧边栏的屏幕截图。工作流“CodeQL”以深橙色标出。

  4. 在工作流运行列表中,单击运行的名称以查看工作流运行摘要。

  5. 在左侧边栏或可视化图效果中,单击要查看的作业。

  6. 要查看某个步骤的结果,请单击该步骤。

后续步骤

GitHub Actions 可以帮助您自动执行应用程序开发过程的几乎每个方面。 准备好开始了吗? 以下是一些帮助您对 GitHub Actions 执行后续操作的有用资源:

  • 有关创建 GitHub Actions 工作流的快速方法,请参阅“使用入门工作流程”。
  • 有关生成和测试代码的持续集成 (CI) 工作流,请参阅“自动构建和测试”。
  • 有关生成和发布包,请参阅“发布包”。
  • 有关部署项目,请参阅“部署”。
  • 有关 GitHub 上的自动化任务和流程,请参阅“管理议题和拉取请求”。
  • 有关演示 GitHub Actions 更复杂功能的示例,包括上述许多用例,请参阅“示例”。 你可以看到有关如何在运行器上测试代码、访问 GitHub CLI 以及使用高级功能(如并发和测试矩阵)的详细示例。
  • 要认证你在 GitHub Actions 自动化工作流和加速开发方面的能力,可以通过 GitHub Certifications 获得 GitHub Actions 证书。 有关详细信息,请参阅“关于 GitHub Certifications”。