GitHub Actions: Map Key/Values Based on Input

Let’s say we have an input in our GitHub Actions workflow for the environment like the one below.

on:
  workflow_dispatch:
    inputs:
      environment:
        description: Environment
        type: choice
        options:
        - qa
        - uat

Based on the environments we selected, we need to pass the below three key/values to a job, let’s call it the Deploy job. Three values are:

account_id
region
parameter_name

GitHub Actions doesn’t have a native feature for value mapping, so we will do a simple bash script that’s executed before the deploy job.

jobs:
  map-envs:
    runs-on: ubuntu-latest
    steps:
    - id: map
      run: |
        case "${{ inputs.environment }}" in
          qa)
            echo "account_id=111111111111" >> $GITHUB_OUTPUT
            echo "region=us-east-1" >> $GITHUB_OUTPUT
            echo "parameter_name=/Foo/Bar" >> $GITHUB_OUTPUT
            ;;
          uat)
            echo "account_id=222222222222" >> $GITHUB_OUTPUT
            echo "region=us-east-2" >> $GITHUB_OUTPUT
            echo "parameter_name=/James/Bond" >> $GITHUB_OUTPUT
            ;;
        esac
    outputs:
      account_id: ${{ steps.map.outputs.account_id }}
      region: ${{ steps.map.outputs.region }}
      parameter_name: ${{ steps.map.outputs.parameter_name }}

As you can see above, first we set the key=value based on the input and add them to $GITHUB_OUTPUT

And then we output those values using the output parameter so we can reference them from other jobs. Let’s see how we can utilize these mapped values. As the final form, we have below complete workflow file:

name: Example map values

on:
  workflow_dispatch:
    inputs:
      environment:
        description: Environment
        type: choice
        options:
        - qa
        - uat
        
jobs:
  map-envs:
    runs-on: ubuntu-latest
    steps:
    - id: map
      run: |
        case "${{ inputs.environment }}" in
          qa)
            echo "account_id=111111111111" >> $GITHUB_OUTPUT
            echo "region=us-east-1" >> $GITHUB_OUTPUT
            echo "parameter_name=/Foo/Bar" >> $GITHUB_OUTPUT
            ;;
          uat)
            echo "account_id=222222222222" >> $GITHUB_OUTPUT
            echo "region=us-east-2" >> $GITHUB_OUTPUT
            echo "parameter_name=/James/Bond" >> $GITHUB_OUTPUT
            ;;
        esac
    outputs:
      account_id: ${{ steps.map.outputs.account_id }}
      region: ${{ steps.map.outputs.region }}
      parameter_name: ${{ steps.map.outputs.parameter_name }}
      
  deploy:
    name: "Deploy"
    needs: map-envs
    steps:
    - run: |
      echo "Deploying in ${{ needs.map-envs.outputs.account_id }} account, in ${{ needs.map-envs.outputs.region }} region, using ${{ needs.map-envs.outputs.parameter_name }} parameter"

In the deploy job, we define the dependency with needs and call the mapped values from the map-envs job.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *