I am having trouble passing the step output from one job to another job. When I pass the output to another job it returns an empty string
jobs:
extract-branch-name:
if: github.event.pull_request.draft == false
name: Extract branch name
outputs:
branch: ${{ steps.extract_branch.outputs.formatted_branch }}
runs-on: ubuntu-latest
steps:
- name: Extract branch name
id: extract_branch
shell: bash
run: |
branch="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
formatted_branch=$(echo "$branch" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-zA-Z0-9]/-/g')
echo $formatted_branch
echo "branch=$formatted_branch" >> "$GITHUB_OUTPUT"
push-engine-image:
if: github.event.pull_request.draft == false
name: Push Engine Image
runs-on: ubuntu-latest
needs: extract-branch-name
steps:
- name: Build, tag, and push ENGINE image to Amazon ECR
id: build-engine-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
run: |
echo "hello"
echo ${{ needs.extract-branch-name.outputs.branch }}
echo "hello"
...
When I look at the github action logs the echo needs.extract-branch-name.outputs.branch
returns empty string, to be honest I am not sure what to do.
Your issue seems to be in the first job, specifically this line:
echo "branch=$formatted_branch" >> "$GITHUB_OUTPUT"
With this configuration, the output name will be branch
, and not formatted_branch
, as you stated in the output configuration in the job, at that line:
branch: ${{ steps.extract_branch.outputs.formatted_branch }}
There are 2 ways to resolve this:
The updated workflow would look like this for the first option:
jobs:
extract-branch-name:
if: github.event.pull_request.draft == false
name: Extract branch name
outputs:
branch: ${{ steps.extract_branch.outputs.formatted_branch }}
runs-on: ubuntu-latest
steps:
- name: Extract branch name
id: extract_branch
shell: bash
run: |
branch="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
formatted_branch=$(echo "$branch" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-zA-Z0-9]/-/g')
echo $formatted_branch
echo "formatted_branch=$formatted_branch" >> "$GITHUB_OUTPUT" # updated here