|
| 1 | +# ============================================================================= |
| 2 | +# Container CI - World-class container testing before merge |
| 3 | +# ============================================================================= |
| 4 | +name: Container CI |
| 5 | + |
| 6 | +on: |
| 7 | + push: |
| 8 | + branches: [main, develop] |
| 9 | + paths: |
| 10 | + - 'apps/*/Dockerfile' |
| 11 | + - 'apps/**/*.rs' |
| 12 | + - 'apps/**/*.go' |
| 13 | + - 'apps/**/*.ex' |
| 14 | + - 'apps/**/*.exs' |
| 15 | + - 'apps/web/**' |
| 16 | + - 'packages/**' |
| 17 | + - 'docker-compose.yml' |
| 18 | + - '.github/workflows/container-ci.yml' |
| 19 | + pull_request: |
| 20 | + branches: [main, develop] |
| 21 | + paths: |
| 22 | + - 'apps/*/Dockerfile' |
| 23 | + - 'apps/**/*.rs' |
| 24 | + - 'apps/**/*.go' |
| 25 | + - 'apps/**/*.ex' |
| 26 | + - 'apps/**/*.exs' |
| 27 | + - 'apps/web/**' |
| 28 | + - 'packages/**' |
| 29 | + - 'docker-compose.yml' |
| 30 | + - '.github/workflows/container-ci.yml' |
| 31 | + workflow_dispatch: |
| 32 | + inputs: |
| 33 | + containers: |
| 34 | + description: 'Containers to test (comma-separated, or "all")' |
| 35 | + required: false |
| 36 | + default: 'all' |
| 37 | + |
| 38 | +env: |
| 39 | + REGISTRY: ghcr.io |
| 40 | + IMAGE_PREFIX: ${{ github.repository_owner }}/chronos |
| 41 | + |
| 42 | +jobs: |
| 43 | + # =========================================================================== |
| 44 | + # Detect which containers changed |
| 45 | + # =========================================================================== |
| 46 | + changes: |
| 47 | + name: Detect Changes |
| 48 | + runs-on: ubuntu-latest |
| 49 | + outputs: |
| 50 | + core: ${{ steps.filter.outputs.core }} |
| 51 | + query-service: ${{ steps.filter.outputs.query-service }} |
| 52 | + mcp-server: ${{ steps.filter.outputs.mcp-server }} |
| 53 | + control-plane: ${{ steps.filter.outputs.control-plane }} |
| 54 | + web: ${{ steps.filter.outputs.web }} |
| 55 | + matrix: ${{ steps.set-matrix.outputs.matrix }} |
| 56 | + steps: |
| 57 | + - uses: actions/checkout@v4 |
| 58 | + |
| 59 | + - uses: dorny/paths-filter@v3 |
| 60 | + id: filter |
| 61 | + with: |
| 62 | + filters: | |
| 63 | + core: |
| 64 | + - 'apps/core/**' |
| 65 | + query-service: |
| 66 | + - 'apps/query-service/**' |
| 67 | + mcp-server: |
| 68 | + - 'apps/mcp-server-elixir/**' |
| 69 | + control-plane: |
| 70 | + - 'apps/control-plane/**' |
| 71 | + web: |
| 72 | + - 'apps/web/**' |
| 73 | + - 'packages/ui/**' |
| 74 | + - 'tooling/typescript/**' |
| 75 | + - 'package.json' |
| 76 | + - 'bun.lock' |
| 77 | +
|
| 78 | + - name: Set Matrix |
| 79 | + id: set-matrix |
| 80 | + run: | |
| 81 | + if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then |
| 82 | + if [[ "${{ github.event.inputs.containers }}" == "all" ]]; then |
| 83 | + echo 'matrix=["core","query-service","mcp-server","control-plane","web"]' >> $GITHUB_OUTPUT |
| 84 | + else |
| 85 | + # Convert comma-separated to JSON array |
| 86 | + containers=$(echo '${{ github.event.inputs.containers }}' | tr ',' '\n' | jq -R . | jq -s .) |
| 87 | + echo "matrix=$containers" >> $GITHUB_OUTPUT |
| 88 | + fi |
| 89 | + else |
| 90 | + # Build matrix from changed paths |
| 91 | + matrix='[]' |
| 92 | + if [[ "${{ steps.filter.outputs.core }}" == "true" ]]; then |
| 93 | + matrix=$(echo $matrix | jq '. + ["core"]') |
| 94 | + fi |
| 95 | + if [[ "${{ steps.filter.outputs.query-service }}" == "true" ]]; then |
| 96 | + matrix=$(echo $matrix | jq '. + ["query-service"]') |
| 97 | + fi |
| 98 | + if [[ "${{ steps.filter.outputs.mcp-server }}" == "true" ]]; then |
| 99 | + matrix=$(echo $matrix | jq '. + ["mcp-server"]') |
| 100 | + fi |
| 101 | + if [[ "${{ steps.filter.outputs.control-plane }}" == "true" ]]; then |
| 102 | + matrix=$(echo $matrix | jq '. + ["control-plane"]') |
| 103 | + fi |
| 104 | + if [[ "${{ steps.filter.outputs.web }}" == "true" ]]; then |
| 105 | + matrix=$(echo $matrix | jq '. + ["web"]') |
| 106 | + fi |
| 107 | + |
| 108 | + # If no changes detected (e.g., workflow file change), test all |
| 109 | + if [[ "$matrix" == "[]" ]]; then |
| 110 | + matrix='["core","query-service","mcp-server","control-plane","web"]' |
| 111 | + fi |
| 112 | + |
| 113 | + echo "matrix=$matrix" >> $GITHUB_OUTPUT |
| 114 | + fi |
| 115 | +
|
| 116 | + # =========================================================================== |
| 117 | + # Build and Test Containers |
| 118 | + # =========================================================================== |
| 119 | + build: |
| 120 | + name: Build ${{ matrix.container }} |
| 121 | + needs: changes |
| 122 | + runs-on: ubuntu-latest |
| 123 | + if: ${{ needs.changes.outputs.matrix != '[]' }} |
| 124 | + strategy: |
| 125 | + fail-fast: false |
| 126 | + matrix: |
| 127 | + container: ${{ fromJson(needs.changes.outputs.matrix) }} |
| 128 | + |
| 129 | + steps: |
| 130 | + - name: Checkout |
| 131 | + uses: actions/checkout@v4 |
| 132 | + |
| 133 | + - name: Set up Docker Buildx |
| 134 | + uses: docker/setup-buildx-action@v3 |
| 135 | + |
| 136 | + - name: Set build context and dockerfile |
| 137 | + id: config |
| 138 | + run: | |
| 139 | + case "${{ matrix.container }}" in |
| 140 | + core) |
| 141 | + echo "context=apps/core" >> $GITHUB_OUTPUT |
| 142 | + echo "dockerfile=apps/core/Dockerfile" >> $GITHUB_OUTPUT |
| 143 | + echo "image=chronos-core" >> $GITHUB_OUTPUT |
| 144 | + echo "port=3900" >> $GITHUB_OUTPUT |
| 145 | + ;; |
| 146 | + query-service) |
| 147 | + echo "context=apps/query-service" >> $GITHUB_OUTPUT |
| 148 | + echo "dockerfile=apps/query-service/Dockerfile" >> $GITHUB_OUTPUT |
| 149 | + echo "image=chronos-query-service" >> $GITHUB_OUTPUT |
| 150 | + echo "port=4000" >> $GITHUB_OUTPUT |
| 151 | + ;; |
| 152 | + mcp-server) |
| 153 | + echo "context=apps/mcp-server-elixir" >> $GITHUB_OUTPUT |
| 154 | + echo "dockerfile=apps/mcp-server-elixir/Dockerfile" >> $GITHUB_OUTPUT |
| 155 | + echo "image=chronos-mcp-server" >> $GITHUB_OUTPUT |
| 156 | + echo "port=4001" >> $GITHUB_OUTPUT |
| 157 | + ;; |
| 158 | + control-plane) |
| 159 | + echo "context=apps/control-plane" >> $GITHUB_OUTPUT |
| 160 | + echo "dockerfile=apps/control-plane/Dockerfile" >> $GITHUB_OUTPUT |
| 161 | + echo "image=chronos-control-plane" >> $GITHUB_OUTPUT |
| 162 | + echo "port=8080" >> $GITHUB_OUTPUT |
| 163 | + ;; |
| 164 | + web) |
| 165 | + echo "context=." >> $GITHUB_OUTPUT |
| 166 | + echo "dockerfile=apps/web/Dockerfile" >> $GITHUB_OUTPUT |
| 167 | + echo "image=chronos-web" >> $GITHUB_OUTPUT |
| 168 | + echo "port=3000" >> $GITHUB_OUTPUT |
| 169 | + ;; |
| 170 | + esac |
| 171 | +
|
| 172 | + - name: Build image |
| 173 | + uses: docker/build-push-action@v6 |
| 174 | + with: |
| 175 | + context: ${{ steps.config.outputs.context }} |
| 176 | + file: ${{ steps.config.outputs.dockerfile }} |
| 177 | + push: false |
| 178 | + load: true |
| 179 | + tags: ${{ steps.config.outputs.image }}:test |
| 180 | + cache-from: type=gha,scope=${{ matrix.container }} |
| 181 | + cache-to: type=gha,mode=max,scope=${{ matrix.container }} |
| 182 | + build-args: | |
| 183 | + VERSION=${{ github.sha }} |
| 184 | + REVISION=${{ github.sha }} |
| 185 | + BUILDTIME=${{ github.event.head_commit.timestamp }} |
| 186 | +
|
| 187 | + - name: Check image size |
| 188 | + run: | |
| 189 | + SIZE=$(docker images --format "{{.Size}}" ${{ steps.config.outputs.image }}:test) |
| 190 | + echo "### 📦 Image Size: $SIZE" >> $GITHUB_STEP_SUMMARY |
| 191 | + |
| 192 | + # Convert to MB for comparison |
| 193 | + SIZE_MB=$(docker images --format "{{.Size}}" ${{ steps.config.outputs.image }}:test | \ |
| 194 | + awk '{ |
| 195 | + if (index($0, "GB") > 0) { gsub("GB", "", $0); print $0 * 1024 } |
| 196 | + else if (index($0, "MB") > 0) { gsub("MB", "", $0); print $0 } |
| 197 | + else if (index($0, "KB") > 0) { gsub("KB", "", $0); print $0 / 1024 } |
| 198 | + else { print 0 } |
| 199 | + }') |
| 200 | + |
| 201 | + if (( $(echo "$SIZE_MB > 500" | bc -l) )); then |
| 202 | + echo "⚠️ Warning: Image size ($SIZE) exceeds 500MB threshold" >> $GITHUB_STEP_SUMMARY |
| 203 | + fi |
| 204 | +
|
| 205 | + - name: Test container startup |
| 206 | + run: | |
| 207 | + # Start container |
| 208 | + CONTAINER_ID=$(docker run -d -p ${{ steps.config.outputs.port }}:${{ steps.config.outputs.port }} \ |
| 209 | + ${{ steps.config.outputs.image }}:test) |
| 210 | + |
| 211 | + # Wait for container to be ready |
| 212 | + MAX_WAIT=60 |
| 213 | + WAITED=0 |
| 214 | + |
| 215 | + while [ $WAITED -lt $MAX_WAIT ]; do |
| 216 | + STATUS=$(docker inspect --format='{{.State.Status}}' $CONTAINER_ID) |
| 217 | + |
| 218 | + if [ "$STATUS" = "exited" ]; then |
| 219 | + EXIT_CODE=$(docker inspect --format='{{.State.ExitCode}}' $CONTAINER_ID) |
| 220 | + echo "❌ Container exited with code $EXIT_CODE" >> $GITHUB_STEP_SUMMARY |
| 221 | + docker logs $CONTAINER_ID |
| 222 | + docker rm -f $CONTAINER_ID |
| 223 | + exit 1 |
| 224 | + fi |
| 225 | + |
| 226 | + # Check if container has healthcheck |
| 227 | + HEALTH=$(docker inspect --format='{{if .State.Health}}{{.State.Health.Status}}{{else}}no-healthcheck{{end}}' $CONTAINER_ID) |
| 228 | + |
| 229 | + if [ "$HEALTH" = "healthy" ] || [ "$HEALTH" = "no-healthcheck" ]; then |
| 230 | + if [ "$STATUS" = "running" ]; then |
| 231 | + echo "✅ Container started successfully in ${WAITED}s" >> $GITHUB_STEP_SUMMARY |
| 232 | + docker rm -f $CONTAINER_ID |
| 233 | + exit 0 |
| 234 | + fi |
| 235 | + fi |
| 236 | + |
| 237 | + sleep 1 |
| 238 | + WAITED=$((WAITED + 1)) |
| 239 | + done |
| 240 | + |
| 241 | + echo "❌ Container failed to start within ${MAX_WAIT}s" >> $GITHUB_STEP_SUMMARY |
| 242 | + docker logs $CONTAINER_ID |
| 243 | + docker rm -f $CONTAINER_ID |
| 244 | + exit 1 |
| 245 | +
|
| 246 | + - name: Run Trivy vulnerability scan |
| 247 | + uses: aquasecurity/trivy-action@master |
| 248 | + with: |
| 249 | + image-ref: '${{ steps.config.outputs.image }}:test' |
| 250 | + format: 'sarif' |
| 251 | + output: 'trivy-results.sarif' |
| 252 | + severity: 'CRITICAL,HIGH' |
| 253 | + exit-code: '0' # Don't fail on vulnerabilities, just report |
| 254 | + |
| 255 | + - name: Upload Trivy scan results |
| 256 | + uses: github/codeql-action/upload-sarif@v3 |
| 257 | + if: always() |
| 258 | + with: |
| 259 | + sarif_file: 'trivy-results.sarif' |
| 260 | + |
| 261 | + - name: Security scan summary |
| 262 | + run: | |
| 263 | + if [ -f trivy-results.sarif ]; then |
| 264 | + CRITICAL=$(cat trivy-results.sarif | jq '[.runs[].results[] | select(.level == "error")] | length') |
| 265 | + HIGH=$(cat trivy-results.sarif | jq '[.runs[].results[] | select(.level == "warning")] | length') |
| 266 | + |
| 267 | + if [ "$CRITICAL" -gt 0 ]; then |
| 268 | + echo "⚠️ Found $CRITICAL critical vulnerabilities" >> $GITHUB_STEP_SUMMARY |
| 269 | + fi |
| 270 | + if [ "$HIGH" -gt 0 ]; then |
| 271 | + echo "⚠️ Found $HIGH high vulnerabilities" >> $GITHUB_STEP_SUMMARY |
| 272 | + fi |
| 273 | + if [ "$CRITICAL" -eq 0 ] && [ "$HIGH" -eq 0 ]; then |
| 274 | + echo "✅ No critical or high vulnerabilities found" >> $GITHUB_STEP_SUMMARY |
| 275 | + fi |
| 276 | + fi |
| 277 | +
|
| 278 | + # =========================================================================== |
| 279 | + # Docker Compose Integration Test |
| 280 | + # =========================================================================== |
| 281 | + integration: |
| 282 | + name: Integration Test |
| 283 | + needs: [changes, build] |
| 284 | + runs-on: ubuntu-latest |
| 285 | + if: | |
| 286 | + always() && |
| 287 | + needs.build.result == 'success' && |
| 288 | + (contains(fromJson(needs.changes.outputs.matrix), 'core') || |
| 289 | + contains(fromJson(needs.changes.outputs.matrix), 'query-service')) |
| 290 | + |
| 291 | + steps: |
| 292 | + - name: Checkout |
| 293 | + uses: actions/checkout@v4 |
| 294 | + |
| 295 | + - name: Build all services |
| 296 | + run: | |
| 297 | + docker compose build core query-service |
| 298 | +
|
| 299 | + - name: Start services |
| 300 | + run: | |
| 301 | + docker compose up -d core |
| 302 | + |
| 303 | + # Wait for core to be healthy |
| 304 | + timeout 60 bash -c 'until docker compose exec -T core wget --spider -q http://localhost:3900/health; do sleep 2; done' |
| 305 | + |
| 306 | + echo "✅ Core service is healthy" >> $GITHUB_STEP_SUMMARY |
| 307 | +
|
| 308 | + - name: Run integration tests |
| 309 | + run: | |
| 310 | + # Basic health check |
| 311 | + curl -sf http://localhost:3900/health || exit 1 |
| 312 | + |
| 313 | + # Test event creation |
| 314 | + RESPONSE=$(curl -sf -X POST http://localhost:3900/api/v1/events \ |
| 315 | + -H "Content-Type: application/json" \ |
| 316 | + -d '{"stream_id":"test-stream","event_type":"TestEvent","data":{"message":"hello"}}') |
| 317 | + |
| 318 | + if echo "$RESPONSE" | jq -e '.event_id' > /dev/null; then |
| 319 | + echo "✅ Event creation test passed" >> $GITHUB_STEP_SUMMARY |
| 320 | + else |
| 321 | + echo "❌ Event creation test failed" >> $GITHUB_STEP_SUMMARY |
| 322 | + exit 1 |
| 323 | + fi |
| 324 | +
|
| 325 | + - name: Cleanup |
| 326 | + if: always() |
| 327 | + run: docker compose down -v |
| 328 | + |
| 329 | + # =========================================================================== |
| 330 | + # Summary Report |
| 331 | + # =========================================================================== |
| 332 | + summary: |
| 333 | + name: Test Summary |
| 334 | + needs: [changes, build, integration] |
| 335 | + runs-on: ubuntu-latest |
| 336 | + if: always() |
| 337 | + |
| 338 | + steps: |
| 339 | + - name: Generate Summary |
| 340 | + run: | |
| 341 | + echo "# 🐳 Container CI Summary" >> $GITHUB_STEP_SUMMARY |
| 342 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 343 | + echo "| Container | Build | Startup | Security |" >> $GITHUB_STEP_SUMMARY |
| 344 | + echo "|-----------|-------|---------|----------|" >> $GITHUB_STEP_SUMMARY |
| 345 | + |
| 346 | + # This is a simplified summary - actual results come from build job |
| 347 | + if [ "${{ needs.build.result }}" == "success" ]; then |
| 348 | + echo "| All | ✅ | ✅ | ✅ |" >> $GITHUB_STEP_SUMMARY |
| 349 | + else |
| 350 | + echo "| All | ❌ | - | - |" >> $GITHUB_STEP_SUMMARY |
| 351 | + fi |
| 352 | + |
| 353 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 354 | + |
| 355 | + if [ "${{ needs.integration.result }}" == "success" ]; then |
| 356 | + echo "**Integration Tests:** ✅ Passed" >> $GITHUB_STEP_SUMMARY |
| 357 | + elif [ "${{ needs.integration.result }}" == "skipped" ]; then |
| 358 | + echo "**Integration Tests:** ⏭️ Skipped" >> $GITHUB_STEP_SUMMARY |
| 359 | + else |
| 360 | + echo "**Integration Tests:** ❌ Failed" >> $GITHUB_STEP_SUMMARY |
| 361 | + fi |
| 362 | +
|
| 363 | + - name: Check overall status |
| 364 | + if: needs.build.result != 'success' |
| 365 | + run: exit 1 |
0 commit comments