matsev
matsev

Reputation: 33759

Git SHA in CodeBuild?

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.

Upvotes: 5

Views: 3291

Answers (1)

Linghao Zhu
Linghao Zhu

Reputation: 736

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.

Upvotes: 5

Related Questions