【问题标题】:get build status through github api通过 github api 获取构建状态
【发布时间】:2015-06-09 05:06:41
【问题描述】:

GitHub API 提供了很多功能,但是有没有办法检索提交的构建状态? GitHub UI 提供了来自我们配置的 CI 系统的信息,但是我看不到通过 API 暴露的这些信息?

【问题讨论】:

    标签: github github-api


    【解决方案1】:

    它不直接提供状态,而是提供给您create a status

    这意味着 CI 可以有一个最终构建步骤,以这种方式将状态发布到 GitHub 存储库。

    POST /repos/:owner/:repo/statuses/:sha
    

    例如:

    {
      "state": "success",
      "target_url": "https://example.com/build/status",
      "description": "The build succeeded!",
      "context": "continuous-integration/jenkins"
    }
    

    (对于给定的 SHA1)


    参见例如“Github Commit Status API with Bamboo from Atlassian”,其中:

    • ${bamboo.buildResultsUrl} 是 GitHub 提交的 SHA1:
    • <xxx> 是占位符值,可以替换为值,也可以将变量${var} 替换为shown here

    将这些作为脚本添加到您的计划中。

    • complete.sh:

        # specs and cukes results are stored in JUnit format under test-reports
        if (grep 'failures="[^0]"' test-reports/* || \
          grep 'errors="[^0]"' test-reports/*); then
          curl -H "Authorization: token <MY_TOKEN>" --request POST \
            --data '{"state": "failure", "description": "Failed!", \
            "target_url": "${bamboo.buildResultsUrl}"}' \
            https://api.github.com/repos/<USER>/<REPO>/statuses/${bamboo.repository.revision.number} > /dev/null
        else
          curl -H "Authorization: token <MY_TOKEN>" --request POST \
            --data '{"state": "success", "description": "Success!", \
            "target_url": "${bamboo.buildResultsUrl}"}' \
            https://api.github.com/repos/<USER>/<REPO>/statuses \
            /${bamboo.repository.revision.number} > /dev/null
        fi
      
    • pending.sh:

        curl -H "Authorization: token <MY_TOKEN>" --request POST \
          --data '{"state": "pending", "description": "Build is running", \
          "target_url": "${bamboo.buildResultsUrl}"}' \
          https://api.github.com/repos/<USER>/<REPO>/statuses/${bamboo.repository.revision.number} > /dev/null
      

    【讨论】:

    • 我不知道 ${bamboo.repository.revision.number} 最初是什么(它是 GitHub 提交 SHA),并且对 MY_TOKENUSERREPO 变量没有以$ 就像我在 bash 中习惯的那样。 This question 包含此答案的一个不那么复杂的 bash 示例。另外,目标 url 应该以 https:// 开头,所以 stackoverflow.com 作为目标 url 是不够的。 (但是&gt; dev/null 混淆了此类错误消息)。
    • @a.t.谢谢你。我已经编辑了这个 6 岁的答案,以考虑到您的评论及其非常好的观点。
    【解决方案2】:

    You can access the status for a particular ref

    GET https://api.github.com/repos/:owner/:repo/commits/:ref/statuses

    :ref 的值可以使用 SHA、分支名称或标签名称。

    【讨论】:

      猜你喜欢
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 2020-01-12
      • 1970-01-01
      • 2019-02-09
      • 2010-10-29
      • 2016-12-14
      相关资源
      最近更新 更多