1

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?

2
  • 2
    It's not possible to get this value from the workflow context. Why not adding an environment input in the reusable workflow receiving the value? Then, you could access it from anywhere in the reusable workflow by using ${{ inputs.environment }}. Commented Nov 17, 2022 at 11:29
  • 1
    @GuiFalourd thanks, I like the idea. If you convert your comment into an answer, I'll happily accept it. Commented Nov 17, 2022 at 15:43

2 Answers 2

4

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 }}.

Sign up to request clarification or add additional context in comments.

Comments

2

You could use environment secrets to store the stage name within that deployment environment, then access the environment variable within your script (eg bash script) or as a component of the action ${{ env.DAY_OF_WEEK == 'Monday' }} (ref)

4 Comments

Thank you for your answer! Actually, that's what we already had in place. But I don't like the fact that the variables are treated as secret whereas they can be public. This makes outputs etc. less readable, and renders it unnecessarily difficult to understand for those who don't know the value of the secret.
Since a few weeks Github has a new functionality for environments variables that can be used for that.
@user2189998 thank you for the update! Can you possibly provide a pointer to that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.