我已经设置了多个工作的工作流。作业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
我为该作业设置了outputs
中comment
的检查输出。
在作业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 }}