Skip to content

Commit

Permalink
entrypoint added
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank-Gu-81 committed Feb 14, 2025
1 parent 6aa54a1 commit a001e8b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Empty file added Dockerfile
Empty file.
31 changes: 31 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

set -e # Exit immediately if a command fails

REPO_NAME="$1"
KEEP_IMAGES="$2"

if [ -z "$REPO_NAME" ] || [ -z "$KEEP_IMAGES" ]; then
echo "Usage: ./entrypoint.sh <ECR_REPOSITORY> <NUMBER_TO_KEEP>"
exit 1
fi

echo "Cleaning up ECR repository: $REPO_NAME"
echo "Keeping the latest $KEEP_IMAGES images..."

# Get image digests sorted by push time, keeping only older images beyond the most recent $KEEP_IMAGES
DIGESTS=$(aws ecr describe-images --repository-name "$REPO_NAME" --query "sort_by(imageDetails, &imagePushedAt)[0:-$KEEP_IMAGES].imageDigest" --output json | jq -r '.[]')

if [ -z "$DIGESTS" ]; then
echo "No old images to delete."
else
echo "The following images will be deleted: $DIGESTS"

# Format for batch delete
IMAGE_IDS=$(echo "$DIGESTS" | jq -R '[{"imageDigest": .}]' | jq -s '{imageIds: add}')

echo "Final JSON Payload for Deletion:"
echo "$IMAGE_IDS"

aws ecr batch-delete-image --repository-name "$REPO_NAME" --cli-input-json "$IMAGE_IDS"
fi

0 comments on commit a001e8b

Please sign in to comment.