Skip to content

Instantly share code, notes, and snippets.

@WesselAtWork
Forked from sportebois/ecr-add-tag.sh
Last active October 19, 2022 11:31
Show Gist options
  • Select an option

  • Save WesselAtWork/476cbdc0fff554e513629bec0c406548 to your computer and use it in GitHub Desktop.

Select an option

Save WesselAtWork/476cbdc0fff554e513629bec0c406548 to your computer and use it in GitHub Desktop.
Add tag to a docker image in ECR via AWS CLI
#!/usr/bin/env bash
function ecr-add-tag() {
if (( $# < 3 )); then
echo "Wrong number of arguments. Usage: ecr-add-tag ECR_REPO_NAME TAG_TO_FIND TAG_TO_ADD [AWS_PROFILE] [AWS_REGION]"
return
fi
local repo_name=$1
local existing_tag=$2
local new_tag=$3
local profile=$4
local region=$5
[[ ! -z "$profile" ]] && profile="--profile ${profile}"
[[ ! -z "$region" ]] && region="--region ${region}"
manifest=`aws ecr batch-get-image ${profile} ${region} \
--repository-name $repo_name \
--image-ids imageTag=$existing_tag \
--query 'images[].imageManifest' \
--output text`
aws ecr put-image ${profile} ${region} \
--repository-name $repo_name \
--image-tag $new_tag \
--image-manifest "${manifest}"
}
@WesselAtWork
Copy link
Copy Markdown
Author

Added region argument because I needed that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment