githubgithub-actionscontinuous-deployment

Read out environment name in github reusable workflow


In our github repository, we have a github action that calls a reusable workflow in an environment.

name: Pull Request Merged

concurrency:
  group: ${{ github.ref }}

on:
  pull_request:
    types: [closed]
  
jobs:
  deploy_to_stage:
    if: |
      github.event.pull_request.merged == true && 
      contains(github.event.pull_request.labels.*.name, 'Stage')
    name: Deploy to Stage
    uses: ./.github/workflows/deploy.yml
    with:
      environment: Stage
    secrets: inherit

The reusable workflow is roughly as follows:

name: deploy
on:
  workflow_call:
    secrets:
      AWS_ACCESS_KEY_ID:
        required: true
      AWS_SECRET_ACCESS_KEY:
        required: true

jobs:
  deployment:
    runs-on: ubuntu-latest
    steps:
[...]

How can I access the value of the environment name (here: "Stage") in a step of the reusable workflow?


Solution

  • It's not possible to get this value from the workflow context.

    A workaround could be adding an environment input in the reusable workflow receiving the value:

    name: deploy
    
    on:
      workflow_call:
        inputs:
          environment:
            required: true
        secrets:
          AWS_ACCESS_KEY_ID:
            required: true
          AWS_SECRET_ACCESS_KEY:
            required: true
    
    

    Then, you could access the input value from anywhere in the reusable workflow by using ${{ inputs.environment }}.