Skip to content

feat(deploy): one image for every environment, configured at runtime - #1139

Merged
cjorge-thegraph merged 1 commit into
mainfrom
ci/one-image
Nov 30, 2026
Merged

cjorge-thegraph merged 1 commit into
mainfrom
ci/one-image

Conversation

@cjorge-thegraph

@cjorge-thegraph cjorge-thegraph commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Makes the docs image environment-agnostic: the build takes no environment's values and each deployment supplies its environment at runtime, so the image tested in staging is the one a release promotes to production. Adds a release workflow that tags that image for v* releases. The production deploy workflow (ci-cd-production.yml) is unchanged and still deploys production on v* tags until the production cutover.

graph-infra companion: graphprotocol/graph-infra#1148 (draft). Merge this first (see Merge order).

Current state (main)

ci-cd-staging.yml and ci-cd-production.yml each build an image with build args ENVIRONMENT and ORIGIN. next.config.js inlines ORIGIN and a Mixpanel token chosen by ENVIRONMENT into the bundle. In practice, neither build arg reaches next build: turbo 2 runs tasks in strict env mode and passes only declared variables, so Next falls back to website/.env (ENVIRONMENT=local, ORIGIN=http://localhost:3000). Both deployed images therefore carry the same values:

  • Mixpanel: both carry the staging token e57a98…. Production page views go to the staging project. A local run of graph-docs-production:latest sends e57a9892339b2acfd02943c86b746d32, and the live https://thegraph.com/docs/en/ bundle _app-a91ab44bebbcc1d4.js contains that token and localhost:3000.
  • Links: ORIGIN is http://localhost:3000, which is not a thegraph.com origin, so the staging→staging link rewrite in _app.tsx never runs. Staging's links to thegraph.com go to production.

Nothing else differs by environment. BASE_PATH, Algolia and the GA measurement ID are the same everywhere. The sitemap uses relative URLs (siteUrl: '/') and is not in the image. There is no canonical link or robots file, and the Open Graph URL is fixed to https://thegraph.com/.

What changes

Runtime environment

  • docker-entrypoint.sh is the image's entrypoint. It requires ENVIRONMENT to be staging or production; otherwise the container exits and never becomes ready. It writes an nginx include, and nginx's sub_filter inserts <script>window.__DOCS_ENV__={"ENVIRONMENT":"…"}</script> right after <head> in every HTML response, ahead of Next's deferred scripts.
  • website/src/runtimeEnv.ts reads it in the browser. mixpanelToken() returns the environment's project token, or none (no tracking) without an environment, as in next dev. The two tokens are the ones next.config.js already held.
  • next.config.js no longer inlines ENVIRONMENT, ORIGIN or MIXPANEL_TOKEN, and website/.env drops ENVIRONMENT/ORIGIN. The Dockerfile has no ARGs.
  • Links: _app.tsx rewrites absolute thegraph.com links to the origin the page is served from, once mounted, when that origin is (www.|staging.)thegraph.com. Until then the links stay as written, in the build-time HTML and in the first client render, so hydration matches. This replaces ORIGIN with no configuration: the value was always the site's own origin.

Why this mechanism: the site is a static export served by nginx, so there is no server render to carry runtime values (Studio's approach). The pages rendered at build time must therefore be environment-independent. Injecting one inline script per HTML response needs no extra request, and no call site depends on build output. Rewriting placeholders in the bundle at container start would mutate the image and break on constant-folded comparisons. A separate /env.js would cost a render-blocking request per page load. Choosing the Mixpanel token by hostname would hard-code environments into the code. The nginx image stays at 1.16.0-alpine. Its sub_filter handles gzip responses, which I checked.

Image and workflows

  • ci-cd-staging.yml becomes image.yml. It builds ghcr.io/graphprotocol/graph-docs with no build args and checks it (.github/scripts/check-runtime-env.sh). It then pushes :<commit> on every run, and :latest only from the default branch. It no longer pushes graph-docs-staging. Runs of a branch are serialized and never cancelled (concurrency, replacing cancel-workflow-action), so latest only moves forward and each built commit keeps its image for a release.
  • ci-cd-pull-request.yml builds the same image without pushing it and runs the same check.
  • check-runtime-env.sh asserts the following. The image's config sets no ENVIRONMENT/ORIGIN. For staging and production, /docs/en/ and /docs/en/about/ start <head> with that environment's script. The container exits when ENVIRONMENT is unset, empty, local or prod.
  • New release.yml, modelled on subgraph-studio#42's release job, runs on a v* tag:
    • It runs the checks (lint, format, typecheck), then refuses a tag that is not a valid image-tag name or whose commit is not on the default branch.
    • It points graph-docs:<tag> at the digest main's build pushed as graph-docs:<commit>, as a carbon copy with no rebuild (docker buildx imagetools create --prefer-index=false), and verifies the result.
    • If main's build of the commit is still running, it waits up to 40 minutes for <commit>.
    • An existing version tag with the same digest is left alone and the run succeeds. With another digest, or on any registry error other than not found, the run fails. Releases run one at a time. Tags never move latest.
    • graph-infra will follow these tags for production once graph-infra#1128 (delivery.follow.image.releases) lands.
  • ci-cd-production.yml is unchanged. Its build args are now unused, so its image is the same agnostic build. .k8s/production sets ENVIRONMENT=production on the container, so the next tag's deploy keeps serving. .k8s/staging sets ENVIRONMENT=staging, so the overlay stays valid.
  • package.json: docker:build takes no build args; docker:up runs with ENVIRONMENT=staging.

Behaviour changes the owner should know

  • Production analytics move to the production Mixpanel project (cfeac8…) from the next production deploy after this merges, which is what next.config.js always intended. Today they go to the staging project. If production should keep reporting where it does now, say so before tagging a release.
  • Staging's links to thegraph.com (e.g. /studio, /explorer) go to staging.thegraph.com, as the code intended.

Evidence: one local build, run as staging and as production

I built Dockerfile locally with podman (localhost/graph-docs:branch, no build args). To fit an 8 GiB memory cap, the local copy of next.config.js set experimental.cpus: 2, which changes nothing but the number of static-generation workers. CI builds the same Dockerfile unchanged, and its build job passes the same check on this PR.

check-runtime-env.sh against the local image (CI's build job prints the same):

check-runtime-env: staging: /docs/en/ carries its environment
check-runtime-env: staging: /docs/en/about/ carries its environment
check-runtime-env: production: /docs/en/ carries its environment
check-runtime-env: production: /docs/en/about/ carries its environment
docs: ENVIRONMENT must be staging or production, not ''
docs: ENVIRONMENT must be staging or production, not ''
docs: ENVIRONMENT must be staging or production, not 'local'
docs: ENVIRONMENT must be staging or production, not 'prod'
check-runtime-env: without a valid ENVIRONMENT the container exits

In a browser. I loaded /docs/en/ in headless Chrome, using the container as the HTTP proxy, so the page's origin is the real hostname. Nothing left the machine: Mixpanel's request was captured, not delivered. These are the hydrated page's values after 12 s, served over plain HTTP (in deployment the links are https://):

Image Container ENVIRONMENT Host <head> starts with Mixpanel token sent thegraph.com links Hydration errors
graph-docs-production:latest (today) n/a thegraph.com <meta charset> e57a98… (staging) https://thegraph.com/… none
graph-docs-production:latest (today) n/a staging.thegraph.com <meta charset> e57a98… (staging) https://thegraph.com/… none
this PR staging staging.thegraph.com window.__DOCS_ENV__={"ENVIRONMENT":"staging"} e57a98… (staging) http://staging.thegraph.com/, /brand/, /privacy/, /terms-of-service/ none
this PR production thegraph.com window.__DOCS_ENV__={"ENVIRONMENT":"production"} cfeac8… (production) http://thegraph.com/, /brand/, /privacy/, /terms-of-service/ none

The same image served both rows: one build, with the environment set only on the container. Other subdomains (forum., status., testnet.) are left as written. The image config sets no ENVIRONMENT or ORIGIN. Neither environment serves a sitemap, robots file or canonical link, the same as today.

Production keeps deploying. ci-cd-production.yml is unchanged: it builds this Dockerfile (its ENVIRONMENT/ORIGIN build args are now unused, which Docker only warns about) and runs .github/scripts/kustomize-apply.sh production. Rendering .k8s/production the way that script does (kustomize edit set image graph-docs=…; kustomize build) gives:

graph-docs replicas 5
image ghcr.io/graphprotocol/graph-docs-production:<sha>
env [{'name': 'ENVIRONMENT', 'value': 'production'}]
readinessProbe {'httpGet': {'port': 'http'}}

Run with exactly that environment, the image answers the readiness probe's GET / with 200 and serves the production row above. A deploy that left ENVIRONMENT out would fail its readiness rather than serve with the wrong label: the container exits, the rollout stalls on its first new Pod, and the old Pods keep serving. There is no default: a default could only mislabel one of the two environments.

Merge order

  1. This PR. It pushes only ghcr.io/graphprotocol/graph-docs and no longer moves graph-docs-staging:latest, so staging stays on its current image. Kargo, which now merges image pins itself (graph-infra#1144), never pins an image that needs ENVIRONMENT before staging supplies it.
  2. First main build pushes graph-docs:<commit> and :latest.
  3. Check that the fleet pull credential (fleet-ghcr-pull), Kargo's registry credential (kargo-ghcr-read) and forge's node credential can read the new package's manifest.
  4. graphprotocol/graph-infra#1148: set its pin to that first digest, then merge it. It follows graph-docs:latest and generates docs-env (ENVIRONMENT=staging) in the stg leaves. Kargo then pins each new build as today.
  5. Once staging runs the new image, the old graph-docs-staging package can be deleted.

At no point does staging lose its followed image or receive one it cannot run. Production's tag deploy keeps working throughout (see Evidence).

Checked locally

  • pnpm lint, pnpm prettier:check, pnpm typecheck: clean.
  • actionlint: no findings in the changed workflows (only existing SC2086 notes in ci-cd-production.yml). shellcheck is clean on both scripts.
  • CI on this PR: lint, format, typecheck and build (image build plus check-runtime-env.sh) pass.

🤖 Generated with Claude Code

The image takes no environment's values at build time. Its entrypoint checks
ENVIRONMENT (staging or production) and nginx writes it into every HTML page
as window.__DOCS_ENV__; the browser picks the Mixpanel project from it. Links
to (www.|staging.)thegraph.com follow the page's own origin after mount.

image.yml builds ghcr.io/graphprotocol/graph-docs (<commit> on every run,
latest from the default branch), checks the runtime environment, and pushes.
release.yml gives a v* tag of a default-branch commit that commit's image
digest under the tag's name, without rebuilding. ci-cd-production.yml is
unchanged; .k8s/production sets ENVIRONMENT=production.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cjorge-thegraph
cjorge-thegraph marked this pull request as ready for review November 30, 2026 18:18
@cjorge-thegraph
cjorge-thegraph marked this pull request as draft November 30, 2026 18:22
@cjorge-thegraph
cjorge-thegraph marked this pull request as ready for review November 30, 2026 18:36
@cjorge-thegraph
cjorge-thegraph merged commit 57cdfac into main Nov 30, 2026
4 checks passed
@cjorge-thegraph
cjorge-thegraph deleted the ci/one-image branch November 30, 2026 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants