Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing environment variables as inputs to a reusable workflow

I'm trying to call a reusable workflow from another one, passing it some input variables. In the caller workflow, I have some environment variables that I want to pass as input to the reusable one, like so:

env:
  SOME_VAR: bla_bla_bla
  ANOTHER_VAR: stuff_stuff

jobs:
  print:
    runs-on: ubuntu-latest
    steps:
      - name: Print inputs passed to the reusable workflow
        run: |
          echo "some var: $SOME_VAR"
          echo "another var: $ANOTHER_VAR"
  call_reusable:
    uses: ...
    with:
      input_var: $SOME_VAR
      another_input_var: $ANOTHER_VAR

The reusable workflow:

on:
  workflow_dispatch:
  workflow_call:
    inputs:
      input_var:
        required: true
        type: string
      another_input_var:
        required: true
        type: string

jobs:
  the_job:
    runs-on: ubuntu-latest
    steps:
      - name: Print inputs
        run: |
          echo "input_var: ${{ inputs.input_var }}"
          echo "another_input_var: ${{ inputs.another_input_var }}"

The Print inputs passed to the reusable workflow step works fine - all variables are correctly printed. However, the Print inputs step in the reusable workflow (the callee) does not work as expected - all the variables are empty.

I couldn't find anything in the docs suggesting that there is something wrong with my approach so, the way I see it, this should be working. Still, looking at the logs there is something wrong, as in the reusable workflow (callee) I can see:

Run echo "input_var: $SOME_VAR"
  echo "another_input_var: $ANOTHER_VAR"
  shell: /usr/bin/bash -e {0}
input_var: 
another_input_var: 

I tried wrapping the values in the with: block in $(echo) but that didn't work.

Any ideas?

like image 589
takecare Avatar asked Aug 31 '25 15:08

takecare


2 Answers

After some researches, I found this thread explaining that:

  1. Because you can’t pass ENV variables to the reusable workflow, they are almost useless in this pattern.

Moreover, on the official documentation, it is stated that (emphasis mine):

  • Any environment variables set in an env context defined at the workflow level in the caller workflow are not propagated to the called workflow.

Therefore, in your case, you won't be able to achieve what you want using directly the env variable, but there are workarounds. You could use outputs from a first job, and use those outputs as the reusable workflow inputs.

Here is an example of how it could be done:

env:
  SOME_VAR: bla_bla_bla
  ANOTHER_VAR: stuff_stuff

jobs:
  print:
    runs-on: ubuntu-latest
    outputs:
      some_var: ${{ steps.step1.outputs.some_var }}
      another_var: ${{ steps.step1.outputs.another_var }}   
    steps:
      - name: Print inputs passed to the reusable workflow
        id: step1
        run: |
          echo "some var: $SOME_VAR"
          echo "some_var=$SOME_VAR" >> $GITHUB_OUTPUT
          echo "another var: $ANOTHER_VAR"
          echo "another_var=$ANOTHER_VAR" >> $GITHUB_OUTPUT
  
  call_reusable:
    needs:
      - print
    uses: ...
    with:
      input_var: ${{ needs.print.outputs.some_var }}
      another_input_var: ${{ needs.print.outputs.another_var }}

That way, without updating the reusable workflow implementation, the inputs would be filled with the expected values.

Here are the workflows I used to test: main + reusable

And you can check the workflow run with the expected outcome here.

like image 51
GuiFalourd Avatar answered Sep 02 '25 19:09

GuiFalourd


Lately GitHub introduced a feature to support variables in environment (very basic and the long awaited feature, I must say).
So other solution could be to create an environment in GitHub, create some variables under it with desired values and use the environment as needed.

Caller workflow:

jobs:
  Dev-Deployment:
    uses: ./.github/workflows/reusable-workflow.yml
    with:
        environment: Development

  QA-Deployment:
    uses: ./.github/workflows/reusable-workflow.yml
    with:
        environment: QA

Reusable workflow:

on:
 workflow_call:
  inputs:
    environment:
      type: string
      required: true

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: ${{ inputs.environment }}
    steps:
      - name: Print variables - ${{ vars.SOME_VAR }} - ${{ vars.ANOTHER_VAR }}
        run: |
            echo ${{ vars.SOME_VAR }}
            echo ${{ vars.ANOTHER_VAR }}

GitHub environment:

enter image description here

like image 37
Sid Avatar answered Sep 02 '25 20:09

Sid