Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get build status through github api

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?

like image 404
Nippysaurus Avatar asked Feb 11 '23 12:02

Nippysaurus


2 Answers

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.

like image 183
Wade Williams Avatar answered Feb 14 '23 09:02

Wade Williams


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
    
like image 36
VonC Avatar answered Feb 14 '23 10:02

VonC