Last active
November 18, 2016 02:13
-
-
Save mlf4aiur/2ab96ebedfb725dd19f5e7f70077bcdf to your computer and use it in GitHub Desktop.
Cleanup AWS ECR untagged images
This file contains hidden or 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 | |
| function delete_image () { | |
| local repo_name=$1 | |
| local image_digest=$2 | |
| echo "${image_digest}" | |
| aws --out=json \ | |
| ecr \ | |
| batch-delete-image \ | |
| --repository-name="${repo_name}" \ | |
| --image-ids \ | |
| imageDigest="${image_digest}" | |
| } | |
| # Check requirements | |
| function require { | |
| command -v "$1" > /dev/null 2>&1 || { | |
| echo "Some of the required software is not installed:" | |
| echo " please install $1" >&2; | |
| exit 1; | |
| } | |
| } | |
| # Check for AWS, AWS Command Line Interface | |
| require aws | |
| # Check for jq, Command-line JSON processor | |
| require jq | |
| repositories=$(aws --out=json \ | |
| ecr \ | |
| describe-repositories | \ | |
| jq '.repositories[].repositoryName' | \ | |
| sed 's/"//g' | |
| ) | |
| for repo_name in ${repositories} | |
| do | |
| echo "${repo_name}" | |
| images=$(aws --out=json \ | |
| ecr \ | |
| list-images \ | |
| --repository-name="${repo_name}" | \ | |
| jq '.imageIds[] | select(.imageTag == null) | .imageDigest' | |
| ) | |
| # alternative | |
| # images=$(aws --out=json \ | |
| # ecr \ | |
| # list-images \ | |
| # --repository-name="${repo_name}" \ | |
| # --filter tagStatus=UNTAGGED | \ | |
| # jq '.imageIds[] | .imageDigest' | |
| # ) | |
| for image_digest in ${images} | |
| do | |
| delete_image "${repo_name}" "${image_digest}" | |
| done | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment