title: GitHub Actions for multiple environment variable setup with Automation
body: <p>I am working with GitHub to deploy a container-based application on multiple environments, I have two environments:</p>
  <ol>
  <li><p>Dev</p>
  </li>
  <li><p>Prod</p>
  </li>
  </ol>
  <p>I am building the application on both Environments, this is my yml file:</p>
  <pre><code>name: 'Manual - Build &amp; Deploy - Enterprise'
  
  on:
    push:
      branches-ignore:
        - '**'
  
    workflow_dispatch:
      inputs:
        git-ref:
          description: Git Ref (Optional)
          default: develop
          required: false
  
        account:
          description: slb-dev, slb-prod
          default: slb-dev
          required: true
          
        account_prod:
         description: slb-dev, slb-prod
         default: slb-prod
         required: true
  
        environment:
          description: development (main, int, qs), production (v1_demo, v1_rosecity, demo)
          default: main
          required: false
  
        microservice:
          description: chroma, liquid, tenant, dashboard, lims, lims-simulator, client, logging, metrc
          default: chroma
          required: false
  
        builddir:
          description: MicroChromatographyService/MicroChromatographyService, MicroLiquidHandlingService/MicroLiquidHandlingService, MicroTenantService/MicroTenantService, MicroDashboardService/MicroDashboardService, LIMSIntegrationService/LIMSIntegrationService, LIMSSimulatorService/LIMSSimulatorService, IntegrationHubClientService/IntegrationHubClientService, PerkinElmer.LoggingService/PerkinElmer.LoggingService, MetRCReportService/MetRCReportService
          default: MicroChromatographyService/MicroChromatographyService
          required: false
  
  jobs:
    setup:
      name: Setup ENV Variables
      runs-on: ubuntu-latest
      environment:
       name: Production
       url: https://dev.test.com
      steps:
  
      - name: Set Vars
        id: setvars
        run: |
            echo &quot;::set-output name=APP_NAME::${{ github.event.inputs.microservice }}&quot;
            echo &quot;::set-output name=AWS_REGION::us-east-1&quot;
            echo &quot;::set-output name=SHA8::${{ github.sha }} | cut -c1-8)&quot;
            echo &quot;::set-output name=BUILD_DIR::${{ github.event.inputs.builddir }}&quot;
            echo &quot;::set-output name=ECR_REPOSITORY::${{ github.event.inputs.account }}-${{ github.event.inputs.environment }}-${{ github.event.inputs.microservice }}&quot;
            echo &quot;::set-output name=ECS_CLUSTER::${{ github.event.inputs.account }}-${{ github.event.inputs.environment }}&quot;
            echo &quot;::set-output name=ECS_SERVICE::${{ github.event.inputs.account }}-${{ github.event.inputs.environment }}-${{ github.event.inputs.microservice }}&quot;
            echo &quot;::set-output name=ECS_TASK_DEFINITION::${{ github.event.inputs.account }}-${{ github.event.inputs.environment }}-${{ github.event.inputs.microservice }}&quot;
            echo &quot;::set-output name=ECS_TASK_DEFINITION_FILE::task-definition-${{ github.event.inputs.microservice }}.json&quot;
            echo &quot;::set-output name=ECS_CONTAINER_NAME::${{ github.event.inputs.account }}-${{ github.event.inputs.environment }}-${{ github.event.inputs.microservice }}&quot;
  
      outputs:
        APP_NAME: ${{ steps.setvars.outputs.APP_NAME }}
        AWS_REGION: ${{ steps.setvars.outputs.AWS_REGION }}
        SHA8: ${{ steps.setvars.outputs.SHA8 }}
        BUILD_DIR: ${{ steps.setvars.outputs.BUILD_DIR }}
        ECR_REPOSITORY: ${{ steps.setvars.outputs.ECR_REPOSITORY }}
        ECS_CLUSTER: ${{ steps.setvars.outputs.ECS_CLUSTER }}
        ECS_SERVICE: ${{ steps.setvars.outputs.ECS_SERVICE }}
        ECS_TASK_DEFINITION: ${{ steps.setvars.outputs.ECS_TASK_DEFINITION }}
        ECS_TASK_DEFINITION_FILE: ${{ steps.setvars.outputs.ECS_TASK_DEFINITION_FILE }}
        ECS_CONTAINER_NAME: ${{ steps.setvars.outputs.ECS_CONTAINER_NAME }}
        
    setup2:
      name: Setup prod ENV Variables
      runs-on: ubuntu-latest
      environment:
        name: dev
        url: https://production.test.com
      steps:
        
      - name: Set Vars
        id: setprodvars
        run: |
            echo &quot;::set-output name=APP_NAME::${{ github.event.inputs.microservice }}&quot;
            echo &quot;::set-output name=AWS_REGION::us-east-1&quot;
            echo &quot;::set-output name=SHA8::${{ github.sha }} | cut -c1-8)&quot;
            echo &quot;::set-output name=BUILD_DIR::${{ github.event.inputs.builddir }}&quot;
            echo &quot;::set-output name=ECR_REPOSITORY::${{ github.event.inputs.account_prod }}-${{ github.event.inputs.environment }}-${{ github.event.inputs.microservice }}&quot;
            echo &quot;::set-output name=ECS_CLUSTER::${{ github.event.inputs.account_prod }}-${{ github.event.inputs.environment }}&quot;
            echo &quot;::set-output name=ECS_SERVICE::${{ github.event.inputs.account_prod }}-${{ github.event.inputs.environment }}-${{ github.event.inputs.microservice }}&quot;
            echo &quot;::set-output name=ECS_TASK_DEFINITION::${{ github.event.inputs.account_prod }}-${{ github.event.inputs.environment }}-${{ github.event.inputs.microservice }}&quot;
            echo &quot;::set-output name=ECS_TASK_DEFINITION_FILE::task-definition-${{ github.event.inputs.microservice }}.json&quot;
            echo &quot;::set-output name=ECS_CONTAINER_NAME::${{ github.event.inputs.account_prod }}-${{ github.event.inputs.environment }}-${{ github.event.inputs.microservice }}&quot;
      outputs:
        APP_NAME: ${{ steps.setvars.outputs.APP_NAME }}
        AWS_REGION: ${{ steps.setvars.outputs.AWS_REGION }}
        SHA8: ${{ steps.setvars.outputs.SHA8 }}
        BUILD_DIR: ${{ steps.setvars.outputs.BUILD_DIR }}
        ECR_REPOSITORY: ${{ steps.setvars.outputs.ECR_REPOSITORY }}
        ECS_CLUSTER: ${{ steps.setvars.outputs.ECS_CLUSTER }}
        ECS_SERVICE: ${{ steps.setvars.outputs.ECS_SERVICE }}
        ECS_TASK_DEFINITION: ${{ steps.setvars.outputs.ECS_TASK_DEFINITION }}
        ECS_TASK_DEFINITION_FILE: ${{ steps.setvars.outputs.ECS_TASK_DEFINITION_FILE }}
        ECS_CONTAINER_NAME: ${{ steps.setvars.outputs.ECS_CONTAINER_NAME }}
  
  
        
    DeployDev:
      name: Deploy to Dev 
      needs: setup
      runs-on: ubuntu-latest
      permissions:
       packages: write
       contents: write
       id-token: write
      environment: 
        name: Dev
        url: 'http://dev.myapp.com'
      steps:
      - name: Set Environments
        run: |
          if [[ &quot;${{github.event.inputs.account}}&quot; == &quot;slb-dev&quot; ]]; then
            echo &quot;AWS_ACCESS_KEY_ID=${{ env.AWS_ACCESS_KEY_ID }}&quot; &gt;&gt; $GITHUB_ENV
            echo &quot;AWS_SECRET_ACCESS_KEY=${{ env.AWS_SECRET_ACCESS_KEY }}&quot; &gt;&gt; $GITHUB_ENV
          fi
  
          if [[ &quot;${{github.event.inputs.account}}&quot; == &quot;slb-prod&quot; ]]; then
            echo &quot;AWS_ACCESS_KEY_ID=${{ env.AWS_ACCESS_KEY_ID }}&quot; &gt;&gt; $GITHUB_ENV
            echo &quot;AWS_SECRET_ACCESS_KEY=${{ env.AWS_SECRET_ACCESS_KEY }}&quot; &gt;&gt; $GITHUB_ENV
          fi
  
      - name: Clone Repository (Current branch)
        uses: actions/checkout@v2
        if: github.event.inputs.git-ref == ''
  
      - name: Clone Repository (Custom Ref)
        uses: actions/checkout@v2
        if: github.event.inputs.git-ref != ''
        with:
          ref: ${{ github.event.inputs.git-ref }}
  
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}         
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} 
          aws-region: ${{ needs.setup.outputs.AWS_REGION }}
  
      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1
  
      - name: Build, tag, and push image to Amazon ECR
        id: build-image
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          ECR_REPOSITORY: ${{ needs.setup.outputs.ECR_REPOSITORY }}
          IMAGE_TAG: ${{ github.sha }}
        run: |
          cd ${{ needs.setup.outputs.BUILD_DIR }}
          docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t $ECR_REGISTRY/$ECR_REPOSITORY:latest .
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
          echo &quot;::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG&quot;
  
      - name: Download task definition
        run: |
          aws ecs describe-task-definition --task-definition ${{ needs.setup.outputs.ECS_TASK_DEFINITION }} --query taskDefinition &gt; ${{ needs.setup.outputs.ECS_TASK_DEFINITION_FILE }}
  
      - name: Fill in the new image ID in the Amazon ECS task definition
        id: task-def
        uses: aws-actions/amazon-ecs-render-task-definition@v1
        with:
          task-definition: ${{ needs.setup.outputs.ECS_TASK_DEFINITION_FILE }}
          container-name: ${{ needs.setup.outputs.ECS_CONTAINER_NAME }}
          image: ${{ steps.build-image.outputs.image }}
  
      - name: Deploy Amazon ECS task definition
        uses: aws-actions/amazon-ecs-deploy-task-definition@v1
        with:
          task-definition: ${{ steps.task-def.outputs.task-definition }}
          service: ${{ needs.setup.outputs.ECS_SERVICE }}
          cluster: ${{ needs.setup.outputs.ECS_CLUSTER }}
          wait-for-service-stability: true
  
  
    DeployProd:
      name: Deploy to Production 
      needs: [setup2, DeployDev]
      runs-on: ubuntu-latest
      permissions:
       packages: write
       contents: write
       id-token: write
      environment: 
        name: Production
        url: 'http://dev.myapp.com'
      steps:
      - name: Set Environments
        run: |
          if [[ &quot;${{github.event.inputs.account_prod}}&quot; == &quot;slb-dev&quot; ]]; then
            echo &quot;AWS_ACCESS_KEY_ID=${{ env.AWS_ACCESS_KEY_ID }}&quot; &gt;&gt; $GITHUB_ENV
            echo &quot;AWS_SECRET_ACCESS_KEY=${{ env.AWS_SECRET_ACCESS_KEY }}&quot; &gt;&gt; $GITHUB_ENV
          fi
          if [[ &quot;${{github.event.inputs.account_prod}}&quot; == &quot;slb-prod&quot; ]]; then
            echo &quot;AWS_ACCESS_KEY_ID=${{ env.AWS_ACCESS_KEY_ID }}&quot; &gt;&gt; $GITHUB_ENV
            echo &quot;AWS_SECRET_ACCESS_KEY=${{ env.AWS_SECRET_ACCESS_KEY }}&quot; &gt;&gt; $GITHUB_ENV
          fi
      - name: Clone Repository (Current branch)
        uses: actions/checkout@v2
        if: github.event.inputs.git-ref == ''
  
      - name: Clone Repository (Custom Ref)
        uses: actions/checkout@v2
        if: github.event.inputs.git-ref != ''
        with:
          ref: ${{ github.event.inputs.git-ref }}
  
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}         
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} 
          aws-region: ${{ needs.setup2.outputs.AWS_REGION }}
  
      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v1
  
      - name: Build, tag, and push image to Amazon ECR
        id: build-image
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
          ECR_REPOSITORY: ${{ needs.setup.outputs.ECR_REPOSITORY }}
          IMAGE_TAG: ${{ github.sha }}
        run: |
          cd ${{ needs.setup.outputs.BUILD_DIR }}
          docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t $ECR_REGISTRY/$ECR_REPOSITORY:latest .
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
          docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
          echo &quot;::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG&quot;
      - name: Download task definition
        run: |
          aws ecs describe-task-definition --task-definition ${{ needs.setup.outputs.ECS_TASK_DEFINITION }} --query taskDefinition &gt; ${{ needs.setup.outputs.ECS_TASK_DEFINITION_FILE }}
      - name: Fill in the new image ID in the Amazon ECS task definition
        id: task-def
        uses: aws-actions/amazon-ecs-render-task-definition@v1
        with:
          task-definition: ${{ needs.setup.outputs.ECS_TASK_DEFINITION_FILE }}
          container-name: ${{ needs.setup.outputs.ECS_CONTAINER_NAME }}
          image: ${{ steps.build-image.outputs.image }}
  
      - name: Deploy Amazon ECS task definition
        uses: aws-actions/amazon-ecs-deploy-task-definition@v1
        with:
          task-definition: ${{ steps.task-def.outputs.task-definition }}
          service: ${{ needs.setup.outputs.ECS_SERVICE }}
          cluster: ${{ needs.setup.outputs.ECS_CLUSTER }}
          wait-for-service-stability: true
  </code></pre>
  <p>As you can see, we are just duplicating the env variables, but what we want is essentially get rid of that step and do it using a single step for the inputs which we have, then things should go automatically from dev to prod, I think something based on an if condition can be done, but I am just not sure how to implement it any ideas are welcome, thanks.</p>