Files
service-state/ci/lolympus-tournament-support/tasks.yaml
T

571 lines
19 KiB
YAML

apiVersion: tekton.dev/v1
kind: Task
metadata:
name: lolympus-clone
spec:
description: Clone and verify the exact source revision from Gitea.
params:
- name: revision
type: string
workspaces:
- name: source
- name: credentials
readOnly: true
steps:
- name: clone
image: docker.io/alpine/git:2.49.1@sha256:c0280cf9572316299b08544065d3bf35db65043d5e3963982ec50647d2746e26
computeResources:
requests: {cpu: 100m, memory: 128Mi}
limits: {cpu: "1", memory: 1Gi}
env:
- name: REPO_URL
value: https://git.dahyeon.us/dahyeon/lolympus-tournament-support.git
script: |
#!/bin/sh
set -eu
SOURCE_DIR="$(workspaces.source.path)"
REVISION="$(params.revision)"
AUTH_HOME="$(mktemp -d)"
trap 'rm -rf "$AUTH_HOME"' EXIT
USERNAME="$(cat "$(workspaces.credentials.path)/username")"
PASSWORD="$(cat "$(workspaces.credentials.path)/password")"
cat >"$AUTH_HOME/.netrc" <<EOF
machine git.dahyeon.us
login $USERNAME
password $PASSWORD
EOF
chmod 0600 "$AUTH_HOME/.netrc"
export HOME="$AUTH_HOME"
find "$SOURCE_DIR" -mindepth 1 -maxdepth 1 -exec rm -rf {} +
git clone --quiet --no-tags "$REPO_URL" "$SOURCE_DIR"
git -C "$SOURCE_DIR" checkout --quiet --detach "$REVISION"
ACTUAL_REVISION="$(git -C "$SOURCE_DIR" rev-parse HEAD)"
if [ "$ACTUAL_REVISION" != "$REVISION" ]; then
echo "Expected revision $REVISION, got $ACTUAL_REVISION" >&2
exit 1
fi
---
apiVersion: tekton.dev/v1
kind: Task
metadata:
name: lolympus-quality
spec:
description: Install dependencies, lint, test, build, and audit the monorepo.
workspaces:
- name: source
stepTemplate:
image: docker.io/library/node:26-bookworm-slim@sha256:2d49d876e96237d76de412761cf05dbfe5aee325cc4406a4d41d5824c5bb8beb
workingDir: $(workspaces.source.path)
env:
- name: CI
value: "true"
- name: NPM_CONFIG_CACHE
value: $(workspaces.source.path)/.ci/npm-cache
- name: PATH
value: $(workspaces.source.path)/.ci/pnpm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
steps:
- name: install
computeResources:
requests: {cpu: 200m, memory: 512Mi}
limits: {cpu: "2", memory: 4Gi}
script: |
#!/bin/sh
set -eu
mkdir -p .ci/pnpm .ci/npm-cache
npm install --global \
--prefix "$(workspaces.source.path)/.ci/pnpm" pnpm@10.4.1
pnpm install --frozen-lockfile
- name: lint
computeResources:
requests: {cpu: 100m, memory: 256Mi}
limits: {cpu: "2", memory: 2Gi}
script: |
#!/bin/sh
set -eu
pnpm --filter api exec eslint \
"{src,apps,libs,test}/**/*.ts" --max-warnings=0
pnpm --filter web lint
- name: test
computeResources:
requests: {cpu: 100m, memory: 512Mi}
limits: {cpu: "3", memory: 4Gi}
script: |
#!/bin/sh
set -eu
pnpm --filter api test --runInBand
pnpm --filter web test
node --test scripts/deploy-cloudflare-pages.test.mjs
- name: build
computeResources:
requests: {cpu: 200m, memory: 512Mi}
limits: {cpu: "3", memory: 4Gi}
script: |
#!/bin/sh
set -eu
pnpm --filter api build
pnpm --filter web build:staging
- name: audit
computeResources:
requests: {cpu: 50m, memory: 128Mi}
limits: {cpu: "1", memory: 1Gi}
script: |
#!/bin/sh
set -eu
pnpm audit --prod --audit-level high
---
apiVersion: tekton.dev/v1
kind: Task
metadata:
name: lolympus-build-platform
spec:
description: Clone an exact revision and build one native API platform with rootless BuildKit.
params:
- name: revision
type: string
- name: platform
type: string
- name: image
type: string
results:
- name: image-url
description: Pushed image repository and tag.
- name: image-digest
description: Immutable digest returned by the registry.
workspaces:
- name: credentials
readOnly: true
- name: dockerconfig
readOnly: true
volumes:
- name: build-source
emptyDir: {}
- name: buildkit-state
emptyDir: {}
steps:
- name: clone-source
image: docker.io/alpine/git:2.49.1@sha256:c0280cf9572316299b08544065d3bf35db65043d5e3963982ec50647d2746e26
computeResources:
requests: {cpu: 100m, memory: 128Mi}
limits: {cpu: "1", memory: 1Gi}
securityContext:
runAsNonRoot: true
runAsUser: 1000
runAsGroup: 1000
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
workspaces:
- name: credentials
volumeMounts:
- name: build-source
mountPath: /workspace/build-source
env:
- name: REPO_URL
value: https://git.dahyeon.us/dahyeon/lolympus-tournament-support.git
script: |
#!/bin/sh
set -eu
SOURCE_DIR=/workspace/build-source
REVISION="$(params.revision)"
AUTH_HOME="$(mktemp -d)"
trap 'rm -rf "$AUTH_HOME"' EXIT
USERNAME="$(cat "$(workspaces.credentials.path)/username")"
PASSWORD="$(cat "$(workspaces.credentials.path)/password")"
cat >"$AUTH_HOME/.netrc" <<EOF
machine git.dahyeon.us
login $USERNAME
password $PASSWORD
EOF
chmod 0600 "$AUTH_HOME/.netrc"
export HOME="$AUTH_HOME"
find "$SOURCE_DIR" -mindepth 1 -maxdepth 1 -exec rm -rf {} +
git clone --quiet --no-tags "$REPO_URL" "$SOURCE_DIR"
git -C "$SOURCE_DIR" checkout --quiet --detach "$REVISION"
ACTUAL_REVISION="$(git -C "$SOURCE_DIR" rev-parse HEAD)"
if [ "$ACTUAL_REVISION" != "$REVISION" ]; then
echo "Expected revision $REVISION, got $ACTUAL_REVISION" >&2
exit 1
fi
- name: build-and-push
image: docker.io/moby/buildkit:v0.31.1-rootless@sha256:946cf909534b789c9b84af75e6d4f8cb23c3b9cb387daec5ba0f4878a3e4647c
workingDir: /workspace/build-source
computeResources:
requests: {cpu: "1", memory: 2Gi}
limits: {cpu: "4", memory: 8Gi}
securityContext:
privileged: false
allowPrivilegeEscalation: true
runAsNonRoot: true
runAsUser: 1000
runAsGroup: 1000
seccompProfile:
type: Unconfined
appArmorProfile:
type: Unconfined
workspaces:
- name: dockerconfig
volumeMounts:
- name: build-source
mountPath: /workspace/build-source
- name: buildkit-state
mountPath: /home/user/.local/share/buildkit
env:
- name: BUILDKITD_FLAGS
value: --oci-worker-no-process-sandbox
- name: DOCKER_CONFIG
value: $(workspaces.dockerconfig.path)
script: |
#!/bin/sh
set -eu
case "$(params.platform)" in
linux/amd64|linux/arm64) ;;
*)
echo "Unsupported platform: $(params.platform)" >&2
exit 1
;;
esac
buildctl-daemonless.sh build \
--frontend dockerfile.v0 \
--local context=. \
--local dockerfile=. \
--opt filename=Dockerfile \
--opt platform=$(params.platform) \
--output "type=image,name=$(params.image),push=true" \
--metadata-file "$(results.image-digest.path)"
- name: record-digest
image: docker.io/mikefarah/yq:4.47.2@sha256:76def1f56f456ecc1c3173ea275218ee17139bc2018c5a07887b15afd88ec03e
workingDir: /workspace/build-source
computeResources:
requests: {cpu: 50m, memory: 64Mi}
limits: {cpu: 250m, memory: 256Mi}
securityContext:
runAsNonRoot: true
runAsUser: 1000
runAsGroup: 1000
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
volumeMounts:
- name: build-source
mountPath: /workspace/build-source
script: |
#!/bin/sh
set -eu
DIGEST="$(yq -r '."containerimage.digest"' "$(results.image-digest.path)")"
if [ -z "$DIGEST" ] || [ "$DIGEST" = "null" ]; then
echo "BuildKit did not return an image digest" >&2
exit 1
fi
printf '%s' "$(params.image)" >"$(results.image-url.path)"
printf '%s' "$DIGEST" >"$(results.image-digest.path)"
---
apiVersion: tekton.dev/v1
kind: Task
metadata:
name: lolympus-create-image-index
spec:
description: Publish an OCI image index from the native amd64 and arm64 images.
params:
- name: image
type: string
- name: amd64-image
type: string
- name: amd64-digest
type: string
- name: arm64-image
type: string
- name: arm64-digest
type: string
results:
- name: image-url
description: Pushed multi-platform image repository and tag.
- name: image-digest
description: Immutable OCI image index digest returned by the registry.
workspaces:
- name: dockerconfig
readOnly: true
volumes:
- name: regctl-home
emptyDir: {}
steps:
- name: create-index
image: ghcr.io/regclient/regctl:v0.11.5-alpine@sha256:795e27a968ec0681cf1f4fd4bb83a880a1bc4698afb38efd5c961f9264820565
computeResources:
requests: {cpu: 50m, memory: 64Mi}
limits: {cpu: 500m, memory: 256Mi}
securityContext:
runAsNonRoot: true
runAsUser: 1000
runAsGroup: 1000
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
volumeMounts:
- name: regctl-home
mountPath: /home/appuser
env:
- name: HOME
value: /home/appuser
- name: FINAL_IMAGE
value: $(params.image)
- name: AMD64_IMAGE
value: $(params.amd64-image)
- name: AMD64_DIGEST
value: $(params.amd64-digest)
- name: ARM64_IMAGE
value: $(params.arm64-image)
- name: ARM64_DIGEST
value: $(params.arm64-digest)
script: |
#!/bin/sh
set -eu
mkdir -p "$HOME/.docker"
cp "$(workspaces.dockerconfig.path)/config.json" "$HOME/.docker/config.json"
chmod 0600 "$HOME/.docker/config.json"
for DIGEST in "$AMD64_DIGEST" "$ARM64_DIGEST"; do
if ! printf '%s\n' "$DIGEST" | grep -Eq '^sha256:[0-9a-f]{64}$'; then
echo "Invalid image digest: $DIGEST" >&2
exit 1
fi
done
RESOLVED_AMD64="$(regctl image digest "$AMD64_IMAGE")"
RESOLVED_ARM64="$(regctl image digest "$ARM64_IMAGE")"
if [ "$RESOLVED_AMD64" != "$AMD64_DIGEST" ]; then
echo "amd64 tag resolved to $RESOLVED_AMD64, expected $AMD64_DIGEST" >&2
exit 1
fi
if [ "$RESOLVED_ARM64" != "$ARM64_DIGEST" ]; then
echo "arm64 tag resolved to $RESOLVED_ARM64, expected $ARM64_DIGEST" >&2
exit 1
fi
regctl index create "$FINAL_IMAGE" \
--ref "$AMD64_IMAGE@$AMD64_DIGEST" \
--desc-platform linux/amd64
regctl index add "$FINAL_IMAGE" \
--ref "$ARM64_IMAGE@$ARM64_DIGEST" \
--desc-platform linux/arm64
PLATFORMS="$(regctl manifest get "$FINAL_IMAGE" \
--format '{{range .Manifests}}{{printf "%s/%s\n" .Platform.OS .Platform.Architecture}}{{end}}' \
| sort)"
EXPECTED_PLATFORMS="$(printf 'linux/amd64\nlinux/arm64')"
if [ "$PLATFORMS" != "$EXPECTED_PLATFORMS" ]; then
echo "Unexpected image index platforms:" >&2
printf '%s\n' "$PLATFORMS" >&2
exit 1
fi
INDEX_DIGEST="$(regctl image digest "$FINAL_IMAGE")"
if ! printf '%s\n' "$INDEX_DIGEST" | grep -Eq '^sha256:[0-9a-f]{64}$'; then
echo "Invalid image index digest: $INDEX_DIGEST" >&2
exit 1
fi
printf '%s' "$FINAL_IMAGE" >"$(results.image-url.path)"
printf '%s' "$INDEX_DIGEST" >"$(results.image-digest.path)"
---
apiVersion: tekton.dev/v1
kind: Task
metadata:
name: lolympus-assert-latest
spec:
description: Stop superseded main-branch runs before external deployment.
params:
- name: revision
type: string
workspaces:
- name: credentials
readOnly: true
steps:
- name: assert
image: docker.io/alpine/git:2.49.1@sha256:c0280cf9572316299b08544065d3bf35db65043d5e3963982ec50647d2746e26
computeResources:
requests: {cpu: 50m, memory: 64Mi}
limits: {cpu: 500m, memory: 256Mi}
env:
- name: REPO_URL
value: https://git.dahyeon.us/dahyeon/lolympus-tournament-support.git
script: |
#!/bin/sh
set -eu
AUTH_HOME="$(mktemp -d)"
trap 'rm -rf "$AUTH_HOME"' EXIT
USERNAME="$(cat "$(workspaces.credentials.path)/username")"
PASSWORD="$(cat "$(workspaces.credentials.path)/password")"
cat >"$AUTH_HOME/.netrc" <<EOF
machine git.dahyeon.us
login $USERNAME
password $PASSWORD
EOF
chmod 0600 "$AUTH_HOME/.netrc"
export HOME="$AUTH_HOME"
REMOTE_REVISION="$(git ls-remote "$REPO_URL" refs/heads/main | awk 'NR == 1 {print $1}')"
if [ "$REMOTE_REVISION" != "$(params.revision)" ]; then
echo "Revision $(params.revision) was superseded by $REMOTE_REVISION" >&2
exit 1
fi
---
apiVersion: tekton.dev/v1
kind: Task
metadata:
name: lolympus-deploy-cloudflare
spec:
description: Build and deploy the staging frontend to Cloudflare Pages.
workspaces:
- name: source
steps:
- name: deploy
image: docker.io/library/node:26-bookworm-slim@sha256:2d49d876e96237d76de412761cf05dbfe5aee325cc4406a4d41d5824c5bb8beb
workingDir: $(workspaces.source.path)
computeResources:
requests: {cpu: 200m, memory: 512Mi}
limits: {cpu: "2", memory: 4Gi}
env:
- name: CI
value: "true"
- name: NPM_CONFIG_CACHE
value: $(workspaces.source.path)/.ci/npm-cache
- name: PATH
value: $(workspaces.source.path)/.ci/pnpm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
- name: CF_PAGES_PROJECT_NAME
value: lolympus
- name: CF_PAGES_STAGING_BRANCH
value: staging
envFrom:
- secretRef:
name: lolympus-cloudflare
script: |
#!/bin/sh
set -eu
if ! command -v pnpm >/dev/null 2>&1; then
mkdir -p .ci/pnpm .ci/npm-cache
npm install --global \
--prefix "$(workspaces.source.path)/.ci/pnpm" pnpm@10.4.1
fi
pnpm deploy:web:staging
---
apiVersion: tekton.dev/v1
kind: Task
metadata:
name: lolympus-update-gitops
spec:
description: Update the staging image index digest in service-state.
params:
- name: revision
type: string
- name: image-digest
type: string
results:
- name: gitops-commit
description: Commit pushed to service-state.
workspaces:
- name: source
- name: credentials
readOnly: true
stepTemplate:
securityContext:
runAsNonRoot: true
runAsUser: 1000
runAsGroup: 1000
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
env:
- name: STATE_REPO_URL
value: https://git.dahyeon.us/dahyeon/service-state.git
- name: SOURCE_REPO_URL
value: https://git.dahyeon.us/dahyeon/lolympus-tournament-support.git
steps:
- name: clone-state
image: docker.io/alpine/git:2.49.1@sha256:c0280cf9572316299b08544065d3bf35db65043d5e3963982ec50647d2746e26
computeResources:
requests: {cpu: 50m, memory: 64Mi}
limits: {cpu: 500m, memory: 512Mi}
script: |
#!/bin/sh
set -eu
SOURCE_DIR="$(workspaces.source.path)"
STATE_DIR="$SOURCE_DIR/.gitops"
AUTH_HOME="$(mktemp -d)"
trap 'rm -rf "$AUTH_HOME"' EXIT
USERNAME="$(cat "$(workspaces.credentials.path)/username")"
PASSWORD="$(cat "$(workspaces.credentials.path)/password")"
cat >"$AUTH_HOME/.netrc" <<EOF
machine git.dahyeon.us
login $USERNAME
password $PASSWORD
EOF
chmod 0600 "$AUTH_HOME/.netrc"
export HOME="$AUTH_HOME"
rm -rf "$STATE_DIR"
git clone --quiet --depth=1 --branch main "$STATE_REPO_URL" "$STATE_DIR"
- name: update-manifest
image: docker.io/mikefarah/yq:4.47.2@sha256:76def1f56f456ecc1c3173ea275218ee17139bc2018c5a07887b15afd88ec03e
computeResources:
requests: {cpu: 50m, memory: 64Mi}
limits: {cpu: 250m, memory: 256Mi}
env:
- name: IMAGE_NAME
value: registry.dahyeon.us/lolympus/lolympus-tournament-support
- name: IMAGE_DIGEST
value: $(params.image-digest)
script: |
#!/bin/sh
set -eu
if ! printf '%s\n' "$IMAGE_DIGEST" | grep -Eq '^sha256:[0-9a-f]{64}$'; then
echo "Invalid image index digest: $IMAGE_DIGEST" >&2
exit 1
fi
MANIFEST="$(workspaces.source.path)/.gitops/apps/lolympus-tournament-support/overlays/staging/kustomization.yaml"
MATCHES="$(yq '[.images[] | select(.name == strenv(IMAGE_NAME))] | length' "$MANIFEST")"
if [ "$MATCHES" != "1" ]; then
echo "Expected one image entry for $IMAGE_NAME, found $MATCHES" >&2
exit 1
fi
yq -i \
'(.images[] | select(.name == strenv(IMAGE_NAME))) |= (del(.newTag) | .digest = strenv(IMAGE_DIGEST))' \
"$MANIFEST"
- name: commit-and-push
image: docker.io/alpine/git:2.49.1@sha256:c0280cf9572316299b08544065d3bf35db65043d5e3963982ec50647d2746e26
computeResources:
requests: {cpu: 50m, memory: 64Mi}
limits: {cpu: 500m, memory: 512Mi}
script: |
#!/bin/sh
set -eu
STATE_DIR="$(workspaces.source.path)/.gitops"
AUTH_HOME="$(mktemp -d)"
trap 'rm -rf "$AUTH_HOME"' EXIT
USERNAME="$(cat "$(workspaces.credentials.path)/username")"
PASSWORD="$(cat "$(workspaces.credentials.path)/password")"
cat >"$AUTH_HOME/.netrc" <<EOF
machine git.dahyeon.us
login $USERNAME
password $PASSWORD
EOF
chmod 0600 "$AUTH_HOME/.netrc"
export HOME="$AUTH_HOME"
git -C "$STATE_DIR" config user.name "lolympus-tekton"
git -C "$STATE_DIR" config user.email "tekton@dahyeon.us"
if git -C "$STATE_DIR" diff --quiet; then
git -C "$STATE_DIR" rev-parse HEAD >"$(results.gitops-commit.path)"
exit 0
fi
REMOTE_REVISION="$(git ls-remote "$SOURCE_REPO_URL" refs/heads/main | awk 'NR == 1 {print $1}')"
if [ "$REMOTE_REVISION" != "$(params.revision)" ]; then
echo "Revision $(params.revision) was superseded by $REMOTE_REVISION" >&2
exit 1
fi
git -C "$STATE_DIR" add apps/lolympus-tournament-support/overlays/staging/kustomization.yaml
git -C "$STATE_DIR" commit \
-m "chore(lolympus): deploy staging $(params.image-digest)"
git -C "$STATE_DIR" pull --rebase origin main
git -C "$STATE_DIR" push origin HEAD:main
git -C "$STATE_DIR" rev-parse HEAD >"$(results.gitops-commit.path)"