Cleanup dev images of merged PRs #23
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 dev images of merged PRs | |
on: | |
schedule: | |
- cron: "0 2 * * *" # Runs daily at 2 AM UTC | |
workflow_dispatch: # Allows manual triggering | |
env: | |
IMAGE_NAME: ckan-sddi-dev | |
IMAGE_OWNER: ${{ github.repository_owner }} | |
jobs: | |
cleanup-ghcr: | |
runs-on: ubuntu-latest | |
permissions: | |
packages: write | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Get Image Tags from GHCR | |
run: | | |
GHCR_API="https://ghcr.io/v2/$IMAGE_OWNER/$IMAGE_NAME/tags/list" | |
curl -s -H "Authorization: Bearer $(echo ${{ secrets.GITHUB_TOKEN }} | base64)" "$GHCR_API" | jq -r '.tags[]' > tags_list.txt | |
echo "Fetched Image Tags:" && cat tags_list.txt | |
- name: Identify Merged PRs | |
run: | | |
> delete_tags.txt # Clear file before writing | |
while read -r TAG; do | |
if [[ $TAG =~ ^([a-zA-Z0-9-]+-pr-)([0-9]+) ]]; then | |
PR_NUMBER=${BASH_REMATCH[2]} | |
RESPONSE=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
"https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER") | |
MERGED=$(echo "$RESPONSE" | jq -r '.merged') | |
if [[ "$MERGED" == "true" ]]; then | |
echo "$TAG" >> delete_tags.txt | |
fi | |
else | |
echo "$TAG not matched." | |
fi | |
done < tags_list.txt | |
echo "Images to be deleted:" && cat delete_tags.txt | |
# Store the content of delete_tags.txt as a comma-separated list | |
DELETE_TAGS=$(paste -sd "," delete_tags.txt) | |
echo "DELETE_TAGS=$DELETE_TAGS" >> $GITHUB_ENV | |
- name: Delete Merged PR Images | |
uses: dataaxiom/ghcr-cleanup-action@v1 | |
with: | |
dry-run: false | |
package: ${{ env.IMAGE_NAME }} | |
tags: ${{ env.DELETE_TAGS }} | |
# use-regex: true | |
validate: true | |
log-level: info | |
delete-untagged: true | |
# delete-ghost-images: true | |
# delete-partial-images: true | |
# delete-orphaned-images: true |