Skip to content

Instantly share code, notes, and snippets.

@chicagobuss
Created December 14, 2017 16:53
Show Gist options
  • Save chicagobuss/d17cf716e67d096277fb8b0350f4c1d7 to your computer and use it in GitHub Desktop.
Save chicagobuss/d17cf716e67d096277fb8b0350f4c1d7 to your computer and use it in GitHub Desktop.
a sane ecr lifecycle policy
#!/bin/bash
LIFECYCLE_POLICY='{"rules":[{"rulePriority":10,"description":"keeps 50 latest tagged images","selection":{"tagStatus":"tagged","countType":"imageCountMoreThan","countNumber":50,"tagPrefixList":["v"]},"action":{"type":"expire"}},{"rulePriority":20,"description":"keeps 5 latest untagged images","selection":{"tagStatus":"untagged","countType":"imageCountMoreThan","countNumber":5},"action":{"type":"expire"}},{"rulePriority":30,"description":"keeps latest 20 numeric-tagged images","selection":{"tagStatus":"tagged","countType":"imageCountMoreThan","tagPrefixList":["0","1","2","3","4","5","6","7","8","9"],"countNumber":20},"action":{"type":"expire"}},{"rulePriority":40,"description":"keeps latest 20 a-f tagged images","selection":{"tagStatus":"tagged","countType":"imageCountMoreThan","tagPrefixList":["a","b","c","d","e","f"],"countNumber":20},"action":{"type":"expire"}}]}'
aws ecr put-lifecycle-policy --region ${AWS_REGION} --repository-name ${REPO} --lifecycle-policy-text ${LIFECYCLE_POLICY} || echo "Failed to put lifecycle policy on ${REPO} repo"
@ronkot
Copy link

ronkot commented Mar 29, 2018

Looks sane, but probably doesn't work as expected because each of the items in tagPrefixList have to match to image. I.e. rules are combined using AND logic, not OR logic.

@liam102
Copy link

liam102 commented Sep 14, 2022

Thanks @ronkot The AND logic does not make any sense since each image tag obviously has 1 prefix. It should/must go with OR instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment