|
3 | 3 | # separate terms of service, privacy policy, and support |
4 | 4 | # documentation. |
5 | 5 |
|
6 | | -# A sample workflow which checks out the code, builds a container |
7 | | -# image using Docker and scans that image for vulnerabilities using |
| 6 | +# A sample workflow which checks out the code, builds multi-architecture container |
| 7 | +# images using Docker Buildx and scans that image for vulnerabilities using |
8 | 8 | # Snyk. The results are then uploaded to GitHub Security Code Scanning |
9 | 9 | # |
10 | 10 | # For more examples, including how to limit scans to only high-severity |
|
22 | 22 | schedule: |
23 | 23 | - cron: '41 5 * * 5' |
24 | 24 |
|
| 25 | +# Prevent multiple concurrent runs for the same workflow |
| 26 | +concurrency: |
| 27 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 28 | + cancel-in-progress: true |
| 29 | + |
25 | 30 | env: |
26 | 31 | APP_BASE_DIR: "./app" |
| 32 | + REGISTRY: ghcr.io |
| 33 | + IMAGE_NAME: ${{ github.repository }} |
27 | 34 |
|
28 | 35 | jobs: |
29 | | - Build-Test-Scan: |
30 | | - runs-on: ubuntu-latest |
| 36 | + # Build multi-architecture images natively |
| 37 | + build: |
| 38 | + name: Build (${{ matrix.platform }} - ${{ matrix.target }}) |
| 39 | + timeout-minutes: 30 |
| 40 | + strategy: |
| 41 | + fail-fast: false |
| 42 | + matrix: |
| 43 | + include: |
| 44 | + - platform: linux/amd64 |
| 45 | + runner: ubuntu-latest |
| 46 | + target: app |
| 47 | + - platform: linux/amd64 |
| 48 | + runner: ubuntu-latest |
| 49 | + target: web |
| 50 | + - platform: linux/arm64 |
| 51 | + runner: ubuntu-24.04-arm |
| 52 | + target: app |
| 53 | + - platform: linux/arm64 |
| 54 | + runner: ubuntu-24.04-arm |
| 55 | + target: web |
| 56 | + runs-on: ${{ matrix.runner }} |
31 | 57 | steps: |
32 | | - - uses: actions/checkout@v2 |
| 58 | + - name: Checkout |
| 59 | + uses: actions/checkout@v4 |
| 60 | + |
33 | 61 | - name: Install Demo App |
34 | | - uses: php-actions/composer@v6 |
| 62 | + run: | |
| 63 | + # Pull composer image for the correct platform |
| 64 | + docker pull --platform ${{ matrix.platform }} composer:2 |
| 65 | + # Run composer create-project |
| 66 | + docker run --rm --platform ${{ matrix.platform }} \ |
| 67 | + -v ${{ github.workspace }}:/app \ |
| 68 | + -w /app \ |
| 69 | + composer:2 create-project \ |
| 70 | + --no-install --no-scripts \ |
| 71 | + symfony/symfony-demo app |
| 72 | +
|
| 73 | + - name: Set up Docker Buildx |
| 74 | + uses: docker/setup-buildx-action@v3 |
| 75 | + |
| 76 | + - name: Extract metadata for Docker |
| 77 | + id: meta |
| 78 | + uses: docker/metadata-action@v5 |
35 | 79 | with: |
36 | | - dev: no |
37 | | - command: create-project |
38 | | - args: --no-install --no-scripts symfony/symfony-demo:v2.1.0 app |
39 | | - php_version: 8.1 |
| 80 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}/${{ matrix.target }} |
| 81 | + tags: | |
| 82 | + type=ref,event=branch |
| 83 | + type=ref,event=pr |
| 84 | + type=sha |
| 85 | + type=raw,value=latest,enable={{is_default_branch}} |
| 86 | +
|
| 87 | + - name: Build image |
| 88 | + uses: docker/build-push-action@v6 |
| 89 | + with: |
| 90 | + context: . |
| 91 | + platforms: ${{ matrix.platform }} |
| 92 | + target: ${{ matrix.target }} |
| 93 | + push: false |
| 94 | + load: true |
| 95 | + tags: kubephp-${{ matrix.target }}:test |
| 96 | + build-args: | |
| 97 | + APP_BASE_DIR=${{ env.APP_BASE_DIR }} |
| 98 | + BUILDPLATFORM=${{ matrix.platform }} |
| 99 | + TARGETPLATFORM=${{ matrix.platform }} |
| 100 | + cache-from: type=gha,scope=${{ matrix.platform }}-${{ matrix.target }} |
| 101 | + cache-to: type=gha,mode=max,scope=${{ matrix.platform }}-${{ matrix.target }} |
| 102 | + |
| 103 | + - name: Test the App Startup |
| 104 | + if: matrix.target == 'app' |
| 105 | + run: | |
| 106 | + # Run container with explicit platform to ensure native execution |
| 107 | + docker run -d --name test-app --platform ${{ matrix.platform }} kubephp-app:test |
| 108 | + sleep 5 |
| 109 | + docker exec test-app php -v |
| 110 | + docker exec test-app php -m |
| 111 | + docker stop test-app |
| 112 | + docker rm test-app |
| 113 | +
|
| 114 | + # Test the full stack on both AMD64 and ARM64 natively |
| 115 | + integration-test: |
| 116 | + name: Integration Test (${{ matrix.platform }}) |
| 117 | + timeout-minutes: 20 |
| 118 | + strategy: |
| 119 | + fail-fast: false |
| 120 | + matrix: |
| 121 | + include: |
| 122 | + - platform: linux/amd64 |
| 123 | + runner: ubuntu-latest |
| 124 | + - platform: linux/arm64 |
| 125 | + runner: ubuntu-24.04-arm |
| 126 | + runs-on: ${{ matrix.runner }} |
| 127 | + needs: build |
| 128 | + steps: |
| 129 | + - name: Checkout |
| 130 | + uses: actions/checkout@v4 |
| 131 | + |
| 132 | + - name: Install Demo App |
| 133 | + run: | |
| 134 | + # Pull composer image for the correct platform |
| 135 | + docker pull --platform ${{ matrix.platform }} composer:2 |
| 136 | + # Run composer create-project |
| 137 | + docker run --rm --platform ${{ matrix.platform }} \ |
| 138 | + -v ${{ github.workspace }}:/app \ |
| 139 | + -w /app \ |
| 140 | + composer:2 create-project \ |
| 141 | + --no-install --no-scripts \ |
| 142 | + symfony/symfony-demo app |
| 143 | +
|
| 144 | + - name: Set up Docker Buildx |
| 145 | + uses: docker/setup-buildx-action@v3 |
| 146 | + |
40 | 147 | - name: Build & Deploy |
41 | 148 | run: make deploy |
| 149 | + |
| 150 | + - name: Wait for services to be ready |
| 151 | + run: | |
| 152 | + timeout=60 |
| 153 | + elapsed=0 |
| 154 | + while ! curl -f http://localhost:8080 >/dev/null 2>&1; do |
| 155 | + if [ $elapsed -ge $timeout ]; then |
| 156 | + echo "Service failed to start within $timeout seconds" |
| 157 | + docker compose -f docker-compose.prod.yml logs |
| 158 | + exit 1 |
| 159 | + fi |
| 160 | + echo "Waiting for service... ($elapsed/$timeout seconds)" |
| 161 | + sleep 2 |
| 162 | + elapsed=$((elapsed + 2)) |
| 163 | + done |
| 164 | + echo "Service is ready!" |
| 165 | +
|
42 | 166 | - name: Test the App Startup |
43 | | - run: sleep 5 && curl localhost:8080 -I |
44 | | - - name: Run Snyk to check Docker image for vulnerabilities |
45 | | - # Snyk can be used to break the build when it detects vulnerabilities. |
46 | | - # In this case we want to upload the issues to GitHub Code Scanning |
47 | | - continue-on-error: true |
48 | | - uses: snyk/actions/docker@14818c4695ecc4045f33c9cee9e795a788711ca4 |
49 | | - env: |
50 | | - SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} |
| 167 | + run: | |
| 168 | + curl -f http://localhost:8080 -I |
| 169 | + curl -f http://localhost:8080 -s | head -50 |
| 170 | +
|
| 171 | + # Security scanning on both architectures |
| 172 | + security-scan: |
| 173 | + name: Security Scan (${{ matrix.platform }}) |
| 174 | + timeout-minutes: 30 |
| 175 | + strategy: |
| 176 | + fail-fast: false |
| 177 | + matrix: |
| 178 | + include: |
| 179 | + - platform: linux/amd64 |
| 180 | + runner: ubuntu-latest |
| 181 | + - platform: linux/arm64 |
| 182 | + runner: ubuntu-24.04-arm |
| 183 | + runs-on: ${{ matrix.runner }} |
| 184 | + needs: build |
| 185 | + permissions: |
| 186 | + contents: read |
| 187 | + security-events: write |
| 188 | + actions: read |
| 189 | + steps: |
| 190 | + - name: Checkout |
| 191 | + uses: actions/checkout@v4 |
| 192 | + |
| 193 | + - name: Install Demo App |
| 194 | + run: | |
| 195 | + # Pull composer image for the correct platform |
| 196 | + docker pull --platform ${{ matrix.platform }} composer:2 |
| 197 | + # Run composer create-project |
| 198 | + docker run --rm --platform ${{ matrix.platform }} \ |
| 199 | + -v ${{ github.workspace }}:/app \ |
| 200 | + -w /app \ |
| 201 | + composer:2 create-project \ |
| 202 | + --no-install --no-scripts \ |
| 203 | + symfony/symfony-demo app |
| 204 | +
|
| 205 | + - name: Set up Docker Buildx |
| 206 | + uses: docker/setup-buildx-action@v3 |
| 207 | + |
| 208 | + - name: Build app image for scanning |
| 209 | + uses: docker/build-push-action@v6 |
51 | 210 | with: |
52 | | - image: kubephp_app:latest kubephp_web:latest |
53 | | - args: --file=Dockerfile --print-deps |
54 | | - - name: Upload result to GitHub Code Scanning |
55 | | - uses: github/codeql-action/upload-sarif@v1 |
| 211 | + context: . |
| 212 | + platforms: ${{ matrix.platform }} |
| 213 | + target: app |
| 214 | + push: false |
| 215 | + load: true |
| 216 | + tags: kubephp-app:scan |
| 217 | + build-args: | |
| 218 | + APP_BASE_DIR=${{ env.APP_BASE_DIR }} |
| 219 | + BUILDPLATFORM=${{ matrix.platform }} |
| 220 | + TARGETPLATFORM=${{ matrix.platform }} |
| 221 | + cache-from: type=gha,scope=scan-${{ matrix.platform }} |
| 222 | + cache-to: type=gha,mode=max,scope=scan-${{ matrix.platform }} |
| 223 | + |
| 224 | + - name: Run Trivy vulnerability scanner |
| 225 | + uses: aquasecurity/trivy-action@0.33.1 |
| 226 | + with: |
| 227 | + image-ref: kubephp-app:scan |
| 228 | + format: sarif |
| 229 | + output: trivy-results.sarif |
| 230 | + severity: 'CRITICAL,HIGH' |
| 231 | + scanners: 'vuln,config,secret' |
| 232 | + exit-code: '0' |
| 233 | + # The template doesn't install PHP. The packages in this test are from the demo symfony app that is not part of this repo. |
| 234 | + trivy-args: >- |
| 235 | + --skip-dirs /app/vendor |
| 236 | + --skip-packages composer |
| 237 | +
|
| 238 | + - name: Upload Trivy results to GitHub Security |
| 239 | + uses: github/codeql-action/upload-sarif@v3 |
| 240 | + if: always() |
56 | 241 | with: |
57 | | - sarif_file: snyk.sarif |
| 242 | + sarif_file: trivy-results.sarif |
| 243 | + wait-for-processing: true |
0 commit comments