github-actions

Add multiple variables for one selectable option


I want to create a single manual trigger pipeline for multiple environments for example dev,qa and prod. In my pipeline for each of these environments I have 3-4 variables which are dependant on the environment choice. I have the idea to set these variables as an environment variables in a separate step of the pipeline so they get set correctly based on the initial input. However I'm not sure how optimal this is and I want to ask is there a different sollution to my problem. Under my question I've provided my initial idea:

on: 
  workflow_dispatch:
    inputs:
      environment:
        description: 'Environment to run tests against'
        required: true
        default: 'stage'
        type: choice
        options:
          - dev
          - stage
          - prod

permissions:
  contents: write
  pull-requests: write
  security-events: write
  
jobs:
  test_auth_fn:
    runs-on: ...
    permissions:
      contents: read
      id-token: write
    steps:
      - name: Set parameters based on input
        run: |
          if [ "${{ inputs.environment }}" == "dev" ]; then
            echo "Setting parameters for DEV"
            export PARAM_1=dev_value
            export PARAM_2=dev_specific_value
          elif [ "${{ inputs.environment }}" == "stage" ]; then
            echo "Setting parameters for PROD"
            export PARAM_1=prod_value
            export PARAM_2=prod_specific_value
          else
            echo "Setting parameters for STAGING"
            export PARAM_1=staging_value
            export PARAM_2=staging_specific_value
          fi   
      - name: Use parameters
          run: |
            echo "PARAM_1 is ${{ env.PARAM_1 }}"
            echo "PARAM_2 is ${{ env.PARAM_2 }}"

Solution

  • You don't need to create a dedicated step for that, GitHub Actions support it natively in the env section, in this way:

    env:
      PARAM_1: >-    
        ${{ inputs.environment == 'dev' && 'dev_value'
        || inputs.environment == 'stage' && 'staging_value'
        || inputs.environment == 'prod' && 'prod_value'
        }}
      PARAM_2: >-    
        ${{ inputs.environment == 'dev' && 'dev_specific_value'
        || inputs.environment == 'stage' && 'staging_specific_value'
        || inputs.environment == 'prod' && 'prod_specific_value'
        }}