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
--output text --query authorizationData[].authorizationToken)
$myecs_securitytoken
tag=$(aws ecr list-images --repository-name
--output text --query imageIds[].imageTag)
imgflag=($tag)
buildnum=$(curl --user :uservalue$ http://:
/rest/api/latest/result/.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
--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