This GitHub Action automatically deletes old Docker images from an AWS ECR repository, keeping only the latest N images.
✅ Fully automated cleanup
✅ Allows users to specify the number of images to retain
✅ Works with GitHub Actions
Create a .github/workflows/cleanup.yml
file:
name: Cleanup ECR Images
on:
schedule:
- cron: "0 0 * * 1"
workflow_dispatch:
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Cleanup Old Images
uses: Frank-Gu-81/[email protected]
with:
repository-name: "ecr-repo-to-cleanup"
keep-images: 3
Secret Name | Description |
---|---|
AWS_ROLE |
IAM Role with ECR permissions |
AWS_REGION |
AWS region (e.g., us-east-1) |
Your IAM role must have the following policies:
{
"Effect": "Allow",
"Action": ["ecr:DescribeImages", "ecr:BatchDeleteImage"],
"Resource": "*"
}