1 Answer
- Newest
- Most votes
- Most comments
0
Hello there,
You can use CodeBuild to auto modify the Dockerrun.aws.json
to reference to the latest image tag dynamically. In your CodeBuild project buildspec.yml
, you can utilize the predefined CODEBUILD_RESOLVED_SOURCE_VERSION
envrionment variable to retrieve commit ID, passing it as image tag, for example:
version: 0.2 phases: pre_build: commands: - echo Logging in to Amazon ECR... - aws --version - $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email) - REPOSITORY_URI=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/web - IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7) build: commands: - echo Build started on `date` - echo Building the Docker image... - docker build -t $REPOSITORY_URI:latest . - docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG post_build: commands: - echo Build completed on `date` - echo Pushing the Docker images... - docker push $REPOSITORY_URI:latest - docker push $REPOSITORY_URI:$IMAGE_TAG artifacts: files: - 'Dockerrun.aws.json'
$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
gives you the first seven characters of the Git commit ID of the source.
In Dockerrun.aws.json
, you can define something like below:
{ "containerDefinitions": [ { "name": "web", "image": "{$AWS_ACCOUNT_ID}.dkr.ecr.{$AWS_DEFAULT_REGION}.amazonaws.com/web:{$IMAGE_TAG}", "hostname": "web", "essential": true, "memory": 1024, "portMappings": [ { "hostPort": 80, "containerPort": 80 } ] } ] ... }
CodeBuild and CopdePipeline will update image tag and Dockerrun.aws.json
dynamically whenever you push code change to source repo.
Relevant content
- asked 9 months ago
- asked 3 months ago
- Accepted Answerasked 7 months ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 6 months ago
- AWS OFFICIALUpdated 4 months ago
- AWS OFFICIALUpdated 10 months ago
Zhiyuan_L, Thank you for this information.
There is one aspect on which I am not clear. probably because I am learning CodeBuild: I understand from your response that CodeBuild will do the transformation I am after. so: Creates the Dockerrun.aws.json and I would place Dockerrun.aws.json in the beanstalk artifact? I have a Zip file with Dockerfile/ebextensions ect,
In code build how would I use the Dockerrun.aws.json that was produced?
Just making sure I am connecting the dots here.
I'll look at the documentation to find where it discusses these types of transformations!
Thanks!