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

GitHub Actions 的基本功能

GitHub Actions 旨在帮助您建立强大而动态的自动化。 本指南说明如何创建包括环境变量、定制化脚本等的 GitHub Actions 工作流程。

本文内容

此文档对您有帮助吗?

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

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

做出贡献

或, 了解如何参与。

概览

GitHub Actions 允许您自定义工作流程,以满足应用程序和团队的独特需求。 在本指南中,我们将讨论一些基本的自定义技术,例如使用变量、运行脚本以及在作业之间共享数据和构件。

在工作流程中使用变量

GitHub Actions 包含每个工作流程运行的默认环境变量。 如果您需要使用自定义环境变量,可以在 YAML 工作流程文件中设置这些变量。 此示例演示如何创建名为 POSTGRES_HOST 和 POSTGRES_PORT 的自定义变量。 然后,这些变量可供 node client.js 脚本使用。

jobs:
  example-job:
      steps:
        - name: Connect to PostgreSQL
          run: node client.js
          env:
            POSTGRES_HOST: postgres
            POSTGRES_PORT: 5432

更多信息请参阅“使用环境变量”。

添加脚本到工作流程

您可以使用操作来运行脚本和 shell 命令,然后在指定的运行器上执行。 此示例演示操作如何使用 run 关键字在运行器上执行 npm install -g bats。

jobs:
  example-job:
    steps:
      - run: npm install -g bats

例如,要将脚本作为操作运行,您可以将脚本存储在您的仓库中并提供路径和 shell 类型。

jobs:
  example-job:
    steps:
      - name: Run build script
        run: ./.github/scripts/build.sh
        shell: bash

更多信息请参阅“GitHub Actions 的工作流程语法”。

在作业之间共享数据

If your job generates files that you want to share with another job in the same workflow, or if you want to save the files for later reference, you can store them in GitHub as artifacts. 构件是创建并测试代码时所创建的文件。 例如,构件可能包含二进制或包文件、测试结果、屏幕截图或日志文件。 Artifacts are associated with the workflow run where they were created and can be used by another job.

For example, you can create a file and then upload it as an artifact.

jobs:
  example-job:
    name: Save output
    steps:
      - shell: bash
        run: |
          expr 1 + 1 > output.log
      - name: Upload output file
        uses: actions/upload-artifact@v1
        with:
          name: output-log-file
          path: output.log

To download an artifact from a separate workflow run, you can use the actions/download-artifact action. For example, you can download the artifact named output-log-file.

jobs:
  example-job:
    steps:
      - name: Download a single artifact
        uses: actions/download-artifact@v2
        with:
          name: output-log-file

For more information about artifacts, see "Persisting workflow data using artifacts."

后续步骤

To continue learning about GitHub Actions, see "Managing complex workflows."

此文档对您有帮助吗?

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

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

做出贡献

或, 了解如何参与。