如果前一个操作失败,但当前操作成功,有没有办法触发Github操作?
我已经设置了一个Github操作,在失败时发送一封电子邮件(用于集成测试),我想发送另一封电子邮件,通知它已恢复;为此,我需要知道上一次运行是否失败,而当前运行是否成功 这是故障代码:如果前一个操作失败,但当前操作成功,有没有办法触发Github操作?,github,github-actions,Github,Github Actions,我已经设置了一个Github操作,在失败时发送一封电子邮件(用于集成测试),我想发送另一封电子邮件,通知它已恢复;为此,我需要知道上一次运行是否失败,而当前运行是否成功 这是故障代码: (...) - name: Send mail if failed if: ${{ failure() }} uses: juanformoso/action-send-mail@1 with: (...) 有没有办法完成我所需要的?类似于${{if success\u but_previous\u
(...)
- name: Send mail if failed
if: ${{ failure() }}
uses: juanformoso/action-send-mail@1
with:
(...)
有没有办法完成我所需要的?类似于${{if success\u but_previous\u failed()}}
编辑:我的意思是在工作流“运行”之间,这是我想要检测的情况:
最后一次运行应发送一封“测试已恢复”邮件,您可以使用该邮件并请求:
GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs
以获取特定工作流的所有运行,然后解析输出以检索最新的运行结果
我将用这个向您展示一个示例:
- name: Get latest workflow run status
id: latest-workflow-status
run: echo "::set-output name=result::$(curl https://api.github.com/repos/sdras/awesome-actions/actions/workflows/32764/runs 2>/dev/null | jq -r '.workflow_runs[0].conclusion')"
- name: Run only if previous workflow run failed and the current succeeded
if: ${{ success() && steps.latest-workflow-status.outputs.result == 'failure' }}
run: ...
您可以从此请求获取工作流ID:
在soltex的基础上进行扩展,我最终这样做是为了进行经过身份验证的API调用:
- name: Get latest workflow run status
uses: actions/github-script@v3
id: latest-workflow-status
with:
script: |
const runs = await github.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: 'runtests.yml',
per_page: 2
})
return runs.data.workflow_runs[1].conclusion
result-encoding: string
(...)
- name: Send mail if previous workflow run failed and the current succeeded
if: ${{ success() && steps.latest-workflow-status.outputs.result == 'failure' }}
(...)
- name: Send mail if previous workflow run succeeded and the current failed
if: ${{ failure() && steps.latest-workflow-status.outputs.result == 'success' }}
(...)
谢谢,但我指的是以前的“工作流运行”失败,而不是同一运行中的任何步骤。对不起,我误解了你的问题。我刚刚根据您更新的问题更新了答案。这太棒了,非常感谢(我确实需要对它进行一些修改,因为它是一个私有回购,但我认为我可以使用经过身份验证的api来获得它),看起来很好,而且非常简单!我忘了github操作支持js脚本。我们可以用这些工作流做很多事情。感谢分享您的解决方案。