|
| 1 | +# =============================================================== |
| 2 | +# Multiplatform Docker Build Workflow |
| 3 | +# =============================================================== |
| 4 | +# |
| 5 | +# This workflow builds container images for multiple architectures: |
| 6 | +# - linux/amd64 (native on ubuntu-latest) |
| 7 | +# - linux/arm64 (native on ubuntu-24.04-arm) |
| 8 | +# - linux/s390x (QEMU emulation on ubuntu-latest) |
| 9 | +# |
| 10 | +# Pipeline: |
| 11 | +# 1. Lint Dockerfile with Hadolint |
| 12 | +# 2. Build platform images in parallel |
| 13 | +# 3. Create multiplatform manifest |
| 14 | +# 4. Security scan (Trivy, Grype, SBOM) |
| 15 | +# 5. Sign with Cosign (keyless OIDC) |
| 16 | +# |
| 17 | +# =============================================================== |
| 18 | + |
| 19 | +name: Multiplatform Docker Build |
| 20 | + |
| 21 | +on: |
| 22 | + push: |
| 23 | + branches: ["main"] |
| 24 | + paths: |
| 25 | + - 'Containerfile.lite' |
| 26 | + - 'mcpgateway/**' |
| 27 | + - 'plugins/**' |
| 28 | + - 'pyproject.toml' |
| 29 | + - '.github/workflows/docker-multiplatform.yml' |
| 30 | + pull_request: |
| 31 | + branches: ["main"] |
| 32 | + paths: |
| 33 | + - 'Containerfile.lite' |
| 34 | + - 'mcpgateway/**' |
| 35 | + - 'plugins/**' |
| 36 | + - 'pyproject.toml' |
| 37 | + - '.github/workflows/docker-multiplatform.yml' |
| 38 | + schedule: |
| 39 | + - cron: "17 18 * * 2" # Weekly rebuild (Tuesday 18:17 UTC) for CVE patches |
| 40 | + workflow_dispatch: |
| 41 | + inputs: |
| 42 | + platforms: |
| 43 | + description: 'Platforms to build (comma-separated)' |
| 44 | + required: false |
| 45 | + default: 'linux/amd64,linux/arm64,linux/s390x' |
| 46 | + |
| 47 | +permissions: |
| 48 | + contents: read |
| 49 | + packages: write |
| 50 | + security-events: write |
| 51 | + actions: read |
| 52 | + id-token: write |
| 53 | + |
| 54 | +env: |
| 55 | + REGISTRY: ghcr.io |
| 56 | + IMAGE_NAME: ${{ github.repository }} |
| 57 | + |
| 58 | +jobs: |
| 59 | + # --------------------------------------------------------------- |
| 60 | + # Lint Dockerfile (architecture-independent, run once) |
| 61 | + # --------------------------------------------------------------- |
| 62 | + lint: |
| 63 | + name: Lint Dockerfile |
| 64 | + runs-on: ubuntu-latest |
| 65 | + |
| 66 | + steps: |
| 67 | + - name: Checkout code |
| 68 | + uses: actions/checkout@v5 |
| 69 | + |
| 70 | + - name: Hadolint |
| 71 | + id: hadolint |
| 72 | + continue-on-error: true |
| 73 | + run: | |
| 74 | + curl -sSL https://github.com/hadolint/hadolint/releases/latest/download/hadolint-Linux-x86_64 -o /usr/local/bin/hadolint |
| 75 | + chmod +x /usr/local/bin/hadolint |
| 76 | + hadolint -f sarif Containerfile.lite > hadolint-results.sarif || true |
| 77 | +
|
| 78 | + - name: Upload Hadolint SARIF |
| 79 | + if: always() && hashFiles('hadolint-results.sarif') != '' |
| 80 | + uses: github/codeql-action/upload-sarif@v3 |
| 81 | + with: |
| 82 | + sarif_file: hadolint-results.sarif |
| 83 | + |
| 84 | + # --------------------------------------------------------------- |
| 85 | + # Build each platform in parallel |
| 86 | + # --------------------------------------------------------------- |
| 87 | + build: |
| 88 | + name: Build ${{ matrix.suffix }} |
| 89 | + needs: lint |
| 90 | + strategy: |
| 91 | + fail-fast: false |
| 92 | + matrix: |
| 93 | + include: |
| 94 | + - platform: linux/amd64 |
| 95 | + runner: ubuntu-latest |
| 96 | + suffix: amd64 |
| 97 | + - platform: linux/arm64 |
| 98 | + runner: ubuntu-24.04-arm |
| 99 | + suffix: arm64 |
| 100 | + - platform: linux/s390x |
| 101 | + runner: ubuntu-latest |
| 102 | + suffix: s390x |
| 103 | + qemu: true |
| 104 | + |
| 105 | + runs-on: ${{ matrix.runner }} |
| 106 | + |
| 107 | + steps: |
| 108 | + - name: Checkout code |
| 109 | + uses: actions/checkout@v5 |
| 110 | + |
| 111 | + - name: Set image name lowercase |
| 112 | + run: | |
| 113 | + IMAGE_NAME_LC=$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]') |
| 114 | + echo "IMAGE_NAME_LC=${IMAGE_NAME_LC}" >> $GITHUB_ENV |
| 115 | +
|
| 116 | + - name: Set up QEMU |
| 117 | + if: matrix.qemu |
| 118 | + uses: docker/setup-qemu-action@v3 |
| 119 | + with: |
| 120 | + platforms: ${{ matrix.platform }} |
| 121 | + |
| 122 | + - name: Set up Docker Buildx |
| 123 | + uses: docker/setup-buildx-action@v3 |
| 124 | + |
| 125 | + - name: Log in to GHCR |
| 126 | + uses: docker/login-action@v3 |
| 127 | + with: |
| 128 | + registry: ${{ env.REGISTRY }} |
| 129 | + username: ${{ github.actor }} |
| 130 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 131 | + |
| 132 | + - name: Extract metadata |
| 133 | + id: meta |
| 134 | + uses: docker/metadata-action@v5 |
| 135 | + with: |
| 136 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LC }} |
| 137 | + tags: | |
| 138 | + type=raw,value=${{ matrix.suffix }}-${{ github.sha }} |
| 139 | +
|
| 140 | + - name: Build and push |
| 141 | + id: build |
| 142 | + uses: docker/build-push-action@v6 |
| 143 | + with: |
| 144 | + context: . |
| 145 | + file: Containerfile.lite |
| 146 | + platforms: ${{ matrix.platform }} |
| 147 | + push: true |
| 148 | + tags: ${{ steps.meta.outputs.tags }} |
| 149 | + labels: ${{ steps.meta.outputs.labels }} |
| 150 | + cache-from: type=gha,scope=build-${{ matrix.suffix }} |
| 151 | + cache-to: type=gha,mode=max,scope=build-${{ matrix.suffix }} |
| 152 | + provenance: false |
| 153 | + |
| 154 | + - name: Export digest |
| 155 | + run: | |
| 156 | + mkdir -p /tmp/digests |
| 157 | + digest="${{ steps.build.outputs.digest }}" |
| 158 | + touch "/tmp/digests/${digest#sha256:}" |
| 159 | + echo "Digest for ${{ matrix.suffix }}: $digest" |
| 160 | +
|
| 161 | + - name: Upload digest |
| 162 | + uses: actions/upload-artifact@v4 |
| 163 | + with: |
| 164 | + name: digest-${{ matrix.suffix }} |
| 165 | + path: /tmp/digests/* |
| 166 | + if-no-files-found: error |
| 167 | + retention-days: 1 |
| 168 | + |
| 169 | + # --------------------------------------------------------------- |
| 170 | + # Create multiplatform manifest |
| 171 | + # --------------------------------------------------------------- |
| 172 | + manifest: |
| 173 | + name: Create Manifest |
| 174 | + needs: build |
| 175 | + runs-on: ubuntu-latest |
| 176 | + |
| 177 | + steps: |
| 178 | + - name: Set image name lowercase |
| 179 | + run: | |
| 180 | + IMAGE_NAME_LC=$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]') |
| 181 | + echo "IMAGE_NAME_LC=${IMAGE_NAME_LC}" >> $GITHUB_ENV |
| 182 | +
|
| 183 | + - name: Set up Docker Buildx |
| 184 | + uses: docker/setup-buildx-action@v3 |
| 185 | + |
| 186 | + - name: Log in to GHCR |
| 187 | + uses: docker/login-action@v3 |
| 188 | + with: |
| 189 | + registry: ${{ env.REGISTRY }} |
| 190 | + username: ${{ github.actor }} |
| 191 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 192 | + |
| 193 | + - name: Create and push manifest |
| 194 | + run: | |
| 195 | + SHA=${{ github.sha }} |
| 196 | + IMAGE=${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LC }} |
| 197 | +
|
| 198 | + echo "Creating multiplatform manifest..." |
| 199 | + docker buildx imagetools create \ |
| 200 | + --tag "${IMAGE}:${SHA}" \ |
| 201 | + --tag "${IMAGE}:latest" \ |
| 202 | + "${IMAGE}:amd64-${SHA}" \ |
| 203 | + "${IMAGE}:arm64-${SHA}" \ |
| 204 | + "${IMAGE}:s390x-${SHA}" |
| 205 | +
|
| 206 | + echo "Manifest created successfully" |
| 207 | +
|
| 208 | + - name: Inspect manifest |
| 209 | + run: | |
| 210 | + IMAGE=${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LC }} |
| 211 | + echo "Inspecting multiplatform manifest..." |
| 212 | + docker buildx imagetools inspect "${IMAGE}:latest" |
| 213 | +
|
| 214 | + # --------------------------------------------------------------- |
| 215 | + # Security scanning (amd64 only - sufficient for CVE detection) |
| 216 | + # --------------------------------------------------------------- |
| 217 | + scan: |
| 218 | + name: Security Scan |
| 219 | + needs: manifest |
| 220 | + runs-on: ubuntu-latest |
| 221 | + if: github.ref == 'refs/heads/main' |
| 222 | + |
| 223 | + steps: |
| 224 | + - name: Checkout code |
| 225 | + uses: actions/checkout@v5 |
| 226 | + |
| 227 | + - name: Set image name lowercase |
| 228 | + run: | |
| 229 | + IMAGE_NAME_LC=$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]') |
| 230 | + echo "IMAGE_NAME_LC=${IMAGE_NAME_LC}" >> $GITHUB_ENV |
| 231 | +
|
| 232 | + - name: Log in to GHCR |
| 233 | + uses: docker/login-action@v3 |
| 234 | + with: |
| 235 | + registry: ${{ env.REGISTRY }} |
| 236 | + username: ${{ github.actor }} |
| 237 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 238 | + |
| 239 | + - name: Pull amd64 image for scanning |
| 240 | + run: | |
| 241 | + docker pull --platform linux/amd64 ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LC }}:latest |
| 242 | +
|
| 243 | + - name: Generate SBOM (Syft) |
| 244 | + uses: anchore/sbom-action@v0 |
| 245 | + with: |
| 246 | + image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LC }}:latest |
| 247 | + output-file: sbom.spdx.json |
| 248 | + |
| 249 | + - name: Upload SBOM |
| 250 | + uses: actions/upload-artifact@v4 |
| 251 | + with: |
| 252 | + name: sbom |
| 253 | + path: sbom.spdx.json |
| 254 | + retention-days: 30 |
| 255 | + |
| 256 | + - name: Trivy vulnerability scan |
| 257 | + uses: aquasecurity/trivy-action@master |
| 258 | + with: |
| 259 | + image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LC }}:latest |
| 260 | + format: sarif |
| 261 | + output: trivy-results.sarif |
| 262 | + severity: CRITICAL,HIGH |
| 263 | + exit-code: 0 |
| 264 | + |
| 265 | + - name: Upload Trivy SARIF |
| 266 | + if: always() && hashFiles('trivy-results.sarif') != '' |
| 267 | + uses: github/codeql-action/upload-sarif@v3 |
| 268 | + with: |
| 269 | + sarif_file: trivy-results.sarif |
| 270 | + |
| 271 | + - name: Install Grype |
| 272 | + run: | |
| 273 | + curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b /usr/local/bin |
| 274 | +
|
| 275 | + - name: Grype vulnerability scan |
| 276 | + continue-on-error: true |
| 277 | + run: | |
| 278 | + grype ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LC }}:latest --scope all-layers --only-fixed |
| 279 | +
|
| 280 | + - name: Grype SARIF report |
| 281 | + continue-on-error: true |
| 282 | + run: | |
| 283 | + grype ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LC }}:latest --scope all-layers --output sarif --file grype-results.sarif |
| 284 | +
|
| 285 | + - name: Upload Grype SARIF |
| 286 | + if: always() && hashFiles('grype-results.sarif') != '' |
| 287 | + uses: github/codeql-action/upload-sarif@v3 |
| 288 | + with: |
| 289 | + sarif_file: grype-results.sarif |
| 290 | + |
| 291 | + # --------------------------------------------------------------- |
| 292 | + # Sign images with Cosign (keyless OIDC) |
| 293 | + # --------------------------------------------------------------- |
| 294 | + sign: |
| 295 | + name: Sign Images |
| 296 | + needs: [manifest, scan] |
| 297 | + runs-on: ubuntu-latest |
| 298 | + if: github.ref == 'refs/heads/main' |
| 299 | + |
| 300 | + steps: |
| 301 | + - name: Set image name lowercase |
| 302 | + run: | |
| 303 | + IMAGE_NAME_LC=$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]') |
| 304 | + echo "IMAGE_NAME_LC=${IMAGE_NAME_LC}" >> $GITHUB_ENV |
| 305 | +
|
| 306 | + - name: Install Cosign |
| 307 | + uses: sigstore/cosign-installer@v3 |
| 308 | + |
| 309 | + - name: Log in to GHCR |
| 310 | + uses: docker/login-action@v3 |
| 311 | + with: |
| 312 | + registry: ${{ env.REGISTRY }} |
| 313 | + username: ${{ github.actor }} |
| 314 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 315 | + |
| 316 | + - name: Sign multiplatform image |
| 317 | + env: |
| 318 | + COSIGN_EXPERIMENTAL: "1" |
| 319 | + run: | |
| 320 | + IMAGE=${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LC }} |
| 321 | + SHA=${{ github.sha }} |
| 322 | +
|
| 323 | + echo "Signing ${IMAGE}:latest" |
| 324 | + cosign sign --yes "${IMAGE}:latest" |
| 325 | +
|
| 326 | + echo "Signing ${IMAGE}:${SHA}" |
| 327 | + cosign sign --yes "${IMAGE}:${SHA}" |
| 328 | +
|
| 329 | + echo "Images signed successfully" |
0 commit comments