Passing vars using reusable workflow with no success

1.4k Views Asked by At

We try to pass some env variables using a workaround to the reusable workflow as follows, but no variables are passed.

Workflow YAML is:

name: "call my_reusable_workflow"

on:
  workflow_dispatch:

env: 
  env_branch: ${{ github.head_ref }}
  env_workspace: ${{ github.workspace }}

jobs:
  call_reusable_workflow_job:
    uses: my_github/my-reusable-workflow-repo/.github/workflows/used_wf_test.yml@master
    with:
      env_vars: |
        hello-to=Meir
        branch_name=${{ env.env_branch }}
    secrets:
      my_token: ${{secrets.ENVPAT}}

and the reusable workflow YAML is:

name: my_reusable_workflow

on:
  workflow_call:
    inputs:
      env_vars:
        required: true
        type: string
        description: list of vars and values
    secrets:
      giraffe_token:
        required: true    

jobs:
  reusable_workflow_job:
    runs-on: ubuntu-latest
    steps:  
    - name: set environment variables
      if: ${{ inputs.env_vars }}
      run: |
        for env in "${{ inputs.env_vars }}"
        do
          printf "%s\n" $env >> $GITHUB_ENV
        done

When the action is running it gets the value of hello-to=Meir but doesn't get the value branch_name=${{ env.env_branch }}.

I tried to pass the value also as branch_name=${{ github.head_ref }} but with no success.

1

There are 1 best solutions below

0
On

According to the Limitations of Reusing workflows:

Any environment variables set in an env context defined at the workflow level in the caller workflow are not propagated to the called workflow. For more information, see "Variables" and "Contexts."

So, the env context is not supported in reusable workflow callers at the moment.


However, you can pass the Default Environment Variables to reusable workflow callers.

For example, in your particular scenario, you want to use these contexts:

  • github.head_ref
  • github.workspace

The equivalent default environment variables are:

  • GITHUB_HEAD_REF
  • GITHUB_WORKSPACE

And, your reusable workflow (e.g. reusable_workflow_set_env_vars.yml) will be called by its caller (e.g. reusable_workflow_set_env_vars_caller.yml) like this:

name: reusable_workflow_set_env_vars_caller

on:
  workflow_dispatch:

jobs:
  set-env-vars:
    uses: ./.github/workflows/reusable_workflow_set_env_vars.yml
    with:
      env_vars: |
        TEST_VAR='test var'
        GITHUB_HEAD_REF=$GITHUB_HEAD_REF
        GITHUB_WORKSPACE=$GITHUB_WORKSPACE
        GITHUB_REF=$GITHUB_REF

Apart from that, regarding your implementation of the reusable workflow (e.g. reusable_workflow_set_env_vars.yml):