我们经常发布文档更新,此页面的翻译可能仍在进行中。有关最新信息,请访问英文文档。如果此页面上的翻译有问题,请告诉我们

Introduction to GitHub Actions

Learn about the core concepts and various components of GitHub Actions, and see an example that shows you how to add automation to your repository.

本文内容

此文档对您有帮助吗?

帮助我们创建出色的文档!

所有 GitHub 文档都是开源的。看到错误或不清楚的内容了吗?提交拉取请求。

做出贡献

或, 了解如何参与。

概览

GitHub Actions help you automate tasks within your software development life cycle. GitHub Actions are event-driven, meaning that you can run a series of commands after a specified event has occurred. For example, every time someone creates a pull request for a repository, you can automatically run a command that executes a software testing script.

This diagram demonstrates how you can use GitHub Actions to automatically run your software testing scripts. An event automatically triggers the workflow, which contains a job. The job then uses steps to control the order in which actions are run. These actions are the commands that automate your software testing.

Workflow overview

The components of GitHub Actions

Below is a list of the multiple GitHub Actions components that work together to run jobs. You can see how these components interact with each other.

Component and service overview

工作流程

The workflow is an automated procedure that you add to your repository. Workflows are made up of one or more jobs and can be scheduled or triggered by an event. The workflow can be used to build, test, package, release, or deploy a project on GitHub.

事件

An event is a specific activity that triggers a workflow. 例如,当有推送提交到仓库或者创建议题或拉取请求时,GitHub 就可能产生活动。 You can also use the repository dispatch webhook to trigger a workflow when an external event occurs. For a complete list of events that can be used to trigger workflows, see Events that trigger workflows.

Jobs

A job is a set of steps that execute on the same runner. By default, a workflow with multiple jobs will run those jobs in parallel. You can also configure a workflow to run jobs sequentially. 例如,工作流程可以有两个连续的任务来构建和测试代码,其中测试作业取决于构建作业的状态。 如果构建作业失败,测试作业将不会运行。

Steps

A step is an individual task that can run commands (known as actions). Each step in a job executes on the same runner, allowing the actions in that job to share data with each other.

操作

Actions are standalone commands that are combined into steps to create a job. 操作是工作流程最小的便携式构建块。 You can create your own actions, or use actions created by the GitHub community. 要在工作流程中使用操作,必须将其作为一个步骤。

Runners

A runner is a server that has the GitHub Actions runner application installed. You can use a runner hosted by GitHub, or you can host your own. A runner listens for available jobs, runs one job at a time, and reports the progress, logs, and results back to GitHub. For GitHub-hosted runners, each job in a workflow runs in a fresh virtual environment.

GitHub-hosted runners are based on Ubuntu Linux, Microsoft Windows, and macOS. For information on GitHub-hosted runners, see "Virtual environments for GitHub-hosted runners." If you need a different operating system or require a specific hardware configuration, you can host your own runners. For information on self-hosted runners, see "Hosting your own runners."

Create an example workflow

GitHub Actions uses YAML syntax to define the events, jobs, and steps. These YAML files are stored in your code repository, in a directory called .github/workflows.

You can create an example workflow in your repository that automatically triggers a series of commands whenever code is pushed. In this workflow, GitHub Actions checks out the pushed code, installs the software dependencies, and runs bats -v.

  1. In your repository, create the .github/workflows/ directory to store your workflow files.
  2. In the .github/workflows/ directory, create a new file called learn-github-actions.yml and add the following code.
    name: learn-github-actions
    on: [push]
    jobs:
      check-bats-version:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
          - uses: actions/setup-node@v1
          - run: npm install -g bats
          - run: bats -v
    
  3. Commit these changes and push them to your GitHub repository.

Your new GitHub Actions workflow file is now installed in your repository and will run automatically each time someone pushes a change to the repository. For details about a job's execution history, see "Viewing the workflow's activity."

Understanding the workflow file

To help you understand how YAML syntax is used to create a workflow file, this section explains each line of the introduction's example:

name: learn-github-actions
Optional - The name of the workflow as it will appear in the Actions tab of the GitHub repository.
on: [push]
Specify the event that automatically triggers the workflow file. This example uses the push event, so that the jobs run every time someone pushes a change to the repository. You can set up the workflow to only run on certain branches, paths, or tags. For syntax examples including or excluding branches, paths, or tags, see "Workflow syntax for GitHub Actions."
jobs:
Groups together all the jobs that run in the learn-github-actions workflow file.
check-bats-version:
Defines the name of the check-bats-version job stored within the jobs section.
  runs-on: ubuntu-latest
Configures the job to run on 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 "Workflow syntax for GitHub Actions."
  steps:
Groups together all the steps that run in the check-bats-version job. Each line nested under this section is a separate action.
    - uses: actions/checkout@v2
The uses keyword tells the job to retrieve v2 of the community action named actions/checkout@v2. This is an action that checks out your repository and downloads it to the runner, allowing you to run actions against your code (such as testing tools). You must use the checkout action any time your workflow will run against the repository's code or you are using an action defined in the repository.
    - uses: actions/setup-node@v1
This action installs the node software package on the runner, giving you access to the npm command.
    - 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.

Visualizing the workflow file

In this diagram, you can see the workflow file you just created and how the GitHub Actions components are organized in a hierarchy. Each step executes a single action. Steps 1 and 2 use prebuilt community actions. To find more prebuilt actions for your workflows, see "Finding and customizing actions."

Workflow overview

Viewing the job's activity

Once your job has started running, you can view each step's activity on GitHub.

  1. 在 GitHub 上,导航到仓库的主页面。

  2. 在仓库名称下,单击 Actions(操作)

    Navigate to repository

  3. In the left sidebar, click the workflow you want to see.

    Screenshot of workflow results

  4. Under "Workflow runs", click the name of the run you want to see.

    Screenshot of workflow runs

  5. Click on the job name to see the results of each step.

    Screenshot of workflow run details

后续步骤

To continue learning about GitHub Actions, see "Finding and customizing actions."

联系支持

如果需要任何与工作流程配置相关的帮助,比如语法、GitHub 托管的运行器或构建操作,请在 GitHub Community Support 的 GitHub Actions 版面中查找现有主题或发起一个新主题。

如果您有对 GitHub Actions 的反馈或功能请求,请在 GitHub 操作的反馈表 中分享。

如有以下任意情况,无论您的使用或预期使用是否属于使用限制类别,都请联系 GitHub Support or GitHub Premium Support

  • 如果您认为您的帐户受到不正确的限制
  • 如果您在执行您的操作之一时遇到意外错误,例如:唯一的 ID
  • 如果你遇到与预期矛盾现有行为,但不一定是文件记录的情况。

此文档对您有帮助吗?

帮助我们创建出色的文档!

所有 GitHub 文档都是开源的。看到错误或不清楚的内容了吗?提交拉取请求。

做出贡献

或, 了解如何参与。