Github动作在if/needs条件下无法识别另一个作业的输出



我已经设置了多个工作的工作流。作业A是作业B运行的必要条件。

在PR上触发工作流,作业A检查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

我为该作业设置了outputscomment的检查输出。

在作业B中,我需要检查输出是ID还是空字符串:

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

if检查总是等于true,即outputs.comment总是一个空字符串。我尝试了多种变化的if检查,有和没有always(),有和没有[],但needs.job-a.outputs.comment总是一个空字符串,即使它成功地登录了job-a检查中的评论id。这个任务总是运行,这不是我想要的。我只希望在PR注释不存在时运行作业。

谁能告诉我我在这里做错了什么?

您需要为步骤定义一个标识符,以便能够将其作为作业的输出引用。请参阅文档的相关部分:

设置步骤的输出参数。注意,该步骤需要定义一个id,以便稍后检索输出值。

所以你应该把你的代码改成:
job-a:
outputs:
comment: ${{ steps.store-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
id: store-comment
run: echo "comment=${{ steps.find-comment.outputs.comment-id }}" >> $GITHUB_OUTPUT

答案是将outputs变量更改为comment-id而不仅仅是comment。我认为链接属性必须匹配您输入的echo命令,但显然它必须匹配peter-evans/find-comment@v2输出中的内容:https://github.com/peter-evans/find-comment#outputs

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

最新更新