name: Build and Publish Docker Images
on:
push:
branches:
- main
tags:
- 'v*.*.*'
pull_request:
branches:
- main
workflow_dispatch:
inputs:
tag:
description: 'Custom tag for manual builds (optional)'
required: false
type: string
env:
REGISTRY: docker.io
IMAGE_NAME: bjeans/homelab-mcp
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
pull-requests: write
security-events: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
with:
platforms: 'arm64,amd64'
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver-opts: |
image=moby/buildkit:latest
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
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.IMAGE_NAME }}
tags: |
# Tag as 'latest' for main branch
type=raw,value=latest,enable={{is_default_branch}}
# Tag as 'edge' for main branch (development builds)
type=raw,value=edge,enable={{is_default_branch}}
# Semantic versioning for release tags
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
# Custom tag from manual workflow dispatch
type=raw,value=${{ github.event.inputs.tag }},enable=${{ github.event.inputs.tag != '' }}
# Git SHA for traceability
type=sha,prefix={{branch}}-,enable={{is_default_branch}}
# PR builds get a dedicated tag for local scanning
type=ref,event=pr
labels: |
org.opencontainers.image.title=Homelab MCP Servers
org.opencontainers.image.description=MCP servers for homelab infrastructure management
org.opencontainers.image.vendor=Barnaby Jeans
org.opencontainers.image.licenses=MIT
org.opencontainers.image.source=https://github.com/bjeans/homelab-mcp
org.opencontainers.image.documentation=https://github.com/bjeans/homelab-mcp/blob/main/README.md
- name: Build and push Docker image
id: build
uses: docker/build-push-action@v6
with:
context: .
platforms: ${{ github.event_name == 'pull_request' && 'linux/amd64' || 'linux/amd64,linux/arm64' }}
push: ${{ github.event_name != 'pull_request' }}
load: ${{ github.event_name == 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
sbom: ${{ github.event_name != 'pull_request' }}
provenance: ${{ github.event_name != 'pull_request' }}
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
BUILD_DATE=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
VCS_REF=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
VERSION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}
- name: Docker Scout - Scan for vulnerabilities (PRs only)
id: docker-scout
if: github.event_name == 'pull_request'
uses: docker/scout-action@v1.15.1
with:
command: cves
image: ${{ steps.meta.outputs.tags }}
only-severities: critical,high
sarif-file: sarif.json
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Upload Scout SARIF results
if: github.event_name == 'pull_request' && steps.docker-scout.outcome == 'success'
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: sarif.json
category: docker-scout
- name: Generate build summary
if: success()
run: |
echo "## Docker Build Successful! :rocket:" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Image Details" >> $GITHUB_STEP_SUMMARY
echo "- **Repository:** \`${{ env.IMAGE_NAME }}\`" >> $GITHUB_STEP_SUMMARY
echo "- **Platforms:** \`linux/amd64\`, \`linux/arm64\`" >> $GITHUB_STEP_SUMMARY
echo "- **Trigger:** \`${{ github.event_name }}\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Tags Published" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Pull Command" >> $GITHUB_STEP_SUMMARY
echo '```bash' >> $GITHUB_STEP_SUMMARY
echo "docker pull ${{ env.IMAGE_NAME }}:latest" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Docker Hub" >> $GITHUB_STEP_SUMMARY
echo "View on Docker Hub: https://hub.docker.com/r/bjeans/homelab-mcp" >> $GITHUB_STEP_SUMMARY
- name: Build failure notification
if: failure()
run: |
echo "## Docker Build Failed :x:" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "The Docker image build or push failed. Please check the logs above for details." >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Common Issues" >> $GITHUB_STEP_SUMMARY
echo "- Verify DOCKERHUB_USERNAME and DOCKERHUB_TOKEN secrets are configured" >> $GITHUB_STEP_SUMMARY
echo "- Check Dockerfile syntax" >> $GITHUB_STEP_SUMMARY
echo "- Ensure all COPY source files exist" >> $GITHUB_STEP_SUMMARY
echo "- Review platform-specific build errors" >> $GITHUB_STEP_SUMMARY