6

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?

2 Answers 2

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
    
Sign up to request clarification or add additional context in comments.

2 Comments

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 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. Thank you. I have edited this 6 years old answer to take into account your comment and its very good points.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.