Home > database >  Read out environment name in github reusable workflow
Read out environment name in github reusable workflow

Time:11-18

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?

CodePudding user response:

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)

CodePudding user response:

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

  • Related