Cleanup dev images of merged PRs #29
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" > api_output.txt | |
echo "API output" && cat api_output.txt | |
cat api_output.txt | jq -r '.tags[]' > tags_list.txt | |
echo "Fetched Image Tags:" && cat tags_list.txt | |
- name: Identify Merged or Closed PRs | |
run: | | |
> delete_tags.txt # Clear file before writing | |
while read -r TAG; do | |
echo "Read tag: $TAG" | |
if [[ $TAG =~ ^([a-zA-Z0-9-]+-pr-)([0-9]+) ]]; then | |
PR_NUMBER=${BASH_REMATCH[2]} | |
echo "PR Number: $PR_NUMBER" | |
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') | |
STATE=$(echo "$RESPONSE" | jq -r '.state') | |
if [[ "$MERGED" == "true" || "$STATE" == "closed" ]]; then | |
echo "$TAG" >> delete_tags.txt | |
echo "Marked for deletion: $TAG (Merged: $MERGED, State: $STATE)" | |
else | |
echo "Skipping: $TAG (Merged: $MERGED, State: $STATE)" | |
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) | |
- 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 |