Retrigger failed run action #9258
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Retrigger failed run action | |
on: | |
workflow_dispatch: | |
inputs: | |
run_id: | |
description: The ID of the workflow run to retrigger | |
required: true | |
error_messages: | |
description: Custom error messages to look for in the logs | |
required: true | |
repository: | |
description: GitHub repository (ORG/REPO_NAME) | |
required: true | |
notify_back_on_error: | |
description: |- | |
When the error messages (`error_messages`) are not found, the workflow will | |
re-trigger your calling workflow with the parameter `notify_back_error_message`. | |
Your calling workflow must implement the `notify_back_error_message` parameter | |
and forward the error. | |
This allows you to capture failures that are not related to a specific error. | |
default: "false" | |
notify_back_git_ref: | |
description: The Git reference to use when notifying back a workflow. | |
default: main | |
jobs: | |
rerun: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Import Secrets | |
id: secrets | |
uses: hashicorp/[email protected] | |
with: | |
url: ${{ secrets.VAULT_ADDR }} | |
method: approle | |
roleId: ${{ secrets.VAULT_ROLE_ID }} | |
secretId: ${{ secrets.VAULT_SECRET_ID }} | |
secrets: | | |
secret/data/products/infra/ci/retrigger-gha-workflow RETRIGGER_APP_KEY; | |
secret/data/products/infra/ci/retrigger-gha-workflow RETRIGGER_APP_ID; | |
- name: Extract owner and repo from repository input | |
id: extract-owner-repo | |
run: | | |
owner="$(echo '${{ inputs.repository }}' | cut -d'/' -f1)" | |
repository="$(echo '${{ inputs.repository }}' | cut -d'/' -f2)" | |
echo "owner=$owner" | tee -a "$GITHUB_OUTPUT" | |
echo "repository=$repository" | tee -a "$GITHUB_OUTPUT" | |
- name: Generate a Github App for the repository | |
id: github-token | |
uses: actions/create-github-app-token@v1 | |
with: | |
app-id: ${{ steps.secrets.outputs.RETRIGGER_APP_ID }} | |
private-key: ${{ steps.secrets.outputs.RETRIGGER_APP_KEY }} | |
owner: ${{ steps.extract-owner-repo.outputs.owner }} | |
repositories: ${{ steps.extract-owner-repo.outputs.repository }} | |
- name: Check previous run logs | |
id: check_logs | |
env: | |
GH_DEBUG: api | |
GITHUB_TOKEN: ${{ steps.github-token.outputs.token }} | |
run: | | |
echo "Fetching logs for run with id: ${{ inputs.run_id }} from ${{ inputs.repository }} repository." | |
# Fetch the logs from the previous run | |
gh config set pager cat | |
logs=$(gh run view ${{ inputs.run_id }} -R ${{ inputs.repository }} --exit-status --log-failed || true) | |
echo "Logs coming from run with id: ${{ inputs.run_id }}" | |
inputted_errors='${{ inputs.error_messages }}' | |
echo "Inputted errors: $inputted_errors" | |
rerun=$(echo "$logs" | jq -c -n -R --argjson errors "$inputted_errors" '[inputs] | any(test($errors | join("|")))') | |
echo "Notify back: ${{ inputs.notify_back_on_error }}" | |
echo "Rerun: $rerun" | |
echo "rerun=$rerun" >> "$GITHUB_OUTPUT" | |
- name: Rerun run with id ${{ inputs.run_id }} for ${{ inputs.repository }} repository | |
if: steps.check_logs.outputs.rerun == 'true' | |
env: | |
GH_DEBUG: api | |
GITHUB_TOKEN: ${{ steps.github-token.outputs.token }} | |
run: | | |
: # wait until the job is completed | |
gh run watch ${{ inputs.run_id }} -R ${{ inputs.repository }} > /dev/null 2>&1 | |
gh run rerun ${{ inputs.run_id }} -R ${{ inputs.repository }} --failed | |
- name: Notify back error for run with id ${{ inputs.run_id }} for ${{ inputs.repository }} repository | |
if: steps.check_logs.outputs.rerun == 'false' && inputs.notify_back_on_error == 'true' | |
env: | |
GH_DEBUG: api | |
GITHUB_TOKEN: ${{ steps.github-token.outputs.token }} | |
run: | | |
: # retrieve workflow details | |
WORKFLOW_URL="${{ github.server_url }}/${{ inputs.repository }}/actions/runs/${{ inputs.run_id }}" | |
notify_back_error_message="This workflow attempted a retry but there was no match on the log message, so it was not retried. Please check: $WORKFLOW_URL" | |
WORKFLOW_NAME=$(gh run view ${{ inputs.run_id }} -R ${{ inputs.repository }} --json workflowName --jq '.workflowName') | |
echo "WORKFLOW_NAME=${WORKFLOW_NAME}" | |
: # trigger a new run with the error notify | |
gh workflow run "${WORKFLOW_NAME}.yml" -R ${{ inputs.repository }} -F notify_back_error_message="$notify_back_error_message" --ref="${{ inputs.notify_back_git_ref }}" |