github-actions

Passing env variable 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?


Solution

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