name: Build and Push on: push: branches: [main] tags: ["v*.*.*"] workflow_dispatch: jobs: build: runs-on: ubuntu-latest permissions: contents: read packages: write steps: - name: Checkout uses: actions/checkout@v7 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v4 - name: Log in to GHCR uses: docker/login-action@v4 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata id: meta uses: docker/metadata-action@v6 with: images: ghcr.io/${{ github.repository_owner }}/dyndns-updater tags: | type=raw,value=latest,enable={{is_default_branch}} type=semver,pattern={{version}} - name: Build and push uses: docker/build-push-action@v7 with: context: . push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cleanup: runs-on: ubuntu-latest needs: build permissions: packages: write contents: read steps: - name: Keep only last 4 images env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | PKG_NAME="dyndns-updater" KEEP=4 VERSIONS=$(gh api \ -H "Accept: application/vnd.github+json" \ "/user/packages/container/${PKG_NAME}/versions?per_page=100" \ --jq '.[] | "\(.id) \(.created_at) \(.metadata.container.tags // [] | join(","))"') COUNT=$(echo "$VERSIONS" | wc -l) echo "Found $COUNT versions" if [ "$COUNT" -le "$KEEP" ]; then echo "Nothing to delete ($COUNT <= $KEEP)" exit 0 fi DELETE_COUNT=$((COUNT - KEEP)) echo "Deleting $DELETE_COUNT oldest versions..." echo "$VERSIONS" | sort -k2 -r | tail -n "$DELETE_COUNT" | while read -r ID CREATED TAGS; do echo "Deleting version $ID (created: $CREATED, tags: $TAGS)" gh api \ --method DELETE \ "/user/packages/container/${PKG_NAME}/versions/$ID" || true done