spring-petclinic/.github/workflows/container-build.yml
Bhavesh Khandelwal bdbece8d3f Improve Docker and CI/CD configuration with best practices
- Dockerfile: Add non-root user, healthcheck, optimized layer caching
- docker-compose.yml: Add healthchecks, data volumes, networking, better configuration
- CI/CD: Add multi-platform builds, image testing, multiple tags, metadata
- .dockerignore: Comprehensive exclusions for faster builds

These improvements enhance security, reliability, and maintainability
following Docker and Spring Boot best practices.

Signed-off-by: Bhavesh Khandelwal <bhaveshkhandelwal1232@gmail.com>
2025-12-17 10:42:17 +05:30

82 lines
2.4 KiB
YAML

name: Container Build and Publish
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up JDK 25
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '25'
cache: maven
- name: Build and test with Maven
run: ./mvnw -B verify
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Extract metadata (tags, labels) for Docker
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=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Log in to GitHub Container Registry
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
push: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64
- name: Test Docker image
if: github.event_name == 'pull_request'
run: |
docker build -t spring-petclinic-test .
docker run -d --name petclinic-test -p 8080:8080 \
-e SPRING_PROFILES_ACTIVE=h2 \
spring-petclinic-test
echo "Waiting for application to start..."
sleep 45
echo "Testing health endpoint..."
curl -f http://localhost:8080/actuator/health || exit 1
echo "Health check passed!"
docker stop petclinic-test
docker rm petclinic-test