skip to Main Content

I would like to access the deployment environment name in my Github Actions workflow YAML. Here is an example:

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

2

Answers


  1. As jobs context is only available in reusable workflows one of working option could be use env:

    name: question-74910046
    # https://stackoverflow.com/questions/74910046/how-do-i-reference-the-deployment-environment-name-in-github-actions-workflow-ya
    
    on:
      workflow_dispatch:
      
    env:
      env_name: production-74910046
      env_url: https://stackoverflow.com/questions/74910046/how-do-i-reference-the-deployment-environment-name-in-github-actions-workflow-ya
    
    jobs:
      test:
        runs-on: ubuntu-latest
    
        environment: 
          name: $env_name
          url: $env_url
        
        steps:
          - name: context information
            run: echo '${{ toJSON(env) }}'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search