Home > Mobile >  How to pass custom environment variables related to specific environment in Github Actions
How to pass custom environment variables related to specific environment in Github Actions

Time:08-10

I would like to use custom environment variables related to specific environment based on environment selection from dropdown during deployment. How to get the custom variables related to specific environment from environment variable section. Any suggestions on how this can be achieved.

name: Adios CD pipeline.
on:
  workflow_dispatch:
    inputs:
      ENVIRONMENT:
        type: choice
        description: 'Select the Environment to deploy'
        required: true
        options:
          - dev
          - qa
          - uat
          - load
          - prod
        default: 'dev'
env:
  Dev:
    AWS_REGION: "us-east-1"
    STAGE: "${{ github.event.inputs.ENVIRONMENT }}"
    SYSTEM: "ADIOSAPP"
  QA:
    AWS_REGION: "us-east-1"
    STAGE: "${{ github.event.inputs.ENVIRONMENT }}"
    SYSTEM: "ADIOSAPP"

jobs:
  build:
    name: "Deploying ${{ github.ref_name }} branch to ${{ github.event.inputs.ENVIRONMENT }} environment"
    runs-on: ubuntu-latest
    environment: ${{ github.event.inputs.ENVIRONMENT }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-region: ${{ env.AWS_REGION }}

CodePudding user response:

As you already know mapping is not allowed at this place, byt you could use fromJson to mimic this:

on:
  workflow_dispatch:
    inputs:
      ENVIRONMENT:
        type: choice
        description: 'Select the Environment to deploy'
        required: true
        options:
          - dev
          - qa
          - uat
          - load
          - prod
        default: 'dev'
env:
    AWS_REGION: ${{ fromJSON('{"dev":"us-east-1","qa":"us-east-1"}')[github.event.inputs.ENVIRONMENT] }}
    STAGE: "${{ github.event.inputs.ENVIRONMENT }}"
    SYSTEM: ${{ fromJSON('{"dev":"ADIOSAPP","qa":"ADIOSAPP"}')[github.event.inputs.ENVIRONMENT] }}

jobs:
  build:
    name: "Deploying ${{ github.ref_name }} branch to ${{ github.event.inputs.ENVIRONMENT }} environment"
    runs-on: ubuntu-latest
    #environment: ${{ github.event.inputs.ENVIRONMENT }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Print env
        run: echo "${{ env.AWS_REGION }}"
  • Related