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