# GitHub Actions快速入门

几分钟内体验 GitHub Actions 的核心功能。

## 简介

GitHub Actions 是一种持续集成和持续交付 (CI/CD) 平台，可用于自动执行生成、测试和部署管道。  你可以创建工作流，以便在推送更改到存储库时运行测试，或将合并的拉取请求部署到生产环境。

本快速入门指南将介绍如何使用 GitHub 的用户界面添加工作流，以便演示 GitHub Actions 的某些基本功能。

若要开始使用预配置的工作流，请浏览 [actions/starter-workflows](https://github.com/actions/starter-workflows) 存储库中的模板列表。 有关详细信息，请参阅“[使用工作流模板](/zh/actions/how-tos/write-workflows/use-workflow-templates)”。

有关 GitHub Actions 工作流的概述，请参阅“[工作流](/zh/actions/using-workflows/about-workflows)”。 如果要了解构成 GitHub Actions 的各种组件，请参阅“[了解GitHub Actions](/zh/actions/learn-github-actions/understanding-github-actions)”。

## 使用工作流模板

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

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

* CI：[持续集成工作流](https://github.com/actions/starter-workflows/tree/main/ci)
* 部署：[部署工作流](https://github.com/actions/starter-workflows/tree/main/deployments)
* 自动化：[自动化工作流](https://github.com/actions/starter-workflows/tree/main/automation)
* 代码扫描：[代码扫描工作流](https://github.com/actions/starter-workflows/tree/main/code-scanning)
* 页面：[页面工作流](https://github.com/actions/starter-workflows/tree/main/pages)

使用这些工作流作为构建自定义工作流的起点或按原样使用它们。 可以在 [actions/starter-workflows](https://github.com/actions/starter-workflows) 存储库中浏览工作流模板的完整列表。

## 先决条件

本指南假定：

* 你至少有关于如何使用 GitHub 的基本知识。 如果没有，你将发现先阅读存储库和拉取请求文档中的一些文章会非常有帮助。 有关示例，请参阅“[仓库快速入门](/zh/repositories/creating-and-managing-repositories/quickstart-for-repositories)”、“[Branches](/zh/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-branches)”和“[拉取请求](/zh/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests)”。
* 你在 GitHub 上有一个存储库，可在其中添加文件。
* 你有 GitHub Actions 的访问权限

  > \[!NOTE] 如果 <svg version="1.1" width="16" height="16" viewBox="0 0 16 16" class="octicon octicon-play" aria-label="play" role="img"><path d="M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0ZM1.5 8a6.5 6.5 0 1 0 13 0 6.5 6.5 0 0 0-13 0Zm4.879-2.773 4.264 2.559a.25.25 0 0 1 0 .428l-4.264 2.559A.25.25 0 0 1 6 10.559V5.442a.25.25 0 0 1 .379-.215Z"></path></svg>“Actions”选项卡未显示在GitHub 上的仓库名称下，则可能是因为对仓库禁用了 Actions\*\*\*\*。 有关详细信息，请参阅“[管理存储库的GitHub Actions设置](/zh/repositories/managing-your-repositorys-settings-and-features/enabling-features-for-your-repository/managing-github-actions-settings-for-a-repository)”。

## 创建第一个工作流程

1. 在 GitHub 上的存储库中，创建 `github-actions-demo.yml` 目录中名为 `.github/workflows` 的工作流文件。 要执行此操作：
   * 如果 `.github/workflows` 目录已存在，请导航到 GitHub 上的该目录，单击“添加文件”****，然后单击“创建新文件”****，然后将命名文件 `github-actions-demo.yml`。

   * 如果存储库没有 `.github/workflows` 目录，请转到 GitHub 上的存储库主页，单击“添加文件”****，然后单击“创建新文件”****，并命名文件 `.github/workflows/github-actions-demo.yml`。 这会在单个步骤中创建 `.github` 和 `workflows` 目录以及 `github-actions-demo.yml` 文件。
   > \[!NOTE]
   > 若要使 GitHub 在存储库中发现任何 GitHub Actions 工作流，必须将工作流文件保存在名为 `.github/workflows` 的目录中。
   >
   > 你可以为工作流文件指定所需的任何名称，但必须使用 `.yml` 或 `.yaml` 作为文件扩展名。 YAML 通常是用于配置文件的标记语言。

2. 将以下 YAML 内容复制到 `github-actions-demo.yml` 文件中：

   ```yaml copy
   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@v6
         - 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 }}."
   ```

   在此阶段，你无需了解此工作流的详细信息。 现在，你只需将内容复制并粘贴到文件中。 完成本快速入门指南后，可以了解“[工作流](/zh/actions/using-workflows/about-workflows#understanding-the-workflow-file)”中工作流文件的语法；有关 GitHub Actions 上下文（例如 `${{ github.actor }}` 和 `${{ github.event_name }}`）的说明，请参阅“[上下文参考](/zh/actions/learn-github-actions/contexts)”。

3. 单击“提交更改”。

4. 在“建议更改”对话框中，选择提交到默认分支的选项或创建新分支并启动拉取请求的选项。 然后单击“提交更改”\*\*\*\* 或“建议更改”\*\*\*\*。

   ![“建议更改”对话框的屏幕截图，其中提到的区域用橙色轮廓突出显示。](/assets/images/help/repository/actions-quickstart-commit-new-file.png)

向存储库的分支提交工作流文件会触发 `push` 事件并运行工作流。

如果选择启动拉取请求，则可以继续并创建拉取请求，但对于本快速入门来说，这不是必要的，因为提交仍然是对分支进行的，并会触发新工作流。

## 查看工作流程结果

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

2. 在仓库名称下，单击“<svg version="1.1" width="16" height="16" viewBox="0 0 16 16" class="octicon octicon-play" aria-label="play" role="img"><path d="M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0ZM1.5 8a6.5 6.5 0 1 0 13 0 6.5 6.5 0 0 0-13 0Zm4.879-2.773 4.264 2.559a.25.25 0 0 1 0 .428l-4.264 2.559A.25.25 0 0 1 6 10.559V5.442a.25.25 0 0 1 .379-.215Z"></path></svg> Actions”\*\*\*\*。

   ![“github/docs”存储库的选项卡的屏幕截图。 “操作”选项卡以橙色边框突出显示。](/assets/images/help/repository/actions-tab-global-nav-update.png)

3. 在左侧栏中，单击要显示的工作流，在本示例中为“GitHub Actions演示”。

   ![“Actions”页面的屏幕截图。 示例工作流名称“GitHub Actions 演示”被深橙色轮廓突出显示。](/assets/images/help/repository/actions-quickstart-workflow-sidebar.png)

4. 在工作流运行列表中，单击要查看的运行名称，在本示例中，“USERNAME 正在测试GitHub Actions”。

5. 在工作流运行页的左侧栏中，在 **Jobs** 下，单击 **Explore-GitHub-Actions** 作业。

   ![“工作流运行”页的屏幕截图。 在左侧边栏中，“Explore-GitHub-Actions”作业以深橙色边框突出显示。](/assets/images/help/repository/actions-quickstart-job.png)

6. 日志显示每个步骤的处理方式。 展开任何步骤之一以查看其详细信息。

   ![工作流运行步骤的屏幕截图。](/assets/images/help/repository/actions-quickstart-logs.png)

   例如，你可以看到存储库中的文件列表：

   ![展开以显示日志输出的“列出存储库中的文件”步骤的屏幕截图。 步骤的输出以橙色轮廓突出显示。](/assets/images/help/repository/actions-quickstart-log-detail.png)

每次将代码推送到分支时，刚刚添加的示例工作流都会运行，并显示 GitHub Actions 如何处理存储库的内容。 有关深入教程，请参阅“[了解GitHub Actions](/zh/actions/learn-github-actions/understanding-github-actions)”。

## 后续步骤

GitHub Actions 可帮助你自动执行应用程序开发过程的几乎所有方面。 准备好开始了吗？ 以下是一些有助于你借助 GitHub Actions 继续下一步的资源：

* 若要创建 GitHub Actions 工作流，请参阅 [使用工作流模板](/zh/actions/how-tos/write-workflows/use-workflow-templates)。
* 有关持续集成 (CI) 工作流，请参阅 [构建和测试代码](/zh/actions/tutorials/build-and-test-code)。
* 有关构建和发布包的信息，请参阅 [发布包](/zh/actions/tutorials/publish-packages)。
* 有关部署项目，请参阅 [部署到第三方平台](/zh/actions/how-tos/deploy/deploy-to-third-party-platforms)。
* 有关在 GitHub 上自动执行任务和流程的信息，请参阅 [使用 GitHub Actions 管理你的工作](/zh/actions/tutorials/manage-your-work)。
* 有关演示 GitHub Actions 更复杂功能的示例，请参阅 [选择工作流执行的操作](/zh/actions/how-tos/write-workflows/choose-what-workflows-do)。 这些详细示例说明如何在运行程序上测试代码、访问 GitHub CLI 并使用高级功能（如并发和测试矩阵）。
* 要证明你在使用 GitHub Actions 自动化工作流和加速开发方面的熟练程度，请通过 GitHub Actions 获取 GitHub Certifications 证书。 有关详细信息，请参阅 [关于 GitHub Certifications](/zh/get-started/showcase-your-expertise-with-github-certifications/about-github-certifications)。