skip to Main Content

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?

2

Answers


  1. After some researches, I found this thread explaining that:

    You can’t pass ENV variables to the reusable workflow, so they are almost useless in this pattern.

    Moreover, on the official documentation, it is stated that:

    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 wont be able to achieve what you want using directly the env variable, but there are workarounds.

    Note: It would be great if GitHub comes up with a better way to assign values inline or pass them into reusable workflows, as handling each parameter many times just to pass it into a reusable workflow is cumbersome.


    A workaround could be to 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 }}
    

    EDIT: Removed ::set-output syntax which is now deprecated.

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

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

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

    Login or Signup to reply.
  2. The use of ::set-output is now deprecated.

    See https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/.

    Now recommended:

    - name: Save state
      run: echo "{name}={value}" >> $GITHUB_STATE
    
    - name: Set output
      run: echo "{name}={value}" >> $GITHUB_OUTPUT
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search