4
votes

I have a pipeline which creates docker images and pushes it to ECR. Since I want to use the AWS provided build environments, I am using 2 build stages.

The pipeline has a total of 3 stages

  1. Get the source code from GitHub : Source
  2. Install dependencies and create a .war file : Build : aws/codebuild/java:openjdk-9
  3. Build the docker image and push it to ECR : Build : aws/codebuild/docker:17.09.0

I would like to tag the docker images with the commit ID which is usually CODEBUILD_RESOLVED_SOURCE_VERSION. However, I have noticed that this variable is only available in my second stage which is immediately after the source.

The worst case work around I found is to write this variable into a file in the second stage and include that file in the artifacts which is the input for the third stage.

Is there a better way to use this in my third stage or overall the pipeline?

3
Did you make any progress on this? I am having the same issue and the only thing I can think of is the file route you mention.edzillion

3 Answers

2
votes

Can you write the commit ID to a file that sits alongside the WAR file in the CodePipeline artifact?

And a couple related thoughts:

  • CodeBuild can be configured in CodePipeline to have multiple input artifacts, so I assume CODEBUILD_RESOLVED_SOURCE_VERSION refers to the primary artifact. I'm not sure how to generalize getting the commit ID into the third action (publish to ECR) because fan-in (multiple sources with a distinct commit id) can occur at both CodeBuild actions.
  • Tagging by commit ID means that multiple pipeline executions may produce an image with the same tag. Ideally I'd like each pipeline execution to be isolated so I don't have to worry about the tag being changed by concurrent pipeline executions or later to use a different dependency closure.
1
votes

I have managed to do something with jq and sponge as shown in this file buildspec.yaml

I modify my config.json file upon each commit and pass it on to the next stage.

0
votes

I am using a combination of codepipeline + jq. It's not the best approach, but it's the best I have so far.

commit=$(aws codepipeline get-pipeline-state --name PIPELINE_NAME | jq '.stageStates[0].actionStates[0].currentRevision.revisionId' | tr -d '"'))

and then push the docker image with the new tag. You need to install jq first, if you don't like jq, you can parse the response by yourself.

This 'may' be a duplicate of this other question