You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Jenkins Pipeline创建并触发GitHub自定义测试检查?

Jenkins Pipeline 触发GitHub自定义检查的可行方案

前置准备

  • 生成GitHub个人访问令牌(PAT),需包含repo:status和checks:write权限
  • Jenkins安装GitHub Integration和Pipeline插件
  • 在Jenkins中添加全局凭据,类型选「Secret text」,填入PAT,凭据ID设为github-checks-token(可自定义)

Pipeline 实现代码

直接通过GitHub Checks API调用,无需依赖第三方插件,避免插件兼容问题:

pipeline {
    agent any
    environment {
        // 从Jenkins凭据中获取GitHub令牌
        GITHUB_TOKEN = credentials('github-checks-token')
        // 替换为你的GitHub用户名/组织名、仓库名
        REPO_OWNER = 'your-org-or-username'
        REPO_NAME = 'your-repo-name'
        // PR触发时Jenkins自动注入的变量,非PR场景可手动指定HEAD_SHA
        HEAD_SHA = env.GIT_COMMIT
    }
    stages {
        stage("tests") {
            steps {
                script {
                    // 1. 初始化检查状态为「运行中」
                    sh """
                        curl -X POST \
                        -H "Authorization: token ${GITHUB_TOKEN}" \
                        -H "Accept: application/vnd.github.v3+json" \
                        https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/check-runs \
                        -d '{
                            "name": "自定义测试检查",
                            "head_sha": "${HEAD_SHA}",
                            "status": "in_progress",
                            "started_at": "'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"
                        }'
                    """

                    // 2. 执行你的测试逻辑
                    sh "echo '执行测试用例...'"
                    // 替换为实际测试命令,比如:sh "mvn test" / sh "pytest"
                    def testExitCode = sh script: "你的测试命令", returnStatus: true

                    // 3. 根据测试结果更新检查状态
                    def conclusion = testExitCode == 0 ? "success" : "failure"
                    def title = testExitCode == 0 ? "测试通过" : "测试失败"
                    def summary = testExitCode == 0 ? "所有测试用例执行成功" : "存在未通过的测试用例"

                    sh """
                        curl -X POST \
                        -H "Authorization: token ${GITHUB_TOKEN}" \
                        -H "Accept: application/vnd.github.v3+json" \
                        https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/check-runs \
                        -d '{
                            "name": "自定义测试检查",
                            "head_sha": "${HEAD_SHA}",
                            "status": "completed",
                            "conclusion": "${conclusion}",
                            "completed_at": "'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'",
                            "output": {
                                "title": "${title}",
                                "summary": "${summary}",
                                "text": "测试执行详情:\\n- 退出码:${testExitCode}"
                            }
                        }'
                    """
                }
            }
        }
    }
}

关键注意事项

  • 如果Pipeline不是PR触发,需手动指定HEAD_SHA为当前提交的哈希值
  • 确保Jenkins服务器能正常访问GitHub API(无网络拦截)
  • 可自定义检查名称、输出内容,匹配项目测试场景
  • 若需避免shell依赖,可改用Groovy的HttpBuilder调用API

内容的提问来源于stack exchange,提问作者Itai Goldman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 13:55:21