name: Build and Publish Docker Image on: push: branches: [main, master] paths: # run only when this file changed at all - "unshackle/core/__init__.py" pull_request: {} # optional – delete if you don’t build on PRs workflow_dispatch: {} # manual override jobs: detect-version-change: runs-on: ubuntu-latest outputs: changed: ${{ steps.vdiff.outputs.changed }} version: ${{ steps.vdiff.outputs.version }} steps: - uses: actions/checkout@v4 with: { fetch-depth: 2 } # we need the previous commit :contentReference[oaicite:1]{index=1} - name: Extract & compare version id: vdiff shell: bash run: | current=$(grep -oP '__version__ = "\K[^"]+' unshackle/core/__init__.py) prev=$(git show HEAD^:unshackle/core/__init__.py \ | grep -oP '__version__ = "\K[^"]+' || echo '') echo "version=$current" >>"$GITHUB_OUTPUT" echo "changed=$([ "$current" != "$prev" ] && echo true || echo false)" >>"$GITHUB_OUTPUT" echo "Current=$current Previous=$prev" build-and-push: needs: detect-version-change if: needs.detect-version-change.outputs.changed == 'true' # only run when bumped :contentReference[oaicite:2]{index=2} runs-on: ubuntu-latest permissions: { contents: read, packages: write } steps: - name: Checkout repository uses: actions/checkout@v4 - name: Extract version from __init__.py id: version run: | VERSION=$(grep -oP '__version__ = "\K[^"]+' unshackle/core/__init__.py) echo "version=$VERSION" >> $GITHUB_OUTPUT echo "major_minor=$(echo $VERSION | cut -d. -f1-2)" >> $GITHUB_OUTPUT echo "major=$(echo $VERSION | cut -d. -f1)" >> $GITHUB_OUTPUT echo "Extracted version: $VERSION" - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to Container Registry if: github.event_name != 'pull_request' uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata id: meta uses: docker/metadata-action@v5 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | type=ref,event=branch type=ref,event=pr type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} type=semver,pattern={{major}} type=raw,value=latest,enable={{is_default_branch}} type=raw,value=v${{ steps.version.outputs.version }},enable={{is_default_branch}} type=raw,value=${{ steps.version.outputs.version }},enable={{is_default_branch}} type=raw,value=${{ steps.version.outputs.major_minor }},enable={{is_default_branch}} type=raw,value=${{ steps.version.outputs.major }},enable={{is_default_branch}} - name: Show planned tags run: | echo "Planning to create the following tags:" echo "${{ steps.meta.outputs.tags }}" - name: Build and push Docker image uses: docker/build-push-action@v5 with: context: . platforms: linux/amd64,linux/arm64 push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha cache-to: type=gha,mode=max - name: Test Docker image if: github.event_name != 'pull_request' run: | docker run --rm ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest env check