'Github Workflow: How to map input option to value

I have the following input set up for my Github Workflow

on:
  workflow_dispatch:
    inputs:
      environment:
        type: choice
        description: Environment
        options:
          - Development
          - QA
          - UAT
          - Production

If the user picks Development as the environment, I want to use the word dev in some of my string concatenations, if they pick Production I want to use prod for the same purpose.

Here's an example of what the areas look like where substitution needs to occur.

- name: Package App
  run: |
    move-file .env.$TARGET_ENVIRONMET .env
    yarn package:$TARGET_ENVIRONMENT

In this example I need the move-file command to be move-file .env.dev .env and the yarn package command to be yarn package:dev for Development but I'm having trouble mapping Development to dev. I looked at setting a variable to dev if the user picked Development but it looks like the only variable support is for setting environment variables and that doesn't appear to be possible conditionally.

So I can't do something like this

env:
  if: inputs.environment == 'Development'
    TARGET_ENVIRONMENT: 'dev'
  if: inputs.environment == 'Production'
    TARGET_ENVIRONMENT: 'prod'


Solution 1:[1]

There are some ways to map your input to env variable - you can either use an action that does just that:

- uses: kanga333/variable-mapper@master
  id: export
  with:
    key: "${{ github.event.inputs.environment }}"
    map: |
      {
        "Development": {
          "environment": "dev"
        },
        "Production": {
          "environment": "prod"
        }
      }
    export_to: env
- run: |
    move-file .env.$environment .env

or you can use kind of hack and define your env (on job level) like this:

env:
  TARGET_ENVIRONMET: >
    ${{ fromJson('{
      "Development": "dev",
      "Production": "prod"
    }')[github.event.inputs.environment] }}
steps:
  - run: |
      move-file .env.$TARGET_ENVIRONMET .env

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ondřej Tůma