5

How can I get the Git SHA of a commit from a CodeBuild build when the source code is managed by CodeCommit?

Like any other Git repository, each CodeCommit commit gets a unique Git SHA. When CodeBuild executes a buildspec.yml the Git SHA can be read from the CODEBUILD_RESOLVED_SOURCE_VERSION environment variable.

However, I would like to know the Git SHA from "outside" of CodeBuild, i.e. in a Lambda function that is triggered by CodeBuild events of "detail-type": "CodeBuild Build State Change". Unfortunately, if you read the documentation you will find that the Git SHA is not part of the event data. I have also tried calling the CodeBuild.batchGetBuilds() function (since I know the build id from the CodeBuild event), regrettably there is no Git SHA in that response either.

1 Answer 1

5

Thank you for using AWS CodeBuild. For builds set with git based sources, the Git SHA can be found in resolvedSourceVersion field in CodeBuild.batchGetBuilds() output, or the CODEBUILD_RESOLVED_SOURCE_VERSION environment variable during the build. The resolvedSourceVersion in the API output is only available after the DOWNLOAD_SOURCE phase. CodeBuild released this change to AWS JavaScript SDK in version 2.328.0 a week ago.

You can post this on the AWS forum so we can better troubleshoot the issue for you.

Thank you.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that was timely.
Hi, the CODEBUILD_RESOLVED_SOURCE_VERSION is for the first/primary source, the git commit ids for other sources appears to not be available from the build environment?

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.