|
| 1 | +name: Dockerfiles CI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main, master] |
| 6 | + pull_request: |
| 7 | + branches: [main, master] |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: docker-quality-${{ github.workflow }}-${{ github.ref }} |
| 12 | + cancel-in-progress: true |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: read |
| 16 | + |
| 17 | +jobs: |
| 18 | + lint-dockerfiles: |
| 19 | + name: Lint Dockerfiles (hadolint) |
| 20 | + runs-on: self-hosted # ubuntu-latest |
| 21 | + steps: |
| 22 | + - name: Checkout |
| 23 | + uses: actions/checkout@v4 |
| 24 | + |
| 25 | + - name: Hadolint (Dockerfile.api) |
| 26 | + uses: hadolint/hadolint-action@v3.1.0 |
| 27 | + with: |
| 28 | + dockerfile: Dockerfile.api |
| 29 | + |
| 30 | + - name: Hadolint (Dockerfile.nginx) |
| 31 | + uses: hadolint/hadolint-action@v3.1.0 |
| 32 | + with: |
| 33 | + dockerfile: Dockerfile.nginx |
| 34 | + |
| 35 | + - name: Validate docker-compose.yml |
| 36 | + run: docker compose -f docker-compose.yml config -q |
| 37 | + |
| 38 | + build-and-test: |
| 39 | + name: Build & smoke tests |
| 40 | + runs-on: self-hosted # ubuntu-latest |
| 41 | + needs: lint-dockerfiles |
| 42 | + strategy: |
| 43 | + matrix: |
| 44 | + include: |
| 45 | + - name: api |
| 46 | + dockerfile: Dockerfile.api |
| 47 | + context: . |
| 48 | + - name: nginx |
| 49 | + dockerfile: Dockerfile.nginx |
| 50 | + context: . |
| 51 | + steps: |
| 52 | + - name: Checkout |
| 53 | + uses: actions/checkout@v4 |
| 54 | + |
| 55 | + - name: Set up Docker Buildx |
| 56 | + uses: docker/setup-buildx-action@v3 |
| 57 | + |
| 58 | + - name: Build image (${{ matrix.name }}) |
| 59 | + uses: docker/build-push-action@v6 |
| 60 | + with: |
| 61 | + context: ${{ matrix.context }} |
| 62 | + file: ${{ matrix.dockerfile }} |
| 63 | + tags: local/${{ matrix.name }}:ci-${{ github.sha }} |
| 64 | + load: true |
| 65 | + push: false |
| 66 | + provenance: false |
| 67 | + sbom: false |
| 68 | + cache-from: type=registry,ref=local/${{ matrix.name }}:buildcache |
| 69 | + cache-to: type=inline |
| 70 | + |
| 71 | + - name: Run NGINX container (smoke test) |
| 72 | + if: matrix.name == 'nginx' |
| 73 | + run: | |
| 74 | + set -euo pipefail |
| 75 | + CONTAINER_NAME=nginx-smoke-${GITHUB_RUN_ID} |
| 76 | + docker run -d --rm --name "$CONTAINER_NAME" -p 8080:80 local/nginx:ci-${GITHUB_SHA} |
| 77 | +
|
| 78 | + for i in {1..30}; do |
| 79 | + STATUS=$(docker inspect --format='{{json .State.Health.Status}}' "$CONTAINER_NAME" | tr -d '"') |
| 80 | + if [ "$STATUS" = "healthy" ]; then |
| 81 | + echo "Container is healthy." |
| 82 | + break |
| 83 | + fi |
| 84 | + sleep 2 |
| 85 | + done |
| 86 | +
|
| 87 | + STATUS=$(docker inspect --format='{{json .State.Health.Status}}' "$CONTAINER_NAME" | tr -d '"') |
| 88 | + if [ "$STATUS" != "healthy" ]; then |
| 89 | + echo "::error::Container did not become healthy. Status=$STATUS" |
| 90 | + docker logs "$CONTAINER_NAME" || true |
| 91 | + docker inspect "$CONTAINER_NAME" || true |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | +
|
| 95 | + curl -fsS http://localhost:8080/ >/dev/null |
| 96 | +
|
| 97 | + docker kill "$CONTAINER_NAME" >/dev/null |
| 98 | +
|
| 99 | + - name: Run API container (smoke run) |
| 100 | + if: matrix.name == 'api' |
| 101 | + run: | |
| 102 | + set -euo pipefail |
| 103 | + CONTAINER_NAME=api-smoke-${GITHUB_RUN_ID} |
| 104 | + docker run -d --rm --name "$CONTAINER_NAME" local/api:ci-${GITHUB_SHA} || (echo "::warning::API container failed to start"; exit 1) |
| 105 | + sleep 5 |
| 106 | + if ! docker ps --filter "name=$CONTAINER_NAME" --format '{{.Names}}' | grep -q "$CONTAINER_NAME"; then |
| 107 | + echo "::error::API container is not running after 5s." |
| 108 | + docker logs "$CONTAINER_NAME" || true |
| 109 | + exit 1 |
| 110 | + fi |
| 111 | + docker kill "$CONTAINER_NAME" >/dev/null |
0 commit comments