Files
caddy-proxy-manager/.github/workflows/docker-build.yml
Claude 9949240789 Fix build pipeline platform and Trivy issues
Fixed two critical build failures:

1. Platform Selection Bug:
   - Fixed operator precedence issue in platform conditional
   - Was evaluating to boolean 'true' instead of platform string
   - Changed: platforms: ${{ ... || ... && 'linux/amd64' || ... }}
   - To: platforms: ${{ (... || ...) && 'linux/amd64' || ... }}
   - Now correctly uses linux/amd64 for PRs, linux/amd64,linux/arm64 for releases

2. Trivy Multiple Tags Issue:
   - Trivy was receiving multiple tags separated by newlines
   - Added step to extract first tag from metadata output
   - Trivy now scans using single tag reference
   - Prevents "multiple targets cannot be specified" error

Both PRs and production builds should now complete successfully.
2025-11-04 21:40:02 +00:00

144 lines
4.7 KiB
YAML

name: Build and Push Docker Images
on:
push:
branches:
- main
- develop
tags:
- 'v*'
pull_request:
branches:
- main
- develop
pull_request_target:
types: [labeled]
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
# Security check for fork PRs
security-check:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
permissions:
pull-requests: read
outputs:
is_fork: ${{ steps.check.outputs.is_fork }}
steps:
- name: Check if PR is from fork
id: check
run: |
if [ "${{ github.event.pull_request.head.repo.full_name }}" != "${{ github.repository }}" ]; then
echo "is_fork=true" >> $GITHUB_OUTPUT
echo "::warning::This PR is from a fork. Builds from forks require manual approval for security."
else
echo "is_fork=false" >> $GITHUB_OUTPUT
fi
build-and-push:
needs: [security-check]
# Only run on non-fork PRs, or if manually approved (has 'safe-to-build' label)
if: |
github.event_name != 'pull_request' ||
needs.security-check.outputs.is_fork == 'false' ||
(github.event_name == 'pull_request_target' && contains(github.event.pull_request.labels.*.name, 'safe-to-build'))
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
security-events: write # For Trivy to upload SARIF results
strategy:
matrix:
include:
- service: web
dockerfile: docker/web/Dockerfile
context: .
- service: caddy
dockerfile: docker/caddy/Dockerfile
context: .
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
# For pull_request_target, checkout the PR head
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.sha || '' }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
if: github.event_name != 'pull_request' && github.event_name != 'pull_request_target'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels)
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-${{ matrix.service }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
id: build
uses: docker/build-push-action@v5
with:
context: ${{ matrix.context }}
file: ${{ matrix.dockerfile }}
push: ${{ github.event_name != 'pull_request' && github.event_name != 'pull_request_target' }}
load: ${{ github.event_name == 'pull_request' || github.event_name == 'pull_request_target' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: ${{ (github.event_name == 'pull_request' || github.event_name == 'pull_request_target') && 'linux/amd64' || 'linux/amd64,linux/arm64' }}
sbom: true
provenance: true
- name: Extract first tag for Trivy
id: trivy-tag
run: |
# Extract the first tag from the metadata output
FIRST_TAG=$(echo "${{ steps.meta.outputs.tags }}" | head -n 1)
echo "tag=$FIRST_TAG" >> $GITHUB_OUTPUT
echo "Using tag for Trivy: $FIRST_TAG"
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@0.24.0
with:
image-ref: ${{ steps.trivy-tag.outputs.tag }}
format: 'sarif'
output: 'trivy-results-${{ matrix.service }}.sarif'
severity: 'CRITICAL,HIGH'
exit-code: '1' # Fail the build on critical/high vulnerabilities
- name: Upload Trivy results to GitHub Security
if: always()
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: 'trivy-results-${{ matrix.service }}.sarif'
category: 'trivy-${{ matrix.service }}'
- name: Run Trivy in table format
if: always()
uses: aquasecurity/trivy-action@0.24.0
with:
image-ref: ${{ steps.trivy-tag.outputs.tag }}
format: 'table'
severity: 'CRITICAL,HIGH,MEDIUM'