Using environments for deployment

您可以使用保护规则和机密配置环境。 A workflow job that references an environment must follow any protection rules for the environment before running or accessing the environment's secrets.

Environments, environment protection rules, and environment secrets are available in public repositories for all products. For access to environments in private repositories, you must use GitHub Enterprise.

关于环境

Environments are used to describe a general deployment target like production, staging, or development. When a GitHub Actions workflow deploys to an environment, the environment is displayed on the main page of the repository. For more information about viewing deployments to environments, see "Viewing deployment history."

您可以使用保护规则和机密配置环境。 当工作流程引用环境时,作业在环境的所有保护规则通过之前不会开始。 在所有环境保护规则通过之前,作业也不能访问在环境中定义的机密。

环境保护规则

环境保护规则要求通过特定的条件,然后引用环境的作业才能继续。 您可以使用环境保护规则要求手动批准或延迟作业。

需要的审查者

使用所需的审查者要求特定人员或团队批准引用环境的工作流程作业。 您最多可以列出六个用户或团队作为审查者。 审查者必须至少具有对仓库的读取访问权限。 只有一个必需的审查者需要批准该作业才能继续。

有关与必需审查者一起审查引用环境的作业的详细信息,请参阅“审查部署”。

等待计时器

在最初触发作业后,使用等待计时器将作业延迟特定时间。 时间(分钟)必须是 0 至 43,200(30天)之间的整数。

环境机密

存储在环境中的机密仅可用于引用环境的工作流程作业。 如果环境需要批准,作业在所需的审查者批准之前不能访问环境机密。 有关机密的更多信息,请参阅“加密密码”。

注意: 在自托管运行器上运行的工作流程不会在一个孤立的容器中运行,即使它们使用环境。 Environment secrets should be treated with the same level of security as repository and organization secrets. 更多信息请参阅“GitHub Actions 的安全性增强”。

创建环境

要在用户帐户仓库中配置环境,您必须是仓库所有者。 要在组织仓库中配置环境,您必须具有管理员访问权限。

  1. 在 GitHub Enterprise Server 上,导航到仓库的主页面。
  2. 在仓库名称下,单击 Settings(设置)仓库设置按钮
  3. 在左侧边栏中,单击 Environments(环境)
  4. 单击 New environment(新环境)
  5. 为环境输入一个名称, 然后单击 Configure environment(配置环境)。 环境名称不区分大小写。 环境名称不能超过 255 个字符,且必须在仓库中唯一。
  6. Optionally, specify people or teams that must approve workflow jobs that use this environment.
    1. Select Required reviewers.
    2. Enter up to 6 people or teams. 只有一个必需的审查者需要批准该作业才能继续。
    3. Click Save protection rules.
  7. Optionally, specify the amount of time to wait before allowing workflow jobs that use this environment to proceed.
    1. Select Wait timer.
    2. Enter the number of minutes to wait.
    3. Click Save protection rules.
  8. Optionally, specify what branches can deploy to this environment. For more information about the possible values, see "Deployment branches."
    1. Select the desired option in the Deployment branches dropdown.
    2. If you chose Selected branches, enter the branch name patterns that you want to allow.
  9. Optionally, add environment secrets. These secrets are only available to workflow jobs that use the environment. Additionally, workflow jobs that use this environment can only access these secrets after any configured rules (for example, required reviewers) pass. 有关机密的更多信息,请参阅“加密密码”。
    1. Under Environment secrets, click Add Secret.
    2. Enter the secret name.
    3. Enter the secret value.
    4. 单击 Add secret(添加密码)

运行引用不存在的环境的工作流程将使用引用的名称创建环境。 新创建的环境将不配置任何保护规则或机密。 可在仓库中编辑工作流程的任何人都可以通过工作流程文件创建环境,但只有仓库管理员才能配置环境。

Using an environment

工作流程中的每个作业都可以引用单个环境。 在将引用环境的作业发送到运行器之前,必须通过为环境配置的任何保护规则。 The job can access the environment's secrets only after the job is sent to a runner.

当工作流程引用环境时,环境将显示在仓库的部署中。 有关查看当前和以前的部署的详细信息,请参阅“查看部署历史记录”。

You can specify an environment for each job in your workflow. To do so, add a jobs.<job_id>.environment key followed by the name of the environment.

For example, this workflow will use an environment called production.

name: Deployment

on:
  push:
    branches:
      - main

jobs:
  deployment:
    runs-on: ubuntu-latest
    environment: production
    steps:
      - name: deploy
        # ...deployment-specific steps

When the above workflow runs, the deployment job will be subject to any rules configured for the production environment. For example, if the environment requires reviewers, the job will pause until one of the reviewers approves the job.

You can also specify a URL for the environment. The specified URL will appear on the deployments page for the repository (accessed by clicking Environments on the home page of your repository) and in the visualization graph for the workflow run. If a pull request triggered the workflow, the URL is also displayed as a View deployment button in the pull request timeline.

name: Deployment

on:
  push:
    branches:
      - main

jobs:
  deployment:
    runs-on: ubuntu-latest
    environment: 
      name: production
      url: https://github.com
    steps:
      - name: deploy
        # ...deployment-specific steps

Workflow graph with URL

删除环境

要在用户帐户仓库中配置环境,您必须是仓库所有者。 要在组织仓库中配置环境,您必须具有管理员访问权限。

删除环境将删除与环境关联的所有机密和保护规则。 由于已删除环境的保护规则而正在等待的任何作业将自动失败。

  1. 在 GitHub Enterprise Server 上,导航到仓库的主页面。
  2. 在仓库名称下,单击 Settings(设置)仓库设置按钮
  3. 在左侧边栏中,单击 Environments(环境)
  4. 在要删除的环境旁边,单击
  5. 单击 I understand, delete this environment(我了解,删除此环境)

How environments relate to deployments

When a workflow job that references an environment runs, it creates a deployment object with the environment property set to the name of your environment. As the workflow progresses, it also creates deployment status objects with the environment property set to the name of your environment, the environment_url property set to the URL for environment (if specified in the workflow), and the state property set to the status of the job.

You can access these objects through the REST API or GraphQL API. You can also subscribe to these webhook events. For more information, see "Repositories" (REST API), "Objects" (GraphQL API), or "Webhook events and payloads."

后续步骤

GitHub Actions provides several features for managing your deployments. For more information, see "Deploying with GitHub Actions."

此文档对您有帮助吗?

隐私政策

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

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

做出贡献

或, 了解如何参与。