Search code examples
githubenvironment-variablesgithub-actionsworkflow

Is it possible to inherit the environment context of a called composite GitHub action in the caller workflow


I have the following composite action;

name: Send Email 
description: Use this composite action
# outputs:
#   key_value_x: 
#     value: ${{ env.testvarenv }} 
runs:
  using: "composite"
  steps:
  - name: set environment variable
    run: |
      echo "testvarenv='wow'" >> $env:GITHUB_ENV
    shell: powershell
  - name: Inside composite action
    run: |
      echo "testvarenv is ${{ env.testvarenv }}"
    shell: powershell

The testvarenv prints fine inside the composite action.

However, when I try to print the same in the caller workflow (outside the composite workflow) it does not print the value of ${{ env.testvarenv }}

    steps:
    - name: Checkout
      uses: actions/checkout@v3
    - name: Use the composite action in the root dir
      uses: ./
    - name: Outside composite action      
      run: |
        echo "testvarenv is ${{ env.testvarenv }}"

I'm aware how to use outputs: to get the value of any composite variable outside i.e. in the caller workflow; but wanted to know if it is possible to use environment variables.


Solution

  • Edit to correct the answer:

    Not sure why OP's saying that their example didn't work. You can repro it and it'll work.

    The docs for composite actions's runs.steps[*].env field state that;

    If you want to modify the environment variable stored in the workflow, use echo "{name}={value}" >> $GITHUB_ENV in a composite step.

    You can repro with linux action SO/76522542/nix/action.yml

    name: Set Env Value
    description: Use this composite action
    runs:
      using: "composite"
      steps:
      - name: set environment variable
        run: echo "ENVWORD=abcxyz" >> $GITHUB_ENV
        shell: bash
      - name: Inside composite action
        run: echo "ENVWORD is ${{ env.ENVWORD }}"
        shell: bash
    

    Windows action SO/76522542/win/action.yml (as OP used powershell which is windows runner exclusive, unlike pwsh)

    name: Set Env Value
    description: Use this composite action
    runs:
      using: "composite"
      steps:
      - name: set environment variable
        run: echo "ENVWORD='abcxyz'" >> $env:GITHUB_ENV
        shell: powershell
      - name: Inside composite action
        run: echo "ENVWORD is ${{ env.ENVWORD }}"
        shell: powershell
    

    And workflow .github/workflows/76522542.yml

    name: SO 76522542 - Env example
    on:
      push:
    jobs:
      linux-action-env-example:
        runs-on: ubuntu-latest
        defaults:
          run:
            shell: bash
        steps:
        - name: Checkout
          uses: actions/checkout@v4
        - name: Use the composite action
          uses: ./SO/76522542/nix
        - name: Outside composite action      
          run: echo "ENVWORD is ${{ env.ENVWORD }}"
      windows-action-env-example:
        runs-on: windows-latest
        defaults:
          run:
            shell: powershell
        steps:
        - name: Checkout
          uses: actions/checkout@v4
        - name: Use the composite action
          uses: ./SO/76522542/win
        - name: Outside composite action      
          run: |
            echo "ENVWORD is ${{ env.ENVWORD }}"
    

    This appears to have always been the case per the docs (at least 4 years ago).

    Original Answer: (that was for worfklows, and is wrong for actions)

    See Reusing workflows: Limitations for;

    Similarly, environment variables set in the env context, defined in the called workflow, are not accessible in the env context of the caller workflow.