How to get SHA commit for Xcode Bot “Run Script” trigger? Updating the status of tests on Github

I created an Xcode bonus that integrates into every commit.

In the "Run Script" trigger, I want to update the current GitHub transaction with the integration status of the tests being tested. Pretty standard CI stuff.

Xcode Bot Run Script

Then I will run the script as shown 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 can get success or failure states from Bot Xcode environment variables:

Access build folder in Xcode Server CI bot run (env variables?)

However, the current commit SHA is not specified. How can I get the SHA commit used for integration at this point for use in a GitHub API request?

+5
source share
3 answers

XCS_OUTPUT_DIR has a file called sourceControl.log . This file has the following logs:

 "DVTSourceControlLocationRevisionKey" : "3787c0d9e5107861a8b8d4c7300b2d414ad41dbb", 

You can analyze this log to find SHA.

Perhaps more practical, CaveJohnson can pull out the SHA:

 PATH=/Library/Frameworks/Python.framework/Versions/3.4/bin:$PATH SHA=`cavejohnson getSha` 

Or he can just go and set GitHub status as single-line:

 #!/bin/bash PATH=/Library/Frameworks/Python.framework/Versions/3.4/bin:$PATH cavejohnson setGithubStatus 

It is noteworthy that there are more statuses than just success and failure, there are at least 6 that I know of. You can learn more about them in the Xcode 6 CI Missing Manual .

+1
source

Using the cavejohnson code in another answer that receives a hash from certain keys, the Xcode log, I ran into a problem in which the returned hash was deprecated from the last build .

Now I use git rev-parse HEAD to get the commit hash that was actually used in the CI assembly. I presented this as a revision on 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 
+1
source

I use this code in my Bot Xcode triggers to get the SHA commit:

 git -C ${XCS_SOURCE_DIR}/name_of_your_git_repo rev-parse HEAD 

And to get the name of the branch:

 git -C ${XCS_SOURCE_DIR}/name_of_your_git_repo rev-parse --abbrev-ref HEAD 

Run the git command in the source directory, replace the name "name_of_your_git_repo" with the name of your repository with git

+1
source

Source: https://habr.com/ru/post/1213772/


All Articles