|
| 1 | +# WS1: Docker Build CI Workflow |
| 2 | + |
| 3 | +**Branch**: `feat/docker-ci` |
| 4 | +**Dependencies**: None — can start immediately |
| 5 | +**Estimated files**: 1 new |
| 6 | + |
| 7 | +## Context |
| 8 | + |
| 9 | +Per-function Docker images are built locally via `scripts/docker-build.ts` (reads `generated/<name>/Dockerfile`). There is no CI workflow to build and push these images to GHCR. |
| 10 | + |
| 11 | +### Current state |
| 12 | +- `scripts/docker-build.ts` — local build script supporting `--all`, `--only=<name>`, `--tag=<tag>`, `--registry=<registry>` |
| 13 | +- `scripts/generate.ts` — generates `generated/<name>/Dockerfile` from `templates/node-graphql/Dockerfile` with `{{name}}` replaced |
| 14 | +- Each generated Dockerfile is a 3-stage build that runs `generate.ts --only=<name>` inside the container, then `pnpm install`, `pnpm build`, `pnpm deploy` |
| 15 | +- Existing K8s CI workflow at `.github/workflows/test-k8s-deployment.yaml` only tests Knative installation, not Docker builds |
| 16 | + |
| 17 | +### Reference pattern |
| 18 | +Follow `constructive-db/.github/workflows/docker.yaml`: |
| 19 | +- Matrix strategy for multiple image targets |
| 20 | +- `docker/metadata-action@v5` for tag generation |
| 21 | +- `docker/build-push-action@v5` with `push: ${{ github.event_name != 'pull_request' }}` |
| 22 | +- GHCR login only on non-PR events |
| 23 | +- GHA build cache (`cache-from: type=gha`, `cache-to: type=gha,mode=max`) |
| 24 | +- Concurrency groups |
| 25 | + |
| 26 | +## Requirements |
| 27 | + |
| 28 | +1. Build Docker images for every function discovered in `functions/*/handler.json` |
| 29 | +2. On **push to main**: build AND push to GHCR |
| 30 | +3. On **pull request**: build only (no push) — validates the Dockerfile works |
| 31 | +4. On **workflow_dispatch**: manual trigger, build + push |
| 32 | +5. Dynamic function discovery — no hardcoded function list in the workflow |
| 33 | +6. Per-function cache scoping to avoid cache eviction between functions |
| 34 | +7. Image naming: `ghcr.io/<owner>/<name>-fn` (matches `docker-build.ts` convention) |
| 35 | +8. Tags: latest (on default branch), git SHA (short), semver (on tags) |
| 36 | + |
| 37 | +## Implementation |
| 38 | + |
| 39 | +### Create `.github/workflows/docker.yaml` |
| 40 | + |
| 41 | +```yaml |
| 42 | +name: Publish Docker Images |
| 43 | + |
| 44 | +on: |
| 45 | + push: |
| 46 | + branches: [main] |
| 47 | + pull_request: |
| 48 | + branches: [main] |
| 49 | + workflow_dispatch: {} |
| 50 | + |
| 51 | +permissions: |
| 52 | + contents: read |
| 53 | + packages: write |
| 54 | + |
| 55 | +concurrency: |
| 56 | + group: ${{ github.workflow }}-${{ github.ref }}-docker |
| 57 | + cancel-in-progress: true |
| 58 | + |
| 59 | +jobs: |
| 60 | + discover: |
| 61 | + name: Discover functions |
| 62 | + runs-on: ubuntu-latest |
| 63 | + outputs: |
| 64 | + matrix: ${{ steps.find.outputs.matrix }} |
| 65 | + steps: |
| 66 | + - name: Checkout |
| 67 | + uses: actions/checkout@v4 |
| 68 | + |
| 69 | + - name: Find functions from handler.json |
| 70 | + id: find |
| 71 | + run: | |
| 72 | + entries=$( |
| 73 | + for f in functions/*/handler.json; do |
| 74 | + [ -f "$f" ] || continue |
| 75 | + name=$(jq -r .name "$f") |
| 76 | + dir=$(dirname "$f" | xargs basename) |
| 77 | + echo "{\"name\":\"$name\",\"dir\":\"$dir\"}" |
| 78 | + done | jq -s -c '.' |
| 79 | + ) |
| 80 | + echo "matrix={\"include\":$entries}" >> "$GITHUB_OUTPUT" |
| 81 | + echo "Discovered functions: $entries" |
| 82 | +
|
| 83 | + build: |
| 84 | + name: Build ${{ matrix.name }}-fn |
| 85 | + needs: discover |
| 86 | + runs-on: ubuntu-latest |
| 87 | + if: ${{ needs.discover.outputs.matrix != '{"include":[]}' }} |
| 88 | + |
| 89 | + strategy: |
| 90 | + fail-fast: false |
| 91 | + matrix: ${{ fromJSON(needs.discover.outputs.matrix) }} |
| 92 | + |
| 93 | + env: |
| 94 | + REGISTRY: ghcr.io |
| 95 | + |
| 96 | + steps: |
| 97 | + - name: Checkout |
| 98 | + uses: actions/checkout@v4 |
| 99 | + |
| 100 | + - name: Setup Node.js |
| 101 | + uses: actions/setup-node@v4 |
| 102 | + with: |
| 103 | + node-version: '22' |
| 104 | + |
| 105 | + - name: Generate Dockerfile |
| 106 | + run: | |
| 107 | + node --experimental-strip-types scripts/generate.ts --only=${{ matrix.dir }} |
| 108 | +
|
| 109 | + - name: Set up Docker Buildx |
| 110 | + uses: docker/setup-buildx-action@v3 |
| 111 | + |
| 112 | + - name: Log in to GHCR |
| 113 | + if: github.event_name != 'pull_request' |
| 114 | + uses: docker/login-action@v3 |
| 115 | + with: |
| 116 | + registry: ${{ env.REGISTRY }} |
| 117 | + username: ${{ github.actor }} |
| 118 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 119 | + |
| 120 | + - name: Docker meta |
| 121 | + id: meta |
| 122 | + uses: docker/metadata-action@v5 |
| 123 | + with: |
| 124 | + images: ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ matrix.name }}-fn |
| 125 | + tags: | |
| 126 | + type=ref,event=tag |
| 127 | + type=semver,pattern={{version}} |
| 128 | + type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }} |
| 129 | + type=sha,format=short,prefix= |
| 130 | +
|
| 131 | + - name: Build and push |
| 132 | + uses: docker/build-push-action@v5 |
| 133 | + with: |
| 134 | + context: . |
| 135 | + file: generated/${{ matrix.dir }}/Dockerfile |
| 136 | + push: ${{ github.event_name != 'pull_request' }} |
| 137 | + tags: ${{ steps.meta.outputs.tags }} |
| 138 | + cache-from: type=gha,scope=${{ matrix.name }} |
| 139 | + cache-to: type=gha,mode=max,scope=${{ matrix.name }} |
| 140 | +``` |
| 141 | +
|
| 142 | +### Design decisions |
| 143 | +
|
| 144 | +**Dynamic matrix via discovery job**: The `discover` job reads `functions/*/handler.json` and outputs a JSON matrix. This means adding a new function only requires creating `functions/<name>/handler.json` — no workflow changes needed. |
| 145 | + |
| 146 | +**`matrix.dir` vs `matrix.name`**: The directory name (e.g., `simple-email`) may differ from the handler.json `name` field (e.g., `knative-job-example` vs dir `example`). We pass both: |
| 147 | +- `matrix.dir` — used for `--only=` flag and Dockerfile path (generate.ts filters by directory name) |
| 148 | +- `matrix.name` — used for image naming (from handler.json `name` field) |
| 149 | + |
| 150 | +**No QEMU/multi-platform**: Starting with `linux/amd64` only. Multi-platform can be added later if ARM64 nodes are used in production. |
| 151 | + |
| 152 | +**Per-function cache scope**: `scope=${{ matrix.name }}` ensures each function's Docker layers are cached independently. Without scoping, functions would evict each other's cache entries since GHA has a 10GB cache limit per repo. |
| 153 | + |
| 154 | +**Generate before build**: The workflow runs `generate.ts --only=<dir>` to create the Dockerfile. This only needs Node.js, not pnpm or full dependencies, because generate.ts uses only Node builtins (`fs`, `path`). |
| 155 | + |
| 156 | +## Verification |
| 157 | + |
| 158 | +1. **PR test**: Create a PR that adds this workflow. GitHub Actions should show: |
| 159 | + - `Discover functions` job finds all 3 functions |
| 160 | + - 3 parallel `Build <name>-fn` jobs |
| 161 | + - Each builds successfully but does NOT push (PR event) |
| 162 | + |
| 163 | +2. **Push test**: Merge to main. Verify: |
| 164 | + - Images appear at `ghcr.io/<owner>/simple-email-fn:latest` |
| 165 | + - Images appear at `ghcr.io/<owner>/send-email-link-fn:latest` |
| 166 | + - Short SHA tags are applied |
| 167 | + |
| 168 | +3. **New function test**: Add a new `functions/test-fn/handler.json` and verify it appears as a 4th matrix job automatically. |
| 169 | + |
| 170 | +4. **Manual trigger**: Use `workflow_dispatch` to trigger a build + push manually. |
0 commit comments