How to add version number for image tag in Code Build buildsec.yml

Viewed 394

I have a pipeline to take code from code commit and build the image using code build and upload the image to ECR repo. Currently, I am passing hardcoded value for Image Tag as "latest" in the buildsec.yml file. Please let me know how to specify versions instead of the latest in the build file.

buildsec.yml file

version: 0.2

env: variables: AWS_ACCOUNT_ID: "XXXXXX" AWS_DEFAULT_REGION: "XXXXX" IMAGE_REPO_NAME: "XXXXXXX" IMAGE_TAG: "latest"

phases: install: runtime-versions: docker: 18 pre_build: commands: - echo Logging in to Amazon ECR... - $(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION) build: commands: - echo Build started on date - echo Building the Docker image... - docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG . - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG post_build: commands: - echo Build completed on date - echo Pushing the Docker image... - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG

1 Answers

You can try putting the version in a special file. And use it in your pipeline:

export IMAGE_TAG=$(cat ./version.txt)

or generate random tag

export IMAGE_TAG=$(openssl rand -hex 4)

or specify tag as Environment Variable in CodeBuild via AWS Management Console (worst variant)

Related