githubgithub-actions

How to get current environment name?


In GitHub Workflow I'm looking for a build-in variable which would contain the name of the current GH deployment environment (e.g. "staging") or be blank/unset if no such thing exist.

Since there doesn't seem to be a build-in variable for it (perhaps I'm overlooking it?), is there another way that this value can be obtained from within a pipeline step?

Here's an example workflow:

jobs:

  job1:
    runs-on: ubuntu-latest
    environment: uat

    steps:
      - name: 'Print name'
        run: echo "GH deployment env : ..."

  job2:
    runs-on: ubuntu-latest
    environment: staging

    steps:
      - name: 'Print name'
        run: echo "GH deployment env : ..."

Solution

  • I've tried different ways.

    The following two worked well for me:

    Input params
    name: Deploy
    
    on:
      workflow_dispatch:
        inputs:
          environment:
            description: 'Environment to deploy to'
            required: false
            default: 'QA'
            type: choice
            options:
              - QA
              - STAGING
              - PROD
    
    env:
      DEPLOY_ENV: ${{ inputs.environment }}
    
    jobs:
      Deploy:
        runs-on: ubuntu-latest
        environment: ${{ inputs.environment }}
        
        steps:
          - name: Deploy
            run: echo "Deploying to $DEPLOY_ENV 🚀"
    

    Deploying to QA

    The Matrix strategy

    https://docs.github.com/en/actions/learn-github-actions/contexts#matrix-context

    name: GitHub Actions Demo
    run-name: ${{ github.actor }} is testing out GitHub Actions 🚀
    on: 
      push:
      workflow_dispatch:
    
    jobs:
      Deploy-DEV:
        runs-on: ubuntu-latest
        strategy:
          matrix:
            environment: [DEV]    
    
        steps:            
          - run: echo "💡 Deploying to ${{ matrix.environment }}."            
    
      Deploy-QA:
        runs-on: ubuntu-latest
        needs: Deploy-DEV
        strategy:
          matrix:
            environment: [QA]    
    
        steps:            
          - run: echo "💡 Deploying to ${{ matrix.environment }}." 
    

    Github workflow

    Deploying to DEV