I have created an Xcode Bot that integrates on each commit.
In the "Run Script" Trigger I would like to update the current GitHub commit with the integration status of Tests that were run. Pretty standard CI stuff.
I'll then be running a script like the below:
curl -i -X POST -H "Content-type: application/json"
-H 'Authorization: token TOKEN_HERE' -d
'{
"state": "success",
"target_url": "https://example.com/build/status",
"description": "The build succeeded!",
"context": "continuous-integration/jenkins"
}'
https://api.github.com/repos/ORGANIZATION_HERE/REPO_HERE/statuses/SHA_HERE
It looks like I'll be able to get the success
or failure
states from the Xcode Bot Environment variables:
Access build folder in Xcode Server CI bot run (env variables?)
However, the SHA of the current commit is not listed. How am I able to get the SHA of the commit used for the Integration at this point, to be used in the GitHub Status API request?
I am using this code in my Xcode Bot triggers to get the SHA of the commit:
git -C ${XCS_SOURCE_DIR}/name_of_your_git_repo rev-parse HEAD
And this to get the branch name:
git -C ${XCS_SOURCE_DIR}/name_of_your_git_repo rev-parse --abbrev-ref HEAD
This executes a git command in the source directory, replace "name_of_your_git_repo" by the name of your repository on git
XCS_OUTPUT_DIR
has a file called sourceControl.log
. This file has logs like the following:
"DVTSourceControlLocationRevisionKey" : "3787c0d9e5107861a8b8d4c7300b2d414ad41dbb",
You can parse that log to find the SHA.
Perhaps more practically, CaveJohnson can pull the SHA:
PATH=/Library/Frameworks/Python.framework/Versions/3.4/bin:$PATH
SHA=`cavejohnson getSha`
Or it can just go ahead and set the GitHub status as a one-liner:
#!/bin/bash
PATH=/Library/Frameworks/Python.framework/Versions/3.4/bin:$PATH
cavejohnson setGithubStatus
Notably, there are more statuses than just success and failure, there are at least 6 that I'm aware of. You can read more about them in my Xcode 6 CI Missing Manual.
Using the cavejohnson
code in the other answer, which gets the hash from certain keys an Xcode log, I ran into an issue where the returned hash was an outdated one from the last build.
I'm now instead using git rev-parse HEAD
to get the hash of the commit that was actually used in the CI build. I've submitted this as a revision to cavejohnson
.
Use get_sha()
to retrieve the SHA-1 hash:
def get_sha():
return get_repo_sha(get_git_directory())
def get_git_directory():
for subdir in os.listdir('.'):
if is_git_directory(subdir):
return subdir
assert False
def is_git_directory(path = '.'):
return subprocess.call(['git', '-C', path, 'status'], stderr=subprocess.STDOUT, stdout = open(os.devnull, 'w')) == 0
def get_repo_sha(repo):
sha = subprocess.check_output(['git', 'rev-parse', 'HEAD'], cwd=repo).decode('ascii').strip()
return sha
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With