Create Issue on Failure #977
This file contains hidden or 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: Create Issue on Failure | |
| on: | |
| workflow_run: | |
| workflows: | |
| - "AI Assistant CI/CD Pipeline" | |
| - "MCP Server CI/CD" | |
| - "Airflow DAG CI/CD" | |
| - "Deploy Documentation to GitHub Pages" | |
| - "Pre-commit" | |
| - "Airflow Validation" | |
| types: | |
| - completed | |
| permissions: | |
| issues: write | |
| actions: read | |
| jobs: | |
| create-issue-on-failure: | |
| runs-on: ubuntu-latest | |
| if: ${{ github.event.workflow_run.conclusion == 'failure' }} | |
| steps: | |
| - name: Get workflow run details | |
| id: workflow | |
| run: | | |
| echo "workflow_name=${{ github.event.workflow_run.name }}" >> $GITHUB_OUTPUT | |
| echo "workflow_url=${{ github.event.workflow_run.html_url }}" >> $GITHUB_OUTPUT | |
| echo "run_id=${{ github.event.workflow_run.id }}" >> $GITHUB_OUTPUT | |
| echo "commit_sha=${{ github.event.workflow_run.head_sha }}" >> $GITHUB_OUTPUT | |
| echo "branch=${{ github.event.workflow_run.head_branch }}" >> $GITHUB_OUTPUT | |
| echo "triggered_by=${{ github.event.workflow_run.triggering_actor.login }}" >> $GITHUB_OUTPUT | |
| - name: Check if issue already exists | |
| id: check_issue | |
| run: | | |
| # Search for existing open issues with the workflow name and run ID | |
| ISSUE_NUMBER=$(gh issue list \ | |
| --repo ${{ github.repository }} \ | |
| --state open \ | |
| --label "ci-failure" \ | |
| --search "CI Failure: ${{ steps.workflow.outputs.workflow_name }} in:title" \ | |
| --json number \ | |
| --jq '.[0].number // ""') | |
| echo "existing_issue=${ISSUE_NUMBER}" >> $GITHUB_OUTPUT | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| - name: Create issue for workflow failure | |
| if: steps.check_issue.outputs.existing_issue == '' | |
| uses: actions/github-script@v8 | |
| with: | |
| script: | | |
| const workflowName = '${{ steps.workflow.outputs.workflow_name }}'; | |
| const workflowUrl = '${{ steps.workflow.outputs.workflow_url }}'; | |
| const runId = '${{ steps.workflow.outputs.run_id }}'; | |
| const commitSha = '${{ steps.workflow.outputs.commit_sha }}'; | |
| const branch = '${{ steps.workflow.outputs.branch }}'; | |
| const triggeredBy = '${{ steps.workflow.outputs.triggered_by }}'; | |
| const title = `🔴 CI Failure: ${workflowName}`; | |
| const body = `## Workflow Failure Report | |
| **Workflow:** ${workflowName} | |
| **Run ID:** ${runId} | |
| **Branch:** \`${branch}\` | |
| **Commit:** \`${commitSha.substring(0, 7)}\` | |
| **Triggered by:** @${triggeredBy} | |
| **Failed at:** ${new Date().toISOString()} | |
| ### 🔗 Links | |
| - [View Failed Workflow Run](${workflowUrl}) | |
| - [View Commit](https://github.com/${{ github.repository }}/commit/${commitSha}) | |
| ### 📋 Next Steps | |
| 1. Review the workflow logs at the link above | |
| 2. Identify the failing step | |
| 3. Fix the issue locally | |
| 4. Push the fix and verify the workflow passes | |
| 5. Close this issue once resolved | |
| ### 🏷️ Labels | |
| This issue was automatically created by the CI/CD pipeline. | |
| --- | |
| **Automated Issue Created by:** [Create Issue on Failure Workflow](${{ github.server_url }}/${{ github.repository }}/actions/workflows/create-failure-issue.yml)`; | |
| await github.rest.issues.create({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| title: title, | |
| body: body, | |
| labels: ['ci-failure', 'automated', 'bug'] | |
| }); | |
| console.log(`✅ Created issue for workflow failure: ${workflowName}`); | |
| - name: Comment on existing issue | |
| if: steps.check_issue.outputs.existing_issue != '' | |
| uses: actions/github-script@v8 | |
| with: | |
| script: | | |
| const issueNumber = '${{ steps.check_issue.outputs.existing_issue }}'; | |
| const workflowUrl = '${{ steps.workflow.outputs.workflow_url }}'; | |
| const commitSha = '${{ steps.workflow.outputs.commit_sha }}'; | |
| const comment = `### 🔴 Another Failure Detected | |
| **New failure at:** ${new Date().toISOString()} | |
| **Commit:** \`${commitSha.substring(0, 7)}\` | |
| **Run:** [View Logs](${workflowUrl}) | |
| The workflow is still failing. Please review the latest run.`; | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: parseInt(issueNumber), | |
| body: comment | |
| }); | |
| console.log(`✅ Added comment to existing issue #${issueNumber}`); |