name: Build and Publish Docker Image
on:
push:
branches:
- main
tags:
- 'v*.*.*'
pull_request:
branches:
- main
workflow_dispatch:
env:
GHCR_REGISTRY: ghcr.io
GHCR_IMAGE_NAME: ${{ github.repository }}
DOCKERHUB_IMAGE_NAME: glassity/focus-mcp
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ env.GHCR_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to Docker Hub
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ env.GHCR_REGISTRY }}/${{ env.GHCR_IMAGE_NAME }}
${{ env.DOCKERHUB_IMAGE_NAME }}
tags: |
# Set latest tag for main branch
type=raw,value=latest,enable={{is_default_branch}}
# Tag with version for releases (v1.0.0 -> 1.0.0)
type=semver,pattern={{version}}
# Tag with major.minor for releases (v1.0.0 -> 1.0)
type=semver,pattern={{major}}.{{minor}}
# Tag with major for releases (v1.0.0 -> 1)
type=semver,pattern={{major}}
# Tag with PR number for PRs
type=ref,event=pr
# Tag with branch name (sanitized)
type=ref,event=branch
# Tag with short SHA
type=sha
- name: Build and push Docker image
id: push
uses: docker/build-push-action@v6
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: Generate artifact attestation
if: github.event_name != 'pull_request'
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.GHCR_REGISTRY }}/${{ env.GHCR_IMAGE_NAME }}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
- name: Output image details
if: github.event_name != 'pull_request'
run: |
echo "Images published to multiple registries:"
echo ""
echo "GitHub Container Registry:"
echo " docker pull ${{ env.GHCR_REGISTRY }}/${{ env.GHCR_IMAGE_NAME }}:latest"
echo ""
echo "Docker Hub:"
echo " docker pull ${{ env.DOCKERHUB_IMAGE_NAME }}:latest"
echo ""
echo "All tags:"
echo "${{ steps.meta.outputs.tags }}"