Skip to main content

Quickstart for GitHub Actions

Try out the core features of GitHub Actions in minutes.

Introduction

GitHub Actions 是一种持续集成和持续交付 (CI/CD) 平台,可用于自动执行生成、测试和部署管道。 You can create workflows that run tests whenever you push a change to your repository, or that deploy merged pull requests to production.

This quickstart guide shows you how to use the user interface of GitHub to add a workflow that demonstrates some of the essential features of GitHub Actions.

若要开始使用预配置的工作流,请浏览 actions/starter-workflows 存储库中的模板列表。 有关详细信息,请参阅“使用工作流模板”。

For an overview of GitHub Actions workflows, see 关于工作流程. If you want to learn about the various components that make up GitHub Actions, see Understanding GitHub Actions.

Using workflow templates

GitHub 提供预配置的工作流模板,可以按原样使用或自定义它来创建自己的工作流。 GitHub 分析代码并显示可能对仓库有用的工作流模板。 例如,如果仓库包含 Node.js 代码,您就会看到 Node.js 项目的建议。

这些工作流模板旨在帮助你快速启动和运行,提供了一系列配置,例如:

使用这些工作流作��构建自定义工作流的起点或按原样使用它们。 可以在 actions/starter-workflows 存储库中浏览工作流模板的完整列表。

Prerequisites

This guide assumes that:

  • You have at least a basic knowledge of how to use GitHub. If you don't, you'll find it helpful to read some of the articles in the documentation for repositories and pull requests first. For example, see 仓库快速入门, 关于分支, and 关于拉取请求.

  • You have a repository on GitHub where you can add files.

  • You have access to GitHub Actions.

    注意

    If the Actions tab is not displayed under the name of your repository on GitHub, it may be because Actions is disabled for the repository. For more information, see 管理存储库的 GitHub Actions 设置.

Creating your first workflow

  1. In your repository on GitHub, create a workflow file called github-actions-demo.yml in the .github/workflows directory. To do this:

    • If the .github/workflows directory already exists, navigate to that directory on GitHub, click Add file, then click Create new file, and name the file github-actions-demo.yml.

    • If your repository doesn't have a .github/workflows directory, go to the main page of the repository on GitHub, click Add file, then click Create new file, and name the file .github/workflows/github-actions-demo.yml. This creates the .github and workflows directories and the github-actions-demo.yml file in a single step.

    注意

    For GitHub to discover any GitHub Actions workflows in your repository, you must save the workflow files in a directory called .github/workflows.

    You can give the workflow file any name you like, but you must use .yml or .yaml as the file name extension. YAML is a markup language that's commonly used for configuration files.

  2. Copy the following YAML contents into the github-actions-demo.yml file:

    YAML
    name: GitHub Actions Demo
    run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
    on: [push]
    jobs:
      Explore-GitHub-Actions:
        runs-on: ubuntu-latest
        steps:
          - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
          - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
          - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
          - name: Check out repository code
            uses: actions/checkout@v4
          - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
          - run: echo "🖥️ The workflow is now ready to test your code on the runner."
          - name: List files in the repository
            run: |
              ls ${{ github.workspace }}
          - run: echo "🍏 This job's status is ${{ job.status }}."
    

    At this stage you don't need to understand the details of this workflow. For now, you can just copy and paste the contents into the file. After completing this quickstart guide, you can learn about the syntax of workflow files in 关于工作流程, and for an explanation of GitHub Actions contexts, such as ${{ github.actor }} and ${{ github.event_name }}, see 访问有关工作流运行的上下文信息.

  3. Click Commit changes.

  4. In the "Propose changes" dialog, select either the option to commit to the default branch or the option to create a new branch and start a pull request. Then click Commit changes or Propose changes.

    Screenshot of the "Propose changes" dialog with the areas mentioned highlighted with an orange outline.

Committing the workflow file to a branch in your repository triggers the push event and runs your workflow.

If you chose to start a pull request, you can continue and create the pull request, but this is not necessary for the purposes of this quickstart because the commit has still been made to a branch and will trigger the new workflow.

Viewing your workflow results

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

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

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

  3. In the left sidebar, click the workflow you want to display, in this example "GitHub Actions Demo."

    Screenshot of the "Actions" page. The name of the example workflow, "GitHub Actions Demo", is highlighted by a dark orange outline.

  4. From the list of workflow runs, click the name of the run you want to see, in this example "USERNAME is testing out GitHub Actions."

  5. In the left sidebar of the workflow run page, under Jobs, click the Explore-GitHub-Actions job.

    Screenshot of the "Workflow run" page. In the left sidebar, the "Explore-GitHub-Actions" job is highlighted with a dark orange outline.

  6. The log shows you how each of the steps was processed. Expand any of the steps to view its details.

    Screenshot of steps run by the workflow.

    For example, you can see the list of files in your repository:

    Screenshot of the "List files in the repository" step expanded to show the log output. The output for the step is highlighted with an orange outline.

The example workflow you just added is triggered each time code is pushed to the branch, and shows you how GitHub Actions can work with the contents of your repository. For an in-depth tutorial, see Understanding GitHub Actions.

Next steps

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

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