Unable to use env variable from prd environment context

29 views Asked by At

Under settings in the repo created environment Variable Var1.

Below is github actions which will again call another template in different repo

name: GitHub Actions Demo
run-name: ${{ github.actor }} is testing out GitHub Actions 
on: 
  workflow_dispatch:
    inputs:
      Pass_Input:
        description: 'test'
        required: true
        default: "From Github action 2"

jobs:
  call-workflow-passing-data:
    env: prd
    uses: your-org/your-repo/.github/workflows/option1.yml@main
    with:
      MT_NAME: "Calling from Github 2"
      MT_FOLDER: ${{ vars.var1 }}

I get following error:

 Invalid workflow file: .github/workflows/option2.yml#L13
 The workflow is not valid. 
 .github/workflows/option2.yml (Line: 13, Col: 10): Unexpected value 'prd' 
 .github/workflows/option2.yml (Line: 14, Col: 5): Unexpected value 'uses'

Reference template of GHA is:

name: GitHub Actions Demo
run-name: ${{ github.actor }} is testing out GitHub Actions 
on: 
  workflow_call:
    inputs:
      MT_NAME:
        description: 'MT Name'
        required: true
        type: string
        
      MT_FOLDER:
        type: string
        description: 'MT Folder location'
        required: true

jobs:
  Explore-GitHub-Actions:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
      - run: echo "${{ inputs.MT_NAME }}"
      - run: echo "${{ inputs.MT_FOLDER }}"
        

Please can somebody help to fix this issue

1

There are 1 answers

3
jessehouwing On

I don't think you can pass the environment in the calling workflow the way you do, you'll need to convert that to an input as well. Plus, to pass an environment you need to use environment: and not env:, the latter is for setting a job level environment variable, the former is to link a job to an environment defined in github. I know, confusing as hell.

The following workflow works for me:

name: GitHub Actions Demo
on: 
  workflow_dispatch:

jobs:
  call-workflow-passing-data:
    uses: ./.github/workflows/callable.yml
    with:
      MT_NAME: "Calling from Github 2"
      MT_FOLDER: ${{ vars.var1 }}
      environment: 'prd'

and

name: Callable Workflow
run-name: ${{ github.actor }} is testing out GitHub Actions 
on: 
  workflow_call:
    inputs:
      MT_NAME:
        description: 'MT Name'
        required: true
        type: string
        
      MT_FOLDER:
        type: string
        description: 'MT Folder location'
        required: true

      environment:
        type: string
        required: true

jobs:
  Explore-GitHub-Actions:
    runs-on: ubuntu-latest
    environment: ${{ inputs.environment }}
    steps:
      - run: echo "${{ inputs.MT_NAME }}"
      - run: echo "${{ inputs.MT_FOLDER }}"

Be sure to create the variable at the Repository level, you can't pass in an Environment level variable, as the calling workflow doesn't have access to the environment!

enter image description here

Result:

enter image description here

If the variable you're trying to reference is stored at the environment level...

enter image description here

You need to use a slightly different syntax:

jobs:
  call-workflow-passing-data:
    uses: ./.github/workflows/callable.yml
    with:
      environment: 'prd'
      MT_FOLDER_VAR: var1 # <--- PASS IN THE VARIABLE NAME, NOT ITs VALUE

Then reference it in the callable workflow as follows:

jobs:
  Explore-GitHub-Actions:
    runs-on: ubuntu-latest
    environment: ${{ inputs.environment }}
    steps:
      # ...
      - run: echo "${{ vars[inputs.MT_FOLDER_VAR] }}"

That will allow you to reference the variable stored at the environment through the input of the callable workflow.

enter image description here