Last active
June 18, 2017 03:59
-
-
Save rcbop/0b28b10f44863974b25db5bd7225c870 to your computer and use it in GitHub Desktop.
Removes old aws ecr docker images that are not tagged keeping the latests given number (default 5)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# removes old ecr images keeping the latests given number | |
# do not removes images with tag | |
# mantainer rogerio.c.peixoto (rcbpeixoto@gmail.com) | |
##################################################### | |
RED='\033[0;31m' | |
GREEN='\033[0;32m' | |
YELLOW='\033[1;33m' | |
CYAN='\033[0;36m' | |
MAGENTA='\033[35m' | |
NC='\033[0m' | |
AWSARGS="--region us-east-1" | |
IMGS_TO_KEEP=5 | |
function printKeepImageWithTag() { | |
echo -e " $GREEN> keeping $YELLOW $1$GREEN\t> Created at $YELLOW $2 $GREEN Tag found! $3 $NC" | |
} | |
function printKeepImageWithoutTag(){ | |
echo -e " $GREEN> keeping $YELLOW $1$GREEN\t> Created at $YELLOW $2$NC" | |
} | |
function keepImage(){ | |
[ -z "$3" ] && printKeepImageWithoutTag $1 $2 || printKeepImageWithTag $1 $2 $3 | |
} | |
function deleteImage(){ | |
echo -e " $RED> deleting $YELLOW $1$RED\t> Created at $YELLOW $2 $NC" | |
aws ecr batch-delete-image --repository-name $repo --image-ids "imageDigest=$1" --output text $AWSARGS > /dev/null | |
} | |
function repoBanner(){ | |
echo -e "$CYAN[$repo]$NC" | |
echo "" | |
} | |
repo_list=$(aws ecr describe-repositories --output json $AWSARGS | jq -r '.repositories[].repositoryName') | |
for repo in $repo_list | |
do | |
repoBanner | |
total_counter=0 | |
deleted=0 | |
if [ "$(uname)"="Darwin" ]; then | |
sorted_list=$(aws ecr describe-images --repository-name $repo --output json $AWSARGS | jq ".imageDetails | map({ imgId: .imageDigest, date: (.imagePushedAt | strftime(\"%Y-%m-%d-%T-UTC%Z\")), tags: .imageTags }) | sort_by(.date)" | jq -cr ".[]" | tail -r) | |
else | |
sorted_list=$(aws ecr describe-images --repository-name $repo --output json $AWSARGS | jq ".imageDetails | map({ imgId: .imageDigest, date: (.imagePushedAt | strftime(\"%Y-%m-%d-%T-UTC%Z\")), tags: .imageTags }) | sort_by(.date)" | jq -cr ".[]" | tac) | |
fi | |
for img_json in $sorted_list | |
do | |
is_tag_present=$(echo $img_json | jq -cr '.tags//empty') | |
img_id=$(echo $img_json | jq -r '.imgId') | |
date=$(echo $img_json | jq '.date') | |
((total_counter++)) | |
if [ -z $is_tag_present ]; then | |
if [ $total_counter -gt $IMGS_TO_KEEP ]; then | |
((deleted++)) | |
deleteImage $img_id $date | |
else | |
keepImage $img_id $date | |
fi | |
else | |
keepImage $img_id $date $is_tag_present | |
fi | |
done | |
echo "" | |
[ $deleted -gt 0 ] && echo -e "$RED deleted $deleted images in repo [$repo]$NC" || echo -e "$GREEN Nothing to be deleted in repo [$repo]$NC" | |
echo -e "$MAGENTA>> total images $total_counter $NC" | |
echo "" | |
done | |
echo -e "$GREEN##### TASK COMPLETE #####$NC" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment