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.