In a Github Workflow, we are currently running a notification action whenever the workflow fails. But we want to also get a notification when the problem get fixed. In github actions terms: when the current workflow successes, but the last run (of the same workflow, on the same branch) failed.
Is there a way to condition the execution of a step to the status of a previous run?
What i tried:
Searches gave me a lot of pages about conditioning on the status of previous steps, but not previous runs.
My first guess was navigating on the Contextual information but i didn't find information beyond the current run or the branch status.
Finally i found a solution. The following line obtains the status of the last run of a given workflow on the branch. It uses the public github api.
# Within a step
LAST_RUN=$(
curl -s https://api.github.com/repos/${{github.repository}}/actions/runs?branch=${{ github.ref_name }} |
jq -r '[.workflow_runs | sort_by(.created_at) | .[] | select(.name == "${{ github.workflow }}") | select(.id != ${{github.run_id}}) | { branch: .head_branch, conclusion: .conclusion } ] | .[-1].conclusion // "new_branch"'
)
echo "LAST_RUN=$LAST_RUN" >> $GITHUB_ENV
The curl
command uses github api to retrieve later runs on the branch for the repository (must be public). The jq filter takes all the runs, sorts them by creation date, filters the ones matching the same workflow, removes the current run, and picks the 'conclusion' attribute (it can be completed, action_required, cancelled, failure, neutral, skipped, stale, success, timed_out, in_progress, queued, requested, waiting, pending). In case of null, because there was no matching run, jq returns new_branch
. Within the command ${{ value }}
are substitutions that github actions will perform using the provided context.
The final echo to $GITHUB_ENV
fills a file that is loaded for each step which is availailable as ${{ env.LAST_RUN }}
in the next steps. This enables a conditional action on fail like this:
steps:
- name: ✅ Run tests
run: |
# here your tests
- name: Compile previous compilation status
if: always()
shell: bash
run: |
LAST_RUN=$(
curl -s https://api.github.com/repos/${{github.repository}}/actions/runs?branch=${{ github.ref_name }} |
jq -r '[.workflow_runs | sort_by(.created_at) | .[] | select(.name == "${{ github.workflow }}") | select(.id != ${{github.run_id}}) | { branch: .head_branch, conclusion: .conclusion } ] | .[-1].conclusion // "new_branch"'
)
echo "LAST_RUN=$LAST_RUN" >> $GITHUB_ENV
- name: 📢 Notification
if: job.status != 'success' || env.LAST_RUN != 'success'
# Here your notification
You can find an action using this in https://github.com/vokimon/gha-notify-telegram