Automatic (outdated) images cleanup #9
This file contains 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
name: Cleanup Old Docker Images > 6 months by the scheduler | |
on: | |
schedule: | |
- cron: '0 11 * * *' | |
push: | |
branches-ignore: | |
- master | |
jobs: | |
cleanup: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v2 | |
- name: Login to DockerHub | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: List Docker Hub images and delete ones matching the pattern | |
run: | | |
REPO="spryker/php" | |
curl -s "https://hub.docker.com/v2/repositories/${REPO}/tags?page_size=1000" > tags.json | |
TODAY=$(date +%s) | |
THRESHOLD=$((180 * 24 * 60 * 60)) # 180 days in seconds | |
# Regex pattern to match tags that end with a hash (40-character hexadecimal) | |
HASH_PATTERN=".*-[a-f0-9]{40}$" | |
for TAG in $(jq -r '.results[] | @base64' < tags.json); do | |
_jq() { | |
echo ${TAG} | base64 --decode | jq -r ${1} | |
} | |
TAG_NAME=$(_jq '.name') | |
LAST_UPDATED=$(_jq '.last_updated') | |
LAST_UPDATED_DATE=$(date -d "${LAST_UPDATED}" +%s) | |
AGE=$((TODAY - LAST_UPDATED_DATE)) | |
if [[ ${AGE} -ge ${THRESHOLD} ]] && [[ ${TAG_NAME} =~ ${HASH_PATTERN} ]]; then | |
echo "Deleting image tag ${TAG_NAME} (last updated: ${LAST_UPDATED})" | |
fi | |
if [[ ${IMAGES_DELETED} == false ]]; then | |
echo "No images matching the pattern were found for deletion." | |
fi | |
done |