diff --git a/ci/lolympus-tournament-support/kustomization.yaml b/ci/lolympus-tournament-support/kustomization.yaml index 4a15000..fd4c078 100644 --- a/ci/lolympus-tournament-support/kustomization.yaml +++ b/ci/lolympus-tournament-support/kustomization.yaml @@ -6,6 +6,7 @@ namespace: tekton-ci resources: - foundation.yaml - infisical-secret.yaml + - tasks.yaml labels: - includeSelectors: false diff --git a/ci/lolympus-tournament-support/tasks.yaml b/ci/lolympus-tournament-support/tasks.yaml new file mode 100644 index 0000000..ae7d842 --- /dev/null +++ b/ci/lolympus-tournament-support/tasks.yaml @@ -0,0 +1,359 @@ +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" <&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: COREPACK_HOME + value: $(workspaces.source.path)/.ci/corepack + steps: + - name: install + computeResources: + requests: {cpu: 200m, memory: 512Mi} + limits: {cpu: "2", memory: 4Gi} + script: | + #!/bin/sh + set -eu + corepack pnpm install --frozen-lockfile + - name: lint + computeResources: + requests: {cpu: 100m, memory: 256Mi} + limits: {cpu: "2", memory: 2Gi} + script: | + #!/bin/sh + set -eu + corepack pnpm --filter api exec eslint \ + "{src,apps,libs,test}/**/*.ts" --max-warnings=0 + corepack pnpm --filter web lint + - name: test + computeResources: + requests: {cpu: 100m, memory: 512Mi} + limits: {cpu: "3", memory: 4Gi} + script: | + #!/bin/sh + set -eu + corepack pnpm --filter api test -- --runInBand + corepack pnpm --filter web test + corepack pnpm test:deploy:web + - name: build + computeResources: + requests: {cpu: 200m, memory: 512Mi} + limits: {cpu: "3", memory: 4Gi} + script: | + #!/bin/sh + set -eu + corepack pnpm --filter api build + corepack pnpm --filter web build:staging + - name: audit + computeResources: + requests: {cpu: 50m, memory: 128Mi} + limits: {cpu: "1", memory: 1Gi} + script: | + #!/bin/sh + set -eu + corepack pnpm audit --prod --audit-level high +--- +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: lolympus-build-image +spec: + description: Build and push the API image with rootless BuildKit. + params: + - 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: source + - name: dockerconfig + readOnly: true + steps: + - name: build-and-push + image: docker.io/moby/buildkit:v0.31.1-rootless@sha256:946cf909534b789c9b84af75e6d4f8cb23c3b9cb387daec5ba0f4878a3e4647c + workingDir: $(workspaces.source.path) + 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 + procMount: Unmasked + env: + - name: BUILDKITD_FLAGS + value: --oci-worker-no-process-sandbox + - name: DOCKER_CONFIG + value: $(workspaces.dockerconfig.path) + script: | + #!/bin/sh + set -eu + mkdir -p .ci + buildctl-daemonless.sh build \ + --frontend dockerfile.v0 \ + --local context=. \ + --local dockerfile=. \ + --opt filename=Dockerfile \ + --output "type=image,name=$(params.image),push=true" \ + --metadata-file .ci/buildkit-metadata.json + - name: record-digest + image: docker.io/mikefarah/yq:4.47.2@sha256:76def1f56f456ecc1c3173ea275218ee17139bc2018c5a07887b15afd88ec03e + workingDir: $(workspaces.source.path) + computeResources: + requests: {cpu: 50m, memory: 64Mi} + limits: {cpu: 250m, memory: 256Mi} + script: | + #!/bin/sh + set -eu + DIGEST="$$(yq -r '."containerimage.digest"' .ci/buildkit-metadata.json)" + 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-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" <&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: COREPACK_HOME + value: $(workspaces.source.path)/.ci/corepack + - name: CF_PAGES_PROJECT_NAME + value: lolympus + - name: CF_PAGES_STAGING_BRANCH + value: staging + envFrom: + - secretRef: + name: lolympus-cloudflare + script: | + #!/bin/sh + set -eu + corepack pnpm deploy:web:staging +--- +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: lolympus-update-gitops +spec: + description: Update the staging image tag in service-state. + params: + - name: revision + type: string + - name: image-tag + type: string + results: + - name: gitops-commit + description: Commit pushed to service-state. + workspaces: + - name: source + - name: credentials + readOnly: true + stepTemplate: + 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" <&2 + exit 1 + fi + yq -i \ + '(.images[] | select(.name == strenv(IMAGE_NAME))).newTag = strenv(IMAGE_TAG)' \ + "$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" <"$(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-tag)" + 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)"