Skip to main content

Using environments for deployment

Specify a deployment environment in your workflow.

About environments

环境用于描述常规部署目标,例如 productionstagingdevelopment。 当 GitHub Actions 工作流部署到某个环境时,该环境将显示在存储库的主页上。 可以使用环境来要求批准作业才能继续,限制哪些分支可以触发工作流 ,使用自定义部署保护规则 控制部署,或限制对机密的访问。 有关创建环境的详细信息,请参阅“Managing environments for deployment”。

Each job in a workflow can reference a single environment. Any protection rules configured for the environment must pass before a job referencing the environment is sent to a runner. The job can access the environment's secrets only after the job is sent to a runner.

When a workflow references an environment, the environment will appear in the repository's deployments. For more information about viewing current and previous deployments, see "查看部署历史记录."

Using an environment in a workflow

可以为工作流中的每个作业指定环境。 为此,请添加一个 jobs.<job_id>.environment 键,后跟环境的名称。

例如,此工作流将使用名为 production 的环境。

name: Deployment

on:
  push:
    branches:
      - main

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

当上述工作流运行时,deployment 作业将受制于为 production 环境配置的任何规则。 例如,如果环境需要审阅者,该作业将在其中一个审阅者批准之前暂停。

还可以为环境指定 URL。 指定的 URL 将显示在存储库的部署页(通过单击存储库主页上的“环境”进行访问)和工作流运行的可视化图中。 如果拉取请求触发了工作流,则 URL 还会在拉取请求时间线中显示为“查看部署”按钮。

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