首页 文章

在GitHub repo上显示Jenkins的当前状态

提问于
浏览
150

有没有办法在我的项目的GitHub Readme.md上显示Jenkins构建状态?

我使用Jenkins运行持续集成构建 . 在每次提交之后,它确保在最终生成文档和发行包之前编译所有内容,以及执行单元和集成测试 .

仍然存在无意中犯下破坏构建的东西的风险 . 访问GitHub项目页面的用户知道当前主服务器处于该状态会很好 .

11 回答

  • 2

    好的,这里是你如何设置Jenkins来设置GitHub构建状态 . 这假设你已经让Jenkins使用GitHub插件配置为在每次推送时进行构建 .

    • 转到GitHub,登录,转到 SettingsPersonal access tokens ,单击 Generate new token .

    screenshot of GitHub settings

    • 检查 repo:status (我不确定这是否必要,但我做到了,它对我有用) .

    screenshot of GitHub token generation

    • 生成令牌,复制它 .

    • 确保您要使用的GitHub用户是存储库协作者(用于私有存储库),或者是具有推送和拉取访问权限(用于组织存储库)到您要构建的存储库的团队的成员 .

    • 转到Jenkins服务器,登录 .

    • Manage JenkinsConfigure System

    • GitHub Web Hook 选择 Let Jenkins auto-manage hook URLs ,然后指定您在步骤3中获得的GitHub usernameOAuth token .

    screenshot of Jenkins global settings

    • 确认它适用于 Test Credential 按钮 . Save 设置 .

    • 找到Jenkins作业并将 Set build status on GitHub commit 添加到后构建步骤

    screenshot of Jenkins job configuration

    而已 . 现在进行测试构建并转到GitHub存储库以查看它是否有效 . 单击主存储库页面中的 Branches 以查看构建状态 .

    sceenshot of the main page where you click on 'branches'

    您应该看到绿色复选标记:

    screenshot of GitHub branches with build status

  • 1

    我做的很简单:

    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!\"
    }"
    
    • 添加一个帖子任务插件,如果“标记为构建失败”将导致失败
    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!\"
    }"
    
    • 您还可以在测试开始时添加对待处理的呼叫
    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!\"
    }"
    

    Screenshot of the Post build task configuration

  • 35

    这个插件应该工作:https://wiki.jenkins-ci.org/display/JENKINS/Embeddable+Build+Status+Plugin

    您应该能够将这样的徽章嵌入到 README.md 文件中:

    build passing

  • 7

    与此同时,Jenkins和GitHub的UI发生了一些变化,我花了一段时间才弄清楚如何正确配置Jenkins . 这里的解释基于Jenkins版本2.121.1 .

    我还假设您已经配置了由webhook或轮询触发的Jenkins作业 . 这些是我为实现它而采取的步骤:

    • 配置Github:使用OAuth范围创建个人访问令牌 repo:status

    • 配置Jenkins: Configure System 并将OAuth Secret添加为GitHub服务器 - 使用 Secret Text 作为身份验证方法将OAuth Secret置于其中 .

    • 配置Jenkins作业:添加 Set GitHub commit status 作为构建后操作 . 将状态结果设置为 One of the default messages and statuses .

    • 在GitHub上检查您的结果:检查您是否获得了GitHub提交的构建状态和构建执行持续时间 .

    配置Github

    Create Personal Access Token


    enter image description here


    enter image description here


    enter image description here


    配置Jenkins

    enter image description here


    enter image description here


    enter image description here


    enter image description here


    enter image description here


    配置Jenkins Job

    enter image description here


    enter image description here


    enter image description here


    结果

    您现在将看到提交和分支的状态:

    enter image description here

  • 144

    Commit Status API允许您查看“Repo Statuses API” .

    自2013年4月26日起,您现在可以在GitHub repo branch page上看到build status

    build status on GitHub repo branches

    这意味着通过访问GitHub项目页面来查看这些状态而不是只有Jenkins是另一种方式 .

    从2013年4月30日开始,API endpoint for commit statuses已扩展为允许分支 and tag names, as well as commit SHAs .

  • 1

    还有这个插件会给你一个徽章网址,你可以在README.md中发布,看起来像这样

    build passing

    https://wiki.jenkins-ci.org/display/JENKINS/Embeddable+Build+Status+Plugin

  • 11

    如果您在 Jenkins 上安装了 Github 插件,则可以在 Post build actions 中执行此操作:

    set build status on github

  • 3

    关于设置Jenkins和GitHub的受保护分支 . 我正在使用Jenkins 2.6,这些是我为使其工作所采取的步骤:

    在您的存储库的GitHub网页上:

    • 导航到“设置”>“分支” .

    • 在“保护分支”下,单击“选择a”分支淹没菜单并选择要设置为受保护分支的分支 .

    • 根据需要启用选项 .

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

    • 导航到管理Jenkins>配置系统 .

    • 在GitHub下,将API URL设置为https://api.github.com . 虽然这是默认值 .

    • 为凭据选择生成的令牌 . 如果您尚未生成令牌,请单击高级...然后在其他操作上,您可以将您的登录名和密码转换为令牌并将其用作您的凭据 .

    此外,请确保您的Jenkins正在使用的GitHub帐户是存储库的协作者 . 我已经设置了写权限级别 .

    希望这可以帮助 .

  • 20

    README.md 中添加以下行,并根据您的jenkins项目更改这两个URL .

    [![Build Status](https://jenkins../..project/lastBuild/buildStatus)](https://jenkins../..project/lastBuild/)
    
  • 16

    Jently更新你的Github commit status(如上面@vonc所述),不幸的是他们还没有实现Repo Status API

  • 9

    Edit:

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

    Update: what I ended up doing, for our specific case: (以上答案很棒 - 谢谢!)

    因为我们的构建服务器不在Internet上,所以我们有一个脚本将构建状态发布到github中的gh-pages分支 .

    • 开始构建标记失败

    • 结束标记成功

    • 项目在主项目之后运行以发布结果 - >构建状态,API文档,测试报告和测试覆盖率 .

    GitHub缓存图像,因此我们创建了.htaccess文件,它指示构建状态图像的短缓存超时 .

    Put this in the directory with the build-status image:

    ExpiresByType image/png "access plus 2 minutes"
    

    Here's构建脚本 . 发布到gh-pages的目标是 '--publish.site.dry.run'

    配置少于400行,我们有:

    • 编译检查

    • 单元和集成测试

    • 测试报告

    • 代码覆盖率报告

    • API文档

    • 发布到Github

    . . and this script can be run in or outside of Jenkins, so that:

    • 开发人员可以在提交之前运行此脚本,从而减少构建破坏影响其他人的可能性 .

    • 失败很容易在本地重现 .

    The Results:

    Project main page具有构建状态,在每次构建后更新,以及最新的API文档,测试结果和测试覆盖率 .

相关问题