I have set up a Github action to send an email on failure (for integration tests) and I'd like to send another one to notify that it recovered; for that I need to know if the previous run failed but the current one succeeded.
This is the code for the failure:
(...)
- name: Send mail if failed
if: ${{ failure() }}
uses: juanformoso/action-send-mail@1
with:
(...)
Is there a way to accomplish what I need? something like ${{ if success_but_previous_failed() }}
?
EDIT: I mean between workflow "runs", this is the situation I want to detect:
The last run should send a "tests recovered" mail
Earlier this week, GitHub announced a new feature which allows developers to trigger workflows manually from within the “Actions” tab.
If you do want to trigger a workflow from within a workflow run, you can use a personal access token instead of GITHUB_TOKEN to trigger events that require a token. You'll need to create a personal access token and store it as a secret.
Under your repository name, click Actions. In the left sidebar, click the workflow you want to see. From the list of workflow runs, click the name of the run to see the workflow run summary. In the upper-right corner of the workflow, use the Re-run jobs drop-down menu, and select Re-run failed jobs.
You call a reusable workflow by using the uses keyword. Unlike when you are using actions within a workflow, you call reusable workflows directly within a job, and not from within job steps.
Expanding on soltex's excellent answer, I ended up doing this in order to make an authenticated API call:
- 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' }}
(...)
You can use GitHub Actions API and make a request to:
GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs
in order to get all runs of a specific workflow and then parse the output to retrieve the latest run result.
I will use this public repository to show you an example:
- 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: ...
You can get the workflow ids from this request:
https://api.github.com/repos/sdras/awesome-actions/actions/workflows
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With