如何在 Github Actions 工作流程 YAML 中引用部署环境名称?

Joo*_*wan 7 github github-enterprise github-actions

我想访问我的 Github Actions 工作流程 YAML 中的部署环境名称。这是一个例子:

jobs:
  example:
    runs-on: ubuntu-latest
    environment: "foobarbaz"
    ...

    steps:
      ...
      - name: Terraform Check Format
        id: terraform-fmt
        # what should I use here instead of "????" to get "foobarbaz"?
        run: terraform -chdir=terraform/stacks/${{ ???? }}/${{ matrix.stack }} fmt -check
Run Code Online (Sandbox Code Playgroud)

BLu*_*LuM 0

由于jobs上下文仅在可重用工作流程中可用,因此可以使用工作选项之一env

name: question-74910046
# /sf/ask/5243703251/

on:
  workflow_dispatch:
  
env:
  env_name: production-74910046
  env_url: /sf/ask/5243703251/

jobs:
  test:
    runs-on: ubuntu-latest

    environment: 
      name: $env_name
      url: $env_url
    
    steps:
      - name: context information
        run: echo '${{ toJSON(env) }}'
Run Code Online (Sandbox Code Playgroud)