Wednesday, 14 June 2017

write a bash script which remove all the images from the ECR except the latest image

!/bin/bash $myecs_securitytoken=$(aws ecr get-authorization-token --region <region_name> --output text --query authorizationData[].authorizationToken) $myecs_securitytoken tag=$(aws ecr list-images --repository-name <repository_name> --output text --query imageIds[].imageTag) imgflag=($tag) buildnum=$(curl --user <user_name>:uservalue$ http://<IP_address>: <port_no>/rest/api/latest/result/<BambooPlan_name>.json |sed 's/"buildNumber":/&\n/;s/.\n//;s/}/\n&/;s/\n.//' ) n="0" while [ $n -lt ${#imgflag[@]} ] do if [ ${imgflag[n]} -eq $buildnum ]; then n=$[$n+1] fi delimg=${imgflag[n]} aws ecr batch-delete-image --repository-name <repository_name> --image-ids imageTag=$delimg echo "ecr imagetag:$delimg is deleted" n=$[$n+1] done note: The images are differentiate with the image tags as a Bamboo build number All the images tags are collected in imgflag and iterate one by one and delete the images using the image tag

No comments:

Post a Comment