Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to trigger a Github action if the previous one failed but the current one succeded?

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:

enter image description here

The last run should send a "tests recovered" mail

like image 210
juan Avatar asked Mar 11 '21 13:03

juan


People also ask

Can you manually trigger a GitHub action?

Earlier this week, GitHub announced a new feature which allows developers to trigger workflows manually from within the “Actions” tab.

How do I trigger another workflow in actions GitHub?

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.

How do I rerun an action on GitHub?

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.

How do I call one workflow from another workflow in GitHub Actions?

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.


2 Answers

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' }}
(...)
like image 69
juan Avatar answered Jan 04 '23 02:01

juan


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

like image 20
soltex Avatar answered Jan 04 '23 02:01

soltex