通过 github api 获取构建状态

get build status through github api

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

暴露的信息

它不直接提供状态,但会为您提供 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}.

Add those to your plan as Script.

  • 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
    

You can access the status for a particular ref

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

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