Clean Docker images #5
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: Clean Docker images | |
on: | |
delete | |
env: | |
REGISTRY: ghcr.io | |
GH_TOKEN: ${{ github.token }} | |
jobs: | |
list-images: | |
if: github.event.ref_type == 'branch' | |
runs-on: ubuntu-latest | |
outputs: | |
images: ${{ steps.set-matrix.outputs.images }} | |
steps: | |
- name: checkout | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: set matrix | |
id: set-matrix | |
run: echo "images=$(find docker -maxdepth 1 -mindepth 1 | jq --raw-input --slurp --compact-output 'split("\n")[:-1]')" >> $GITHUB_OUTPUT | |
clean-images: | |
if: github.event.ref_type == 'branch' | |
needs: list-images | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
image: ${{ fromJson(needs.list-images.outputs.images) }} | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Set Env | |
run: | | |
echo "TOOL=$(jq -r .name ${{ matrix.image }}/package.json)" >> $GITHUB_ENV | |
echo "SEMVER=$(jq -r .version ${{ matrix.image }}/package.json)" >> $GITHUB_ENV | |
echo "REVISION=$(jq -r .revision ${{ matrix.image }}/package.json)" >> $GITHUB_ENV | |
echo "GH_REF=${GITHUB_REF##*/}" >> $GITHUB_ENV | |
- name: Get versions to delete | |
run: | # NOTE: This will delete any images for branch names that contain the current branch name as a prefix. | |
REF="${{ github.event.ref }}" | |
REF="${REF##*/}" | |
gh api /orgs/${{ github.repository_owner }}/packages/container/${{ env.TOOL }}/versions | jq -r '.[] | select(isempty(.metadata.container.tags[]) or (.metadata.container.tags[] | contains("branch-${REF}"))) | .id' > versions.txt | |
echo "IDS=$(tr -s '\n' ',' < versions.txt | sed 's/,$//')" >> $GITHUB_ENV | |
- uses: actions/delete-package-versions@v5 | |
with: | |
owner: '${{ github.repository_owner }}' | |
package-name: '${{ env.TOOL }}' | |
package-type: 'container' | |
package-version-ids: '${{ env.IDS }}' |