Search code examples
terraformyamlgithub-actionspipeline

Read value from Environment variables in Github inputs


I am setting up a Terraform pipeline in Github workflow.

Below is the initial set of code:

name: 'Github Workflow'
run-name: 'Terraform Plan-Apply'

on:
   workflow_dispatch:

    inputs:
      Configuration:
        description: 'Deployment Group'
        required: True
        type: choice
        options:
        - Applications
        - KeyVault
        - Synapse
        - Databricks
      Environment:  
        description: 'Environment'
        required: True
        type: choice
        options:
        - Dev
        - QA
        - Prod
      GithubRunner:
            description: 'Environment'
        required: True
        default: ${{ env.Runner }}

Under Settings in Github, I have created three environments Dev, QA and Prod. Each environment has a variable named 'Runner' which holds the runner information. I tried the above code and it is not fetching the details. Output of the above code: Its showing as ${{ env.Runner }} in the Workflow.

Is there a way to read the value from environment variable? like, fetch the value according to the environment selected?


Solution

  • You need to specify the environment you want to use within the job.

    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.

    https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment

    Only then shall the environment variables defined for that environment be available.

    I have fixed your workflow file.

    name: 'Github Workflow'
    run-name: 'Terraform Plan-Apply'
    
    on:
      workflow_dispatch:
        inputs:
          Configuration:
            description: 'Deployment Group'
            required: True
            type: choice
            options:
              - Applications
              - KeyVault
              - Synapse
              - Databricks
          Environment:  
            description: 'Environment'
            required: True
            type: choice
            options:
              - Dev
              - QA
              - Prod
    
    jobs:
      deployment:
        runs-on: ${{ vars.Runner }}
        environment: ${{ inputs.Environment }}