Search code examples
yamlgithub-actionscicd

Error while passing output parameters between jobs


I need to pass the output parameter environment to the second job but for some reason it is always empty i.e.

environment: ${{ needs.set-environment-variable.outputs.Environment }}

Here's my workflow:

jobs:
  set-environment-variable:
    runs-on: ubuntu-latest
    outputs:
      environment: ${{ steps.set_var.outputs.Environment }}
      region: ${{ steps.set_var.outputs.Region }}
    steps:
      - name: Set Environment Variable
        id: set_var
        run: |
          Environment="${{ github.event.inputs.environment }}"
          echo "Environment=${{ github.event.inputs.environment }}" >> "GITHUB_OUTPUT"
          echo "este es el environment $Environment"
          if [[ -z "$Environment" || -z "$Feature2" ]]; then
            case $Environment in
              dev)
                echo "Configuring region for dev"
                echo "Environment=${{ github.event.inputs.environment }}" >> "GITHUB_OUTPUT"
                Region=us-west-2
                echo "Region=$Region" >> "GITHUB_OUTPUT"
                ;;
              stage)
                echo "Configuring region for stage"
                echo "Environment=${{ github.event.inputs.environment }}" >> "GITHUB_OUTPUT"
                Region=us-east-1
                echo "Region=$Region" >> "GITHUB_OUTPUT"
                ;;
              *)
                echo "Configuring region for default"
                Environment="dev"
                Region="us-west-2"
                echo "Environment=$Environment" >> "GITHUB_OUTPUT"
                echo "Region=$Region" >> "GITHUB_OUTPUT"
                ;;
            esac
          fi
  load-aws-credentials: # este será el "job" load-aws-credentials, el que está generando las variables de salida
    uses: xxx/xxx-github-actions-templates/.github/workflows/aws-assume-role-v1.yml@master
    needs: set-environment-variable
    secrets: inherit
    with:
      ENVIRONMENT: ${{ needs.set-environment-variable.outputs.Environment }}
      ROLE: AWS_CICD_DEPLOYMENT_ROLE
      SHARED_ROLE: AWS_OIDC_ROLE

Solution

  • You need to redirect the content to the correct output. Currently you have >> "GITHUB_OUTPUT", but this needs to be >> $GITHUB_OUTPUT (for bash).

    For shell: pwsh, use $env:GITHUB_OUTPUT

    The minimal yaml file that demonstrates this is:

    on:
      workflow_dispatch:
    
    jobs:
      build:
        outputs:
          environment: ${{ steps.environment-selector.outputs.environment }}
    
        runs-on: ubuntu-latest
        steps:
        - id: environment-selector
          run: |
            echo "environment=prod" >> $GITHUB_OUTPUT
              
      deploy:
         runs-on: ubuntu-latest
         needs: build
         environment: ${{ needs.build.outputs.environment }}
         steps:
         - run: |
             echo ${{ needs.build.outputs.environment }}
    

    If a value is set for an output, the Complete Job step in the logs should log that the value is being set:

    enter image description here

    If the value is empty, that line isn't logged. That can be a useful indicator where the issue is, in the job that sets the value or the job that needs the value.