name: Build and Publish Docker Image on: push: branches: - main - master paths: - "unshackle/core/__init__.py" - "Dockerfile" - "pyproject.toml" - "uv.lock" - ".github/workflows/docker.yml" tags: - "v*" pull_request: branches: - main - master paths: - "unshackle/core/__init__.py" - "Dockerfile" - "pyproject.toml" - "uv.lock" - ".github/workflows/docker.yml" env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }} jobs: build-and-push: 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