Skip to content

Docker Build and Publish #49

Docker Build and Publish

Docker Build and Publish #49

Workflow file for this run

name: Docker Build and Publish
on:
# Triggered automatically when CI completes on main/develop
workflow_run:
workflows: ["CI Pipeline"]
types:
- completed
branches:
- main
- develop
# Manual trigger and tags
workflow_dispatch:
push:
tags:
- 'v*'
pull_request:
branches:
- main
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
# Only run if workflow_run succeeded or triggered manually/by tag
if: |
github.event_name != 'workflow_run' ||
github.event.workflow_run.conclusion == 'success'
permissions:
contents: read
packages: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Check and pull base image
id: base-image
run: |
# Convert owner to lowercase for Docker registry compatibility
OWNER_LOWERCASE=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
BASE_IMAGE="${{ env.REGISTRY }}/${OWNER_LOWERCASE}/apprun-base:latest"
echo "base_image=${BASE_IMAGE}" >> $GITHUB_OUTPUT
# Try to pull the base image
if docker pull ${BASE_IMAGE} 2>/dev/null; then
echo "✅ Base image pulled successfully: ${BASE_IMAGE}"
echo "status=available" >> $GITHUB_OUTPUT
else
echo "⚠️ Base image not found, will build from scratch"
echo "💡 Consider running 'Build Base Image' workflow first"
echo "status=missing" >> $GITHUB_OUTPUT
fi
- 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=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
id: build
uses: docker/build-push-action@v5
with:
context: .
file: ./docker/Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
BASE_IMAGE=${{ steps.base-image.outputs.base_image }}
BUILD_DATE=${{ steps.meta.outputs.created }}
VERSION=${{ steps.meta.outputs.version }}
COMMIT_SHA=${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64
- name: Image digest
if: github.event_name != 'pull_request'
run: echo "Image digest - ${{ steps.build.outputs.digest }}"
- name: Build status summary
if: steps.base-image.outputs.status == 'missing'
run: |
echo "⚠️ Warning: Base image not found" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "The build may take longer as dependencies will be downloaded during build." >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Recommendation:** Run the 'Build Base Image' workflow first:" >> $GITHUB_STEP_SUMMARY
echo "1. Go to Actions > Build Base Image" >> $GITHUB_STEP_SUMMARY
echo "2. Click 'Run workflow' > 'Force rebuild'" >> $GITHUB_STEP_SUMMARY
- name: Create release summary
if: startsWith(github.ref, 'refs/tags/v')
run: |
echo "## Docker Image Published" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Tag:** ${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY
echo "**Registry:** ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Pull the image:" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
echo "docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY