GitHub Actions中如何设置全局失败处理任务?
实现CI/CD通用失败处理步骤的方案
针对你提到的重复编写失败处理步骤的问题,不同主流CI/CD工具都有对应的复用方案,无需在每个任务末尾重复复制代码:
GitHub Actions
方案1:封装为复合动作(Composite Action)
把通用失败步骤封装成可复用的动作,在每个任务的失败分支中调用:
- 创建动作配置文件
.github/actions/failure-handler/action.yml:
name: 'Failure Handler' runs: using: "composite" steps: - name: 发送Slack通知 run: | # 替换为你的Slack通知命令/脚本 curl -X POST -H "Content-Type: application/json" -d '{"text":"任务失败: ${{ github.job }}"}' ${{ secrets.SLACK_WEBHOOK }} shell: bash if: failure() - name: 评论PR uses: actions/github-script@v6 with: script: | // 替换为你的PR评论逻辑 await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, issue_number: context.issue.number, body: `⚠️ 任务 \`${context.job}\` 执行失败` }) if: failure() - name: 添加/移除标签 run: | # 替换为你的标签操作命令/脚本 gh pr edit ${{ github.event.pull_request.number }} --add-label "failed" --remove-label "passed" shell: bash env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} if: failure()
- 在各任务中引用该动作:
jobs: verify: runs-on: ubuntu-latest steps: - name: 拉取代码 uses: actions/checkout@v4 - name: 执行verify任务 run: npm run verify - name: 执行失败处理 uses: ./.github/actions/failure-handler if: failure() build: runs-on: ubuntu-latest steps: - name: 拉取代码 uses: actions/checkout@v4 - name: 执行build任务 run: npm run build - name: 执行失败处理 uses: ./.github/actions/failure-handler if: failure()
方案2:全局失败监听任务
通过needs依赖所有任务,当任意任务失败时自动触发统一的处理任务:
jobs: verify: runs-on: ubuntu-latest steps: - name: 拉取代码 uses: actions/checkout@v4 - name: 执行verify任务 run: npm run verify build: runs-on: ubuntu-latest steps: - name: 拉取代码 uses: actions/checkout@v4 - name: 执行build任务 run: npm run build # 其他任务:deploy-stage、deploy-prod... failure-handler: runs-on: ubuntu-latest needs: [verify, build, deploy-stage, deploy-prod] if: failure() steps: - name: 发送Slack通知 run: | curl -X POST -H "Content-Type: application/json" -d '{"text":"CI任务失败,请查看详情"}' ${{ secrets.SLACK_WEBHOOK }} - name: 评论PR uses: actions/github-script@v6 with: script: | await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, issue_number: context.issue.number, body: "⚠️ CI工作流中有任务执行失败,请检查" }) - name: 添加/移除标签 run: | gh pr edit ${{ github.event.pull_request.number }} --add-label "failed" --remove-label "passed" env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GitLab CI
使用extends关键字定义失败处理模板,各任务直接继承:
- 定义失败处理模板:
.failure_handler: after_script: - | # 发送Slack通知 curl -X POST -H 'Content-type: application/json' --data '{"text":"任务失败: '$CI_JOB_NAME'"}' $SLACK_WEBHOOK - | # 评论Merge Request curl --request POST --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID/notes" --data "body=⚠️ 任务 \`$CI_JOB_NAME\` 执行失败" - | # 添加/移除标签 curl --request PUT --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/merge_requests/$CI_MERGE_REQUEST_IID" --data "add_labels=failed&remove_labels=passed" when: on_failure
- 各任务继承模板:
verify: stage: test script: - npm run verify extends: .failure_handler build: stage: build script: - npm run build extends: .failure_handler # 其他任务:deploy-stage、deploy-prod...
Jenkins
通过共享库(Shared Libraries)封装通用逻辑,在Pipeline中调用:
- 创建共享库文件
vars/failureHandler.groovy:
def call() { sendSlackNotification() commentOnPR() manageLabels() } def sendSlackNotification() { slackSend channel: '#ci-alerts', message: "⚠️ 任务 ${env.JOB_NAME} 执行失败" } def commentOnPR() { githubComment comment: "⚠️ 任务 ${env.JOB_NAME} 在Pipeline中执行失败,请检查" } def manageLabels() { githubLabeler addLabels: ['failed'], removeLabels: ['passed'] }
- 在Jenkinsfile中调用:
pipeline { agent any stages { stage('Verify') { steps { sh 'npm run verify' } } stage('Build') { steps { sh 'npm run build' } } // 其他阶段:deploy-stage、deploy-prod... } post { failure { failureHandler() } } }
内容的提问来源于stack exchange,提问作者Kousha
相关产品推荐
相关产品推荐

