name: Build and Push Image on: push: branches: [main] workflow_dispatch: {} env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} jobs: build-and-push: runs-on: ubuntu-latest permissions: contents: read packages: write steps: - name: Checkout uses: actions/checkout@v7 - name: Log in to GHCR uses: docker/login-action@v4 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Set up Docker Buildx uses: docker/setup-buildx-action@v4 - name: Extract metadata id: meta uses: docker/metadata-action@v6 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | type=sha,format=short type=raw,value=latest,enable={{is_default_branch}} - name: Build and push uses: docker/build-push-action@v7 with: context: . push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha cache-to: type=gha,mode=max cleanup: needs: build-and-push runs-on: ubuntu-latest permissions: packages: write steps: - name: Keep only the last 4 successful images uses: dataaxiom/ghcr-cleanup-action@v1 with: token: ${{ secrets.GITHUB_TOKEN }} package: icloud-contacts-sync keep-n-tagged: 4 delete-untagged: true exclude-tags: latest