|
| 1 | +name: Build and Push Coraza-Caddy Container |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Build weekly to get latest security updates |
| 6 | + - cron: '0 2 * * 1' |
| 7 | + push: |
| 8 | + branches: [ main, feature/* ] |
| 9 | + paths: |
| 10 | + - 'Dockerfile' |
| 11 | + - 'Caddyfile' |
| 12 | + - '.github/workflows/build-container.yml' |
| 13 | + pull_request: |
| 14 | + branches: [ main ] |
| 15 | + paths: |
| 16 | + - 'Dockerfile' |
| 17 | + - 'Caddyfile' |
| 18 | + workflow_dispatch: |
| 19 | + inputs: |
| 20 | + branch: |
| 21 | + description: 'Branch to build from' |
| 22 | + required: false |
| 23 | + default: 'main' |
| 24 | + type: string |
| 25 | + caddy_version: |
| 26 | + description: 'Caddy version to build' |
| 27 | + required: false |
| 28 | + default: '2.7' |
| 29 | + type: string |
| 30 | + coraza_version: |
| 31 | + description: 'Coraza version to build' |
| 32 | + required: false |
| 33 | + default: 'v2' |
| 34 | + type: string |
| 35 | + push_image: |
| 36 | + description: 'Push image to registry' |
| 37 | + required: false |
| 38 | + default: true |
| 39 | + type: boolean |
| 40 | + test_locally: |
| 41 | + description: 'Run local container tests' |
| 42 | + required: false |
| 43 | + default: true |
| 44 | + type: boolean |
| 45 | + |
| 46 | +env: |
| 47 | + REGISTRY: ghcr.io |
| 48 | + IMAGE_NAME: ${{ github.repository }}/coraza-caddy |
| 49 | + |
| 50 | +jobs: |
| 51 | + check: |
| 52 | + name: Container Checks |
| 53 | + runs-on: ubuntu-latest |
| 54 | + permissions: |
| 55 | + contents: read |
| 56 | + steps: |
| 57 | + - uses: actions/checkout@v4 |
| 58 | + |
| 59 | + - name: Setup environment |
| 60 | + uses: ./.github/actions/setup-nix-shell |
| 61 | + |
| 62 | + - name: Run container-specific pre-commit hooks |
| 63 | + run: pre-commit run --files Dockerfile Caddyfile --show-diff-on-failure |
| 64 | + |
| 65 | + build: |
| 66 | + runs-on: ubuntu-latest |
| 67 | + permissions: |
| 68 | + contents: read |
| 69 | + packages: write |
| 70 | + security-events: write |
| 71 | + |
| 72 | + steps: |
| 73 | + - name: Checkout repository |
| 74 | + uses: actions/checkout@v4 |
| 75 | + |
| 76 | + - name: Set up Docker Buildx |
| 77 | + uses: docker/setup-buildx-action@v3 |
| 78 | + |
| 79 | + - name: Log in to Container Registry |
| 80 | + uses: docker/login-action@v3 |
| 81 | + with: |
| 82 | + registry: ${{ env.REGISTRY }} |
| 83 | + username: ${{ github.actor }} |
| 84 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 85 | + |
| 86 | + - name: Extract metadata |
| 87 | + id: meta |
| 88 | + uses: docker/metadata-action@v5 |
| 89 | + with: |
| 90 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 91 | + tags: | |
| 92 | + type=raw,value=caddy-${{ github.event.inputs.caddy_version || '2.8' }}-coraza-${{ github.event.inputs.coraza_version || 'v2.0.0' }} |
| 93 | + type=sha,prefix=build- |
| 94 | + type=raw,value=latest,enable={{is_default_branch}} |
| 95 | +
|
| 96 | + - name: Build and push Docker image |
| 97 | + uses: docker/build-push-action@v5 |
| 98 | + with: |
| 99 | + context: . |
| 100 | + platforms: linux/amd64,linux/arm64 |
| 101 | + push: ${{ github.ref == 'refs/heads/main' || (github.event.inputs.push_image == 'true') }} |
| 102 | + tags: ${{ steps.meta.outputs.tags }} |
| 103 | + labels: ${{ steps.meta.outputs.labels }} |
| 104 | + build-args: | |
| 105 | + CADDY_VERSION=${{ github.event.inputs.caddy_version || '2.8' }} |
| 106 | + CORAZA_VERSION=${{ github.event.inputs.coraza_version || 'v2.0.0' }} |
| 107 | + cache-from: type=gha |
| 108 | + cache-to: type=gha,mode=max |
| 109 | + |
| 110 | + - name: Run Trivy vulnerability scanner |
| 111 | + uses: aquasecurity/trivy-action@master |
| 112 | + with: |
| 113 | + image-ref: ${{ fromJSON(steps.meta.outputs.json).tags[0] }} |
| 114 | + format: 'sarif' |
| 115 | + output: 'trivy-results.sarif' |
| 116 | + |
| 117 | + - name: Upload Trivy scan results to GitHub Security tab |
| 118 | + uses: github/codeql-action/upload-sarif@v3 |
| 119 | + if: always() |
| 120 | + with: |
| 121 | + sarif_file: 'trivy-results.sarif' |
| 122 | + |
| 123 | + - name: Test container |
| 124 | + if: ${{ github.event.inputs.test_locally != 'false' }} |
| 125 | + run: | |
| 126 | + set -euo pipefail |
| 127 | + IMAGE_TAG=${{ fromJSON(steps.meta.outputs.json).tags[0] }} |
| 128 | + echo "🧪 Testing image: $IMAGE_TAG" |
| 129 | +
|
| 130 | + echo "Testing built container..." |
| 131 | + docker run --rm -d --name test-coraza \ |
| 132 | + -p 8080:8080 -p 8443:8443 \ |
| 133 | + $IMAGE_TAG |
| 134 | +
|
| 135 | + sleep 15 |
| 136 | +
|
| 137 | + # Test health endpoint |
| 138 | + echo "Testing health endpoint..." |
| 139 | + curl -f http://localhost:8080/health || exit 1 |
| 140 | +
|
| 141 | + # Test WAF is responding |
| 142 | + echo "Testing WAF response..." |
| 143 | + curl -k -f https://localhost:8443/ -o /dev/null -w "%{http_code}" || echo "Expected failure - no backend" |
| 144 | +
|
| 145 | + docker stop test-coraza |
| 146 | + echo "Container tests completed successfully!" |
| 147 | +
|
| 148 | + - name: Build summary |
| 149 | + run: | |
| 150 | + echo "## 🐳 Container Build Summary" >> $GITHUB_STEP_SUMMARY |
| 151 | + echo "- **Branch:** ${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY |
| 152 | + echo "- **Image:** ${{ fromJSON(steps.meta.outputs.json).tags[0] }}" >> $GITHUB_STEP_SUMMARY |
| 153 | + if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then |
| 154 | + echo "- **Status:** ✅ Built and pushed to registry" >> $GITHUB_STEP_SUMMARY |
| 155 | + else |
| 156 | + echo "- **Status:** ✅ Built successfully (not pushed - feature branch)" >> $GITHUB_STEP_SUMMARY |
| 157 | + fi |
0 commit comments