Cleaning up all the Docker images from the ECR and keeping only the latest Docker image and the latest Docker image tag number is taken from the Bamboo build number.
#!/bin/bash
#myecs_securitytoken=$(aws ecr get-authorization-token --region us-AZregion --output text --query authorizationData[].authorizationToken)
#$myecs_securitytoken
tag=$(aws ecr list-images --repository-name appname --output text --query imageIds[].imageTag)
imgflag=($tag)
buildnum=$(curl --user robot_user:xxxxx99$ http://ipaddress:portnumber/rest/api/latest/result/appname-DB.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 docker-appname --image-ids imageTag=$delimg
echo "ecr imagetag:$delimg is deleted"
n=$[$n+1]
done
Cleaning up all the taskdefinitions and keeping only the latest taskdefinition in the ECS (Ec2 container service).
#!/bin/bash
rev_arry=$(aws ecs list-task-definitions --family-prefix appname --query taskDefinitionArns[]|cut -f7 -d :| cut -c1-2)
maxrev_num=($rev_arry)
arrlen=$((${#maxrev_num[@]}-2))
latest_revision=$(aws ecs list-task-definitions --family appname --sort desc --max-items 1 --query taskDefinitionArns|cut -c64-65)
n="1"
while [ $n -lt $arrlen ]
do
if [ ${maxrev_num[n]} -eq $latest_revision ];
then
n=$[$n+1]
fi
delrevision=${maxrev_num[n]}
aws ecs deregister-task-definition --task-definition appname:$delrevision
echo "ecr taskdefiniton:$delrevision is deleted"
n=$[$n+1]
done
No comments:
Post a Comment