Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions .github/scripts/check-runtime-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/sh
# Checks that the docs image takes its environment at runtime (docker-entrypoint.sh):
# - the image's configuration names no environment;
# - for staging and production, the container serves pages that carry that environment right
# after <head> (window.__DOCS_ENV__);
# - without ENVIRONMENT, or with a value other than those two, the container exits instead.
# Usage: check-runtime-env.sh IMAGE (DOCKER selects the client; docker by default).
set -eu

image="$1"
docker="${DOCKER:-docker}"
container=""

fail() {
echo "check-runtime-env: $*" >&2
exit 1
}

cleanup() {
if [ -n "$container" ]; then "$docker" rm -f "$container" >/dev/null; fi
}
trap cleanup EXIT

config_env=$("$docker" image inspect --format '{{range .Config.Env}}{{println .}}{{end}}' "$image")
if printf '%s\n' "$config_env" | grep -E '^(ENVIRONMENT|ORIGIN)='; then
fail "the image sets an environment's value"
fi

for environment in staging production; do
container=$("$docker" run -d -e ENVIRONMENT="$environment" -p 127.0.0.1::80 "$image")
port=$("$docker" port "$container" 80/tcp | head -n 1 | sed 's/.*://')
expected="<head><script>window.__DOCS_ENV__={\"ENVIRONMENT\":\"$environment\"}</script>"
for path in /docs/en/ /docs/en/about/; do
page=""
attempt=0
until page=$(curl -fsS "http://127.0.0.1:$port$path"); do
attempt=$((attempt + 1))
[ "$attempt" -lt 30 ] || fail "$environment: no answer on $path"
sleep 1
done
case "$page" in
*"$expected"*) echo "check-runtime-env: $environment: $path carries its environment" ;;
*) fail "$environment: $path does not start its <head> with $expected" ;;
esac
done
"$docker" rm -f "$container" >/dev/null
container=""
done

if "$docker" run --rm "$image" true; then
fail "the container started without ENVIRONMENT"
fi
for value in "" local prod; do
if "$docker" run --rm -e ENVIRONMENT="$value" "$image" true; then
fail "the container started with ENVIRONMENT='$value'"
fi
done
echo "check-runtime-env: without a valid ENVIRONMENT the container exits"
12 changes: 7 additions & 5 deletions .github/workflows/ci-cd-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Pull Request CI/CD
on: pull_request

env:
BASE_IMAGE: ghcr.io/graphprotocol/graph-docs-staging
IMAGE: ghcr.io/graphprotocol/graph-docs

jobs:
lint:
Expand Down Expand Up @@ -100,8 +100,10 @@ jobs:
uses: docker/build-push-action@v5
with:
context: . # required to respect .dockerignore
cache-from: type=registry,ref=${{ env.BASE_IMAGE }}:latest
build-args: |
ENVIRONMENT=staging
ORIGIN=https://staging.thegraph.com
cache-from: type=registry,ref=${{ env.IMAGE }}:latest
tags: ${{ env.IMAGE }}:${{ github.sha }}
load: true
push: false

- name: Check the runtime environment
run: .github/scripts/check-runtime-env.sh "$IMAGE:$GITHUB_SHA"
53 changes: 0 additions & 53 deletions .github/workflows/ci-cd-staging.yml

This file was deleted.

67 changes: 67 additions & 0 deletions .github/workflows/image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Image

on:
workflow_dispatch:
push:
branches:
- main

# Builds the docs image, ghcr.io/graphprotocol/graph-docs, and pushes it to ghcr.io; it deploys
# nothing. The image is the same for every environment: the build takes no environment's values,
# and each deployment sets the container's ENVIRONMENT (docker-entrypoint.sh). Every run pushes
# <commit>; only builds of the default branch move latest.
# Staging runs on stg-apps-us-central1, delivered by Argo CD and Kargo from
# graphprotocol/graph-infra: Kargo follows latest. A v* tag releases a commit's image under the
# tag's name (release.yml).

# Runs of one branch build one at a time and in order, so latest only moves forward. A run is not
# cancelled: every commit it builds keeps its <commit> image, which a release tags.
concurrency:
group: image-${{ github.ref }}
cancel-in-progress: false

permissions:
contents: read
packages: write

env:
IMAGE: ghcr.io/graphprotocol/graph-docs

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build Docker image
uses: docker/build-push-action@v5
with:
context: . # required to respect .dockerignore
cache-from: type=registry,ref=${{ env.IMAGE }}:latest
cache-to: type=inline
labels: |
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
org.opencontainers.image.revision=${{ github.sha }}
tags: ${{ env.IMAGE }}:${{ github.sha }}
load: true

- name: Check the runtime environment
run: .github/scripts/check-runtime-env.sh "$IMAGE:$GITHUB_SHA"

- name: Push Docker image
env:
LATEST: ${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}
run: |
docker push "$IMAGE:$GITHUB_SHA"
if [ "$LATEST" = true ]; then
docker tag "$IMAGE:$GITHUB_SHA" "$IMAGE:latest"
docker push "$IMAGE:latest"
fi
129 changes: 129 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
name: Release

on:
push:
tags:
- v*

# A v* tag of a commit on the default branch, once the checks pass, gets an image tag of the same
# name: ghcr.io/graphprotocol/graph-docs:<tag> is the digest the default branch's build pushed as
# <commit> (image.yml), nothing rebuilt. A version tag never moves to another image, and tags never
# move latest. Production Kargo follows these version tags (graphprotocol/graph-infra).
# ci-cd-production.yml still builds and deploys production from the same tags, separately.

# Releases run one at a time, and none is cancelled.
concurrency:
group: release
cancel-in-progress: false

permissions:
contents: read

env:
IMAGE: ghcr.io/graphprotocol/graph-docs

jobs:
checks:
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Set up env
uses: the-guild-org/shared-config/setup@v1
with:
node-version: 22

- name: Install dependencies
run: pnpm install

- name: Lint
run: pnpm lint

- name: Check formatting
run: pnpm prettier:check

- name: Typecheck
run: pnpm typecheck

release:
runs-on: ubuntu-latest
timeout-minutes: 60
needs: [checks]
permissions:
contents: read
packages: write
steps:
- name: Check the tag
run: |
if ! [[ "$GITHUB_REF_NAME" =~ ^[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}$ ]]; then
echo "$GITHUB_REF_NAME is not a valid image tag." >&2
exit 1
fi

- name: Check out the repo
uses: actions/checkout@v4
with:
# The default branch's history, to check the release is on it.
fetch-depth: 0
filter: blob:none

- name: Check the release is on the default branch
env:
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
run: |
git fetch --no-tags origin "+refs/heads/$DEFAULT_BRANCH:refs/remotes/origin/$DEFAULT_BRANCH"
if ! git merge-base --is-ancestor "$GITHUB_SHA" "origin/$DEFAULT_BRANCH"; then
echo "$GITHUB_REF_NAME ($GITHUB_SHA) is not on $DEFAULT_BRANCH; only its commits are released." >&2
exit 1
fi

- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Tag the image
run: |
set -o pipefail
tag=$GITHUB_REF_NAME
error=$(mktemp)
# Prints the digest of $1; fails on any registry error, "not found" included.
digest() {
docker buildx imagetools inspect --format '{{.Manifest.Digest}}' "$1" 2> "$error"
}
not_found() {
grep -qiE 'not found|manifest unknown' "$error"
}
# The default branch's build of the commit may still be running when the tag arrives:
# wait for its image, up to 40 minutes.
attempt=0
until source=$(digest "$IMAGE:$GITHUB_SHA"); do
if ! not_found; then
cat "$error" >&2
exit 1
fi
attempt=$((attempt + 1))
if [ "$attempt" -ge 80 ]; then
echo "$IMAGE:$GITHUB_SHA does not exist: the default branch's build of $GITHUB_SHA pushes it (image.yml)." >&2
exit 1
fi
sleep 30
done
if current=$(digest "$IMAGE:$tag"); then
if [ "$current" != "$source" ]; then
echo "$IMAGE:$tag is already $current, not $source ($GITHUB_SHA); a version tag never moves." >&2
exit 1
fi
echo "$IMAGE:$tag is already $source ($GITHUB_SHA)."
exit 0
elif ! not_found; then
cat "$error" >&2
exit 1
fi
# A carbon copy of the manifest: the version tag gets the commit image's digest.
docker buildx imagetools create --prefer-index=false --tag "$IMAGE:$tag" "$IMAGE@$source"
[ "$(digest "$IMAGE:$tag")" = "$source" ] || { echo "$IMAGE:$tag is not $source" >&2; exit 1; }
echo "$IMAGE:$tag: $source ($GITHUB_SHA)"
7 changes: 7 additions & 0 deletions .k8s/production/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ metadata:
name: graph-docs
spec:
replicas: 5
template:
spec:
containers:
- name: app
env:
- name: ENVIRONMENT
value: production
12 changes: 12 additions & 0 deletions .k8s/staging/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: graph-docs
spec:
template:
spec:
containers:
- name: app
env:
- name: ENVIRONMENT
value: staging
2 changes: 2 additions & 0 deletions .k8s/staging/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ bases:
- ../base
resources:
- ingress.yaml
patchesStrategicMerge:
- deployment.yaml
10 changes: 4 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
# One image for every environment: the build takes no environment's values. The container reads
# ENVIRONMENT when it starts (docker-entrypoint.sh).
FROM node:22-alpine AS builder

ARG ENVIRONMENT
ARG ORIGIN

ENV ENVIRONMENT=$ENVIRONMENT
ENV ORIGIN=$ORIGIN

ENV PNPM_HOME="/usr/bin"
ENV NODE_OPTIONS="--max-old-space-size=8192"

Expand All @@ -24,8 +20,10 @@ RUN pnpm build
FROM nginx:1.16.0-alpine

COPY --from=builder ./app/nginx.conf /etc/nginx/
COPY --from=builder /app/docker-entrypoint.sh /docker-entrypoint.sh
COPY --from=builder /app/out /usr/share/nginx/html

EXPOSE 80/tcp

ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]
Loading
Loading