有没有办法在我项目的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!\" }"

其他回答

编辑:

我不再使用这种方法,请使用其他答案之一。

更新:对于我们的具体情况,我最终做了什么:(上面的答案很棒-谢谢!)

因为我们的构建服务器不在互联网上,我们有一个脚本来发布构建状态到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文档、测试结果和测试覆盖率。

Jently更新你的Github提交状态(如上所述@vonc),不幸的是,他们还没有实现回购状态API

加上这个答案,也加上这个答案,他们已经回答了这个问题,但没有给我们一个关于这个问题的正确的直觉

我的直觉是这样的:

We need to add status to our GitHub commit, this status is based upon the results of our Jenkins build. To do this we need to give Jenkins access to the GitHub API not to the repo, we do this through OAuth we can create the token going to the GH settings -> Developer settings -> Personal access tokens then for public GH repo just check repo:status, and for private repo check the whole repo section After creating your access token you need to add it to ur Jenkins server: Copy and paste the access token to the GitHub plugin section settings, under ur Jenkins configurations Create a GH server it defaults to api.github.com and add the access token as a secret text credentials. The last step is to add a post build settings when U create ur pipeline.

资源:

以下是YT视频的一个部分,他在其中介绍了上述步骤。 阅读OAuth

关于设置Jenkins和GitHub的受保护分支。我使用的是Jenkins 2.6,以下是我让它工作的步骤:

在你的仓库的GitHub网页上:

进入“设置>分支”。 在保护分支下,单击 选择一个分支淹没菜单,并选择你想要的分支 设置为受保护的分支。 根据需要启用这些选项。

在Jenkins服务器上: (确保你已经安装了Git和GitHub插件)

进入“Manage Jenkins > Configure System”界面。 在GitHub下,将API URL设置为https://api.github.com。尽管这是默认值。 为凭证选择生成的令牌。如果您还没有生成令牌,请单击Advanced…然后在附加操作中,您可以将您的登录名和密码转换为令牌,并将其用作您的凭据。

此外,确保Jenkins使用的GitHub帐户是存储库的合作者。我设置了写权限级别。

希望这能有所帮助。

如果你在Jenkins上安装了Github插件,你可以在Post build操作中这样做: