|
| 1 | +name: "Prune old GHCR images" |
| 2 | + |
| 3 | +on: |
| 4 | + # 1) Run daily at 02:00 UTC |
| 5 | + schedule: |
| 6 | + - cron: '0 2 * * *' |
| 7 | + # 2) Also run on every push to main (or your default branch) |
| 8 | + push: |
| 9 | + branches: |
| 10 | + - docker |
| 11 | + |
| 12 | +permissions: |
| 13 | + contents: read # for listing versions |
| 14 | + packages: write # for deleting package versions |
| 15 | + |
| 16 | +jobs: |
| 17 | + prune: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: Install jq and GitHub CLI |
| 22 | + run: | |
| 23 | + sudo apt-get update |
| 24 | + sudo apt-get install -y jq |
| 25 | + curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg \ |
| 26 | + | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg |
| 27 | + echo \ |
| 28 | + "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] \ |
| 29 | + https://cli.github.com/packages stable main" \ |
| 30 | + | sudo tee /etc/apt/sources.list.d/github-cli.list |
| 31 | + sudo apt-get update |
| 32 | + sudo apt-get install -y gh |
| 33 | +
|
| 34 | + - name: Authenticate gh |
| 35 | + env: |
| 36 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 37 | + run: | |
| 38 | + echo "$GITHUB_TOKEN" | gh auth login --with-token |
| 39 | +
|
| 40 | + - name: Delete non-latest versions |
| 41 | + env: |
| 42 | + OWNER: ${{ github.repository_owner }} |
| 43 | + IMAGE: deep-learning-crash-course |
| 44 | + run: | |
| 45 | + gh api -H "Accept: application/vnd.github.v3+json" \ |
| 46 | + /users/$OWNER/packages/container/$IMAGE/versions \ |
| 47 | + | jq -c '.[]' \ |
| 48 | + | while read version; do |
| 49 | + id=$(echo "$version" | jq -r '.id') |
| 50 | + tags=$(echo "$version" | jq -r '.metadata.container.tags[]') |
| 51 | + if [[ ! " $tags " =~ " latest " ]]; then |
| 52 | + echo "Deleting version $id (tags: $tags)" |
| 53 | + gh api -X DELETE \ |
| 54 | + -H "Accept: application/vnd.github.v3+json" \ |
| 55 | + /users/$OWNER/packages/container/$IMAGE/versions/$id |
| 56 | + fi |
| 57 | + done |
0 commit comments