5

The GitHub API provides a lot of functionality, but is there a way to retrieve the build status for a commit? The GitHub UI provides information from the CI system we have configured, but I can't see this information exposed through the API?

Nippysaurus
  • 20,110
  • 21
  • 77
  • 129

2 Answers2

5

It doesn't provide status directly, but offers you to create a status

That means the CI can have a final build step which publishes the status to GitHub repo that way.

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

For example:

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

(and that, for a given SHA1)


See for instance "Github Commit Status API with Bamboo from Atlassian", where:

  • ${bamboo.buildResultsUrl} is the GitHub commit SHA1:
  • <xxx> is a placeholder value, which can be replaced by a value, or a variable ${var} as shown here.

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
    
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I was unaware what the `${bamboo.repository.revision.number}` was initially (it's the GitHub commit SHA), and was a bit confused by the `MY_TOKEN`, `USER` and `REPO` variables not being started with a `$` as I am used to in bash. [This question](https://stackoverflow.com/questions/70410456/set-github-commit-build-status-from-bash-using-ssh) contains a less convoluted bash example of this answer. Also, the target url should start with `https://`, so `stackoverflow.com` as target url would not suffice. (However the `> dev/null` obfuscates such error messages). – a.t. Dec 19 '21 at 10:20
  • 1
    @a.t. Thank you. I have edited this 6 years old answer to take into account your comment and its very good points. – VonC Dec 19 '21 at 21:18
4

You can access the status for a particular ref

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

For the value of :ref, you can use a SHA, a branch name, or a tag name.

Wade Williams
  • 3,943
  • 1
  • 26
  • 35