Search code examples
yamlgithub-actions

Github actions failing to recognise outputs of another job in an if/needs condition


I have set up a workflow with multiple jobs. Job A is a requirement for Job B to run.

The workflow is triggered on PR, and Job A checks for the existence of a comment on the PR:

job-a:
    outputs:
      comment: ${{ steps.find-comment.outputs.comment }}
    steps:
    - name: Check if QR already exists
      uses: peter-evans/find-comment@v2
      id: find-comment
      with:
        issue-number: ${{ github.event.number }}
        comment-author: "github-actions[bot]"
        body-includes: Preview Bundle

    - name: Store find-comment output
      run: echo "comment=${{ steps.find-comment.outputs.comment-id }}" >> $GITHUB_OUTPUT

    - name: Check find-comment outcome
      run: echo "This is the output of find-comment ${{ steps.find-comment.outputs.comment-id }}" # Successfully logs out comment id

I set the output of the check into comment in outputs for that job.

In Job B, I then need to check if that output is an ID or an empty string:

job-b:
    needs: [job-a]
    if: always() && needs.job-a.outputs.comment == ''
    steps:
      - name: Check for find-comment
        run: echo "This is the input of find-comment ${{ needs.job-a.outputs.comment }}" # always logs out an empty string

The if check always equates to true, i.e. the outputs.comment is always an empty string. I've tried multiple variations of the if check, with and without the always(), with and without the [], but needs.job-a.outputs.comment always is an empty string even though it successfully logs out the comment id in the check in job-a. This job then always runs, which is not what I want. I only want the job to run when the PR comment doesn't exist.

Can anyone tell me what I'm doing wrong here?


Solution

  • The answer was to change the outputs variable to take comment-id instead of just comment. I thought the chained property had to match what you put int eh echo command, but apparently it has to match what is in the outputs of peter-evans/find-comment@v2: https://github.com/peter-evans/find-comment#outputs

    job-a:
        outputs:
          comment: ${{ steps.store-comment.outputs.comment-id }}