4

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.

matsev
  • 32,104
  • 16
  • 121
  • 156

1 Answers1

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.

Linghao Zhu
  • 661
  • 5
  • 3
  • Thanks, that was timely. – matsev Oct 15 '18 at 10:36
  • 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? – Aaron Jun 02 '19 at 00:50