|
| 1 | +name: 'Handle failures in the workflow' |
| 2 | +on: |
| 3 | + workflow_call: |
| 4 | + inputs: |
| 5 | + env: |
| 6 | + description: 'Environment the task has been failed.' |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + task_name: |
| 10 | + description: 'Name of the task.' |
| 11 | + required: true |
| 12 | + type: string |
| 13 | + team_id: |
| 14 | + description: 'ID of Jira Team' |
| 15 | + required: true |
| 16 | + type: string |
| 17 | + secrets: # all secrets are passed explicitly in this workflow |
| 18 | + jira_api_token: |
| 19 | + required: true |
| 20 | + |
| 21 | +permissions: |
| 22 | + contents: write |
| 23 | + issues: write |
| 24 | + |
| 25 | +jobs: |
| 26 | + failure-handler: |
| 27 | + runs-on: ubuntu-latest |
| 28 | + steps: |
| 29 | + - name: Checkout repository |
| 30 | + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 |
| 31 | + |
| 32 | + - name: Check if an issue already exists |
| 33 | + id: check-issue |
| 34 | + env: |
| 35 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 36 | + TARGET_ENV: ${{ inputs.env }} |
| 37 | + TASK_NAME: ${{ inputs.task_name }} |
| 38 | + REPO: ${{ github.repository }} |
| 39 | + run: | |
| 40 | + query="(${TARGET_ENV}}) The ${TASK_NAME} task has failed." |
| 41 | + number_issue=$(gh search issues "${query}" --repo "${REPO}" --state open --label failed-release --json title | jq length) |
| 42 | + |
| 43 | + if [ -z "${number_issue}" ]; then |
| 44 | + echo "There was an issue with the GH APIs. Stopping execution." |
| 45 | + return 1 |
| 46 | + fi |
| 47 | + |
| 48 | + echo "number_issue=${number_issue}" |
| 49 | + if [ "${number_issue}" -gt 0 ]; then |
| 50 | + echo "An issue already exists. Stopping execution." |
| 51 | + echo "found-issue=true" >> "${GITHUB_OUTPUT}" |
| 52 | + return 0 |
| 53 | + fi |
| 54 | + |
| 55 | + echo "found-issue=false" >> "${GITHUB_OUTPUT}" |
| 56 | + - name: Create Issue # Create an issue in the repo if the release fails |
| 57 | + if: ${{ steps.check-issue.outputs.found-issue == 'false' }} |
| 58 | + uses: imjohnbo/issue-bot@572eed14422c4d6ca37e870f97e7da209422f5bd |
| 59 | + env: |
| 60 | + target_env: ${{ inputs.env }} |
| 61 | + with: |
| 62 | + labels: failed-release |
| 63 | + title: "(${{env.target_env}}) ${{ inputs.task_name }} failed :scream_cat:" |
| 64 | + body: See https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 65 | + token: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments