Skip to content

Feature/refactoring #24

Feature/refactoring

Feature/refactoring #24

name: Build and Push Coraza-Caddy Container
on:
schedule:
# Build weekly to get latest security updates
- cron: '0 2 * * 1'
push:
branches: [ main, feature/* ]
paths:
- 'Dockerfile'
- 'Caddyfile'
- '.github/workflows/build-container.yml'
pull_request:
branches: [ main ]
paths:
- 'Dockerfile'
- 'Caddyfile'
workflow_dispatch:
inputs:
branch:
description: 'Branch to build from'
required: false
default: 'main'
type: string
caddy_version:
description: 'Caddy version to build'
required: false
default: '2.7'
type: string
coraza_version:
description: 'Coraza version to build'
required: false
default: 'v2'
type: string
push_image:
description: 'Push image to registry'
required: false
default: true
type: boolean
test_locally:
description: 'Run local container tests'
required: false
default: true
type: boolean
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}/coraza-caddy
jobs:
check:
name: Container Checks
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- name: Setup environment
uses: ./.github/actions/setup-nix-shell
- name: Run container-specific pre-commit hooks
run: pre-commit run --files Dockerfile Caddyfile --show-diff-on-failure
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
security-events: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=caddy-${{ github.event.inputs.caddy_version || '2.8' }}-coraza-${{ github.event.inputs.coraza_version || 'v2.0.0' }}
type=sha,prefix=build-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.ref == 'refs/heads/main' || (github.event.inputs.push_image == 'true') }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
CADDY_VERSION=${{ github.event.inputs.caddy_version || '2.8' }}
CORAZA_VERSION=${{ github.event.inputs.coraza_version || 'v2.0.0' }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: ${{ fromJSON(steps.meta.outputs.json).tags[0] }}
format: 'sarif'
output: 'trivy-results.sarif'
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v3
if: always()
with:
sarif_file: 'trivy-results.sarif'
- name: Test container
if: ${{ github.event.inputs.test_locally != 'false' }}
run: |
set -euo pipefail
IMAGE_TAG=${{ fromJSON(steps.meta.outputs.json).tags[0] }}
echo "🧪 Testing image: $IMAGE_TAG"
echo "Testing built container..."
docker run --rm -d --name test-coraza \
-p 8080:8080 -p 8443:8443 \
$IMAGE_TAG
sleep 15
# Test health endpoint
echo "Testing health endpoint..."
curl -f http://localhost:8080/health || exit 1
# Test WAF is responding
echo "Testing WAF response..."
curl -k -f https://localhost:8443/ -o /dev/null -w "%{http_code}" || echo "Expected failure - no backend"
docker stop test-coraza
echo "Container tests completed successfully!"
- name: Build summary
run: |
echo "## 🐳 Container Build Summary" >> $GITHUB_STEP_SUMMARY
echo "- **Branch:** ${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY
echo "- **Image:** ${{ fromJSON(steps.meta.outputs.json).tags[0] }}" >> $GITHUB_STEP_SUMMARY
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
echo "- **Status:** ✅ Built and pushed to registry" >> $GITHUB_STEP_SUMMARY
else
echo "- **Status:** ✅ Built successfully (not pushed - feature branch)" >> $GITHUB_STEP_SUMMARY
fi