I am using CodePipeline
with CodeCommit
. Builds are triggered automatically with push to master
branch. In CodePipeline
console it is clearly visible that i am receiving commit id but i need to get it in the build environment so i can add them as a tag to the ECS image when i build it. Is there a way to get in in build environment.

- 263
- 1
- 3
- 15
5 Answers
You can use the CODEBUILD_RESOLVED_SOURCE_VERSION
environment variable to retrieve the commit hash displayed in CodePipeline at build time.

- 9,640
- 4
- 43
- 72
-
CODEBUILD_RESOLVED_SOURCE_VERSION is the commit sha. – Andy Hayden Jan 18 '18 at 20:32
-
1I had assumed this variable was only available when codebuild pulls the source itself. However even if it's source is CODEPIPELINE, this variable still works. – Yep_It's_Me Apr 15 '21 at 01:20
-
Alas only for the primary source, however. – Phil Nov 03 '22 at 20:51
-
@Unsigned that's a S3 version identifier, not the corresponding git commit sha. – Phil Dec 03 '22 at 07:18
Adding an answer that explains how to achieve this in CloudFormation, as it took me a while to figure it out. You need to define your stage as:
Name: MyStageName
Actions:
-
Name: StageName
InputArtifacts:
- Name: InputArtifact
ActionTypeId:
Category: Build
Owner: AWS
Version: '1'
Provider: CodeBuild
OutputArtifacts:
- Name: OutputArtifact
Configuration:
ProjectName: !Ref MyBuildProject
EnvironmentVariables:
'[{"name":"COMMIT_ID","value":"#{SourceVariables.CommitId}","type":"PLAINTEXT"}]'
In your actions you need to have this kind of syntax. Note that the EnvironmentVariables
property of a CodePipeline stage is different from a AWS::CodeBuild::Project
property. If you were to add #{SourceVariables.CommitId}
as an env variable there, it wouldn't be resolved properly.
-
Thanks for pointing the `AWS::CodeBuild::Project` vs `AWS::CodePipeline::Action` distinction, that was very helpful! – zessx Dec 15 '22 at 19:18
CodePipeline now also allows you to configure your pipeline with variables that are generated at execution time. In this example your CodeCommit action will produce a variable called CommitId that you can pass into a CodeBuild environment variable via the CodeBuild action configuration.
Here is a conceptual overview of the feature: https://docs.aws.amazon.com/codepipeline/latest/userguide/reference-variables.html
For an example walk through of passing the commit id into your build action you can go here: https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-variables.html
It would also be worth considering tagging the image with the CodePipeline execution id instead of the commit id, that way it prevents future builds with the same commit from overwriting the image. Using the CodePipeline execution id is also shown in the example above.

- 83
- 5
Is this what you're looking for?
Most (if not all) of the language SDKs have this API built in also.

- 2,016
- 14
- 16
Additionally to @Bar's answer: just adding EnvironmentVariables
is not enough, you need to set Namespace
also.
For example:
pipeBackEnd:
Type: AWS::CodePipeline::Pipeline
Properties:
...
Stages:
- Name: GitSource
Actions:
- Name: CodeSource
ActionTypeId:
Category: Source
...
Configuration: (...)
Namespace: SourceVariables # <<< === HERE, in Source
- Name: Deploy
Actions:
- Name: BackEnd-Deploy
ActionTypeId:
Category: Build
Provider: CodeBuild (...)
Configuration:
ProjectName: !Ref CodeBuildBackEnd
EnvironmentVariables: '[{"name":"BranchName","value":"#{SourceVariables.BranchName}","type":"PLAINTEXT"},{"name":"CommitMessage","value":"#{SourceVariables.CommitMessage}","type":"PLAINTEXT"}]'
Also, it may be useful: list of CodePipeline variables

- 5,925
- 7
- 38
- 58