有没有办法在我项目的GitHub Readme.md上显示Jenkins构建状态?
我使用Jenkins运行持续集成构建。在每次提交之后,它确保在最终生成文档和发布包之前,所有内容都已编译,并执行单元和集成测试。
仍然存在无意中提交一些破坏构建的内容的风险。对于访问GitHub项目页面的用户来说,知道当前主程序处于该状态是很好的。
有没有办法在我项目的GitHub Readme.md上显示Jenkins构建状态?
我使用Jenkins运行持续集成构建。在每次提交之后,它确保在最终生成文档和发布包之前,所有内容都已编译,并执行单元和集成测试。
仍然存在无意中提交一些破坏构建的内容的风险。对于访问GitHub项目页面的用户来说,知道当前主程序处于该状态是很好的。
当前回答
我所做的很简单:
Install the Hudson Post Task Plugin Create a Personal Access Token here : https://github.com/settings/tokens Add a Post Task Plugin that always put success curl -XPOST -H "Authorization: token OAUTH TOKEN" https://api.github.com/repos/:organization/:repos/statuses/$(git rev-parse HEAD) -d "{ \"state\": \"success\", \"target_url\": \"${BUILD_URL}\", \"description\": \"The build has succeeded!\" }" Add a Post Task Plugin that will put failure if "marked build as failure" curl -XPOST -H "Authorization: token OAUTH TOKEN" https://api.github.com/repos/:organization/:repos/statuses/$(git rev-parse HEAD) -d "{ \"state\": \"failure\", \"target_url\": \"${BUILD_URL}\", \"description\": \"The build has failed!\" }" You can also add a call to pending at the beginning of tests curl -XPOST -H "Authorization: token OAUTH TOKEN" https://api.github.com/repos/:organization/:repos/statuses/$(git rev-parse HEAD) -d "{ \"state\": \"pending\", \"target_url\": \"${BUILD_URL}\", \"description\": \"The build is pending!\" }"
其他回答
与此同时,Jenkins和GitHub的UI发生了一些变化,我花了一段时间才弄清楚如何正确配置Jenkins。这里的解释基于Jenkins版本2.121.1。
我还假设你已经配置了你的Jenkins作业由网络钩子或轮询触发。 以下是我为让它运转起来所采取的步骤:
配置Github:创建个人访问令牌与OAuth范围repo:状态 配置Jenkins:配置系统并添加OAuth秘密作为GitHub服务器-使用秘密文本作为身份验证方法,将OAuth秘密放在那里。 配置你的Jenkins Job:添加设置GitHub提交状态为Post-build action。将“状态结果”设置为默认消息和状态之一。 在GitHub上检查你的结果:检查你是否在GitHub提交上获得了构建状态和构建执行持续时间。
配置Github
配置詹金斯
配置Jenkins Job
结果
你现在会看到你的提交和分支的状态:
对于管道项目,你可以像这样使用post部分:
void setBuildStatus(String message, String state) {
step([
$class: "GitHubCommitStatusSetter",
reposSource: [$class: "ManuallyEnteredRepositorySource", url: "https://github.com/my-user/my-repo"],
contextSource: [$class: "ManuallyEnteredCommitContextSource", context: "ci/jenkins/build-status"],
errorHandlers: [[$class: "ChangingBuildStatusErrorHandler", result: "UNSTABLE"]],
statusResultSource: [ $class: "ConditionalStatusResultSource", results: [[$class: "AnyBuildResult", message: message, state: state]] ]
]);
}
pipeline {
agent any
triggers {
githubPush()
}
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
post {
success {
setBuildStatus("Build succeeded", "SUCCESS");
}
failure {
setBuildStatus("Build failed", "FAILURE");
}
}
}
只需改变“https://github.com/my-user/my-repo”,以满足你的github回购。
参考资料:https://stackoverflow.com/a/47162309/4261333
我所做的很简单:
Install the Hudson Post Task Plugin Create a Personal Access Token here : https://github.com/settings/tokens Add a Post Task Plugin that always put success curl -XPOST -H "Authorization: token OAUTH TOKEN" https://api.github.com/repos/:organization/:repos/statuses/$(git rev-parse HEAD) -d "{ \"state\": \"success\", \"target_url\": \"${BUILD_URL}\", \"description\": \"The build has succeeded!\" }" Add a Post Task Plugin that will put failure if "marked build as failure" curl -XPOST -H "Authorization: token OAUTH TOKEN" https://api.github.com/repos/:organization/:repos/statuses/$(git rev-parse HEAD) -d "{ \"state\": \"failure\", \"target_url\": \"${BUILD_URL}\", \"description\": \"The build has failed!\" }" You can also add a call to pending at the beginning of tests curl -XPOST -H "Authorization: token OAUTH TOKEN" https://api.github.com/repos/:organization/:repos/statuses/$(git rev-parse HEAD) -d "{ \"state\": \"pending\", \"target_url\": \"${BUILD_URL}\", \"description\": \"The build is pending!\" }"
如果你在Jenkins上安装了Github插件,你可以在Post build操作中这样做:
编辑:
我不再使用这种方法,请使用其他答案之一。
更新:对于我们的具体情况,我最终做了什么:(上面的答案很棒-谢谢!)
因为我们的构建服务器不在互联网上,我们有一个脚本来发布构建状态到github中的gh-pages分支。
开始构建戳记失败 构建结束,戳记成功 项目在主项目之后运行,以发布结果——>构建状态、API文档、测试报告和测试覆盖率。
GitHub缓存图像,因此我们创建了.htaccess文件,该文件指示构建状态图像有一个短的缓存超时。
把这个和build-status镜像放在一起:
ExpiresByType image/png "access plus 2 minutes"
下面是构建脚本。发布到gh-page的目标是'——publish.site.dry.run'
在少于400行的配置中,我们有:
编译检查 单元和集成测试 测试报告 代码覆盖率报告 API文档 发布到Github
。。这个脚本可以在Jenkins内部或外部运行,这样:
开发人员可以在提交之前运行这个脚本,从而减少了错误构建影响其他构建的可能性。 故障很容易在本地重现。
结果:
项目主页有构建状态,每次构建后都会更新,还有最新的API文档、测试结果和测试覆盖率。