name: Docker Build, Publish & Test on: push: branches: - main - development - feature/beta-release tags: - 'v*.*.*' pull_request: branches: - main - development - feature/beta-release workflow_dispatch: workflow_call: env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository_owner }}/cpmp jobs: build-and-push: runs-on: ubuntu-latest timeout-minutes: 30 permissions: contents: read packages: write security-events: write outputs: skip_build: ${{ steps.skip.outputs.skip_build }} digest: ${{ steps.build-and-push.outputs.digest }} steps: - name: Checkout repository uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - name: Normalize image name run: | echo "IMAGE_NAME=$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV - name: Determine skip condition id: skip env: ACTOR: ${{ github.actor }} EVENT: ${{ github.event_name }} HEAD_MSG: ${{ github.event.head_commit.message }} REF: ${{ github.ref }} run: | should_skip=false pr_title="" if [ "$EVENT" = "pull_request" ]; then pr_title=$(jq -r '.pull_request.title' "$GITHUB_EVENT_PATH" 2>/dev/null || echo '') fi if [ "$ACTOR" = "renovate[bot]" ]; then should_skip=true; fi if echo "$HEAD_MSG" | grep -Ei '^chore\(deps' >/dev/null 2>&1; then should_skip=true; fi if echo "$HEAD_MSG" | grep -Ei '^chore:' >/dev/null 2>&1; then should_skip=true; fi if echo "$pr_title" | grep -Ei '^chore\(deps' >/dev/null 2>&1; then should_skip=true; fi if echo "$pr_title" | grep -Ei '^chore:' >/dev/null 2>&1; then should_skip=true; fi # Always build on beta-release branch to ensure artifacts for testing if [[ "$REF" == "refs/heads/feature/beta-release" ]]; then should_skip=false echo "Force building on beta-release branch" fi echo "skip_build=$should_skip" >> $GITHUB_OUTPUT - name: Set up QEMU if: steps.skip.outputs.skip_build != 'true' uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf # v3.2.0 - name: Set up Docker Buildx if: steps.skip.outputs.skip_build != 'true' uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349 # v3.7.1 - name: Resolve Caddy base digest if: steps.skip.outputs.skip_build != 'true' id: caddy run: | docker pull caddy:2-alpine DIGEST=$(docker inspect --format='{{index .RepoDigests 0}}' caddy:2-alpine) echo "image=$DIGEST" >> $GITHUB_OUTPUT - name: Log in to Container Registry if: github.event_name != 'pull_request' && steps.skip.outputs.skip_build != 'true' uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.CPMP_TOKEN }} - name: Extract metadata (tags, labels) if: steps.skip.outputs.skip_build != 'true' id: meta uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5.5.1 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | type=raw,value=latest,enable={{is_default_branch}} type=raw,value=dev,enable=${{ github.ref == 'refs/heads/development' }} type=raw,value=beta,enable=${{ github.ref == 'refs/heads/feature/beta-release' }} type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} type=semver,pattern={{major}} type=raw,value=pr-${{ github.ref_name }},enable=${{ github.event_name == 'pull_request' }} type=sha,format=short,enable=${{ github.event_name != 'pull_request' }} - name: Build and push Docker image if: steps.skip.outputs.skip_build != 'true' id: build-and-push uses: docker/build-push-action@v6 # v6.9.0 with: context: . platforms: ${{ github.event_name == 'pull_request' && 'linux/amd64' || '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 build-args: | VERSION=${{ steps.meta.outputs.version }} BUILD_DATE=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }} VCS_REF=${{ github.sha }} CADDY_IMAGE=${{ steps.caddy.outputs.image }} - name: Run Trivy scan (table output) if: github.event_name != 'pull_request' && steps.skip.outputs.skip_build != 'true' uses: aquasecurity/trivy-action@0.28.0 # 0.28.0 with: image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build-and-push.outputs.digest }} format: 'table' severity: 'CRITICAL,HIGH' exit-code: '0' continue-on-error: true - name: Run Trivy vulnerability scanner (SARIF) if: github.event_name != 'pull_request' && steps.skip.outputs.skip_build != 'true' id: trivy uses: aquasecurity/trivy-action@0.28.0 # 0.28.0 with: image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build-and-push.outputs.digest }} format: 'sarif' output: 'trivy-results.sarif' severity: 'CRITICAL,HIGH' continue-on-error: true - name: Check Trivy SARIF exists if: github.event_name != 'pull_request' && steps.skip.outputs.skip_build != 'true' id: trivy-check run: | if [ -f trivy-results.sarif ]; then echo "exists=true" >> $GITHUB_OUTPUT else echo "exists=false" >> $GITHUB_OUTPUT fi - name: Upload Trivy results if: github.event_name != 'pull_request' && steps.skip.outputs.skip_build != 'true' && steps.trivy-check.outputs.exists == 'true' uses: github/codeql-action/upload-sarif@f079b8493333aace61c81488f8bd40919487bd9f # v3.26.13 with: sarif_file: 'trivy-results.sarif' token: ${{ secrets.GITHUB_TOKEN }} - name: Create summary if: steps.skip.outputs.skip_build != 'true' run: | echo "## ๐ŸŽ‰ Docker Image Built Successfully!" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY echo "### ๐Ÿ“ฆ Image Details" >> $GITHUB_STEP_SUMMARY echo "- **Registry**: GitHub Container Registry (ghcr.io)" >> $GITHUB_STEP_SUMMARY echo "- **Repository**: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}" >> $GITHUB_STEP_SUMMARY echo "- **Tags**: " >> $GITHUB_STEP_SUMMARY echo '```' >> $GITHUB_STEP_SUMMARY echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY echo '```' >> $GITHUB_STEP_SUMMARY test-image: name: Test Docker Image needs: build-and-push runs-on: ubuntu-latest if: needs.build-and-push.outputs.skip_build != 'true' && github.event_name != 'pull_request' steps: - name: Normalize image name run: | raw="${{ github.repository_owner }}/${{ github.event.repository.name }}" echo "IMAGE_NAME=$(echo "$raw" | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV - name: Determine image tag id: tag run: | if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then echo "tag=latest" >> $GITHUB_OUTPUT elif [[ "${{ github.ref }}" == "refs/heads/development" ]]; then echo "tag=dev" >> $GITHUB_OUTPUT elif [[ "${{ github.ref }}" == refs/tags/v* ]]; then echo "tag=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT else echo "tag=sha-$(echo ${{ github.sha }} | cut -c1-7)" >> $GITHUB_OUTPUT fi - name: Log in to GitHub Container Registry uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.CPMP_TOKEN }} - name: Pull Docker image run: docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.tag.outputs.tag }} - name: Run container run: | docker run -d \ --name test-container \ -p 8080:8080 \ ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.tag.outputs.tag }} - name: Test health endpoint (retries) run: | set +e for i in $(seq 1 30); do code=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/api/v1/health || echo "000") if [ "$code" = "200" ]; then echo "โœ… Health check passed on attempt $i" exit 0 fi echo "Attempt $i/30: health not ready (code=$code); waiting..." sleep 2 done echo "โŒ Health check failed after retries" docker logs test-container || true exit 1 - name: Check container logs if: always() run: docker logs test-container - name: Stop container if: always() run: docker stop test-container && docker rm test-container - name: Create test summary if: always() run: | echo "## ๐Ÿงช Docker Image Test Results" >> $GITHUB_STEP_SUMMARY echo "" >> $GITHUB_STEP_SUMMARY echo "- **Image**: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.tag.outputs.tag }}" >> $GITHUB_STEP_SUMMARY echo "- **Health Check**: ${{ job.status == 'success' && 'โœ… Passed' || 'โŒ Failed' }}" >> $GITHUB_STEP_SUMMARY