diff --git a/apps/url-shortener/README.md b/apps/url-shortener/README.md index 48b2a5e..91f12d9 100644 --- a/apps/url-shortener/README.md +++ b/apps/url-shortener/README.md @@ -4,3 +4,6 @@ - 이미지: `registry.dahyeon.us/dahyeon/url-shortener` - Harbor pull Secret: `harbor-registry` - 앱 Secret: `url-shortener-secrets` (Infisical) + +운영 승격: `./ci/url-shortener/start-prod.ps1 -StagingRun <성공한 staging PipelineRun>` (저장소 루트에서 실행). +`url-shortener-prod` 완료 후 Argo CD의 `url-shortener-prod`를 수동 Sync합니다. diff --git a/ci/url-shortener/infisical-secret.yaml b/ci/url-shortener/infisical-secret.yaml index 656ff3a..6b36cb3 100644 --- a/ci/url-shortener/infisical-secret.yaml +++ b/ci/url-shortener/infisical-secret.yaml @@ -80,3 +80,30 @@ spec: engineVersion: v1 data: INGEST_HMAC_SECRET: "{{ .INGEST_HMAC_SECRET.Value }}" +--- +apiVersion: secrets.infisical.com/v1beta1 +kind: InfisicalStaticSecret +metadata: + name: url-shortener-ci-edge-prod +spec: + infisicalAuthRef: + name: infisical-auth + namespace: infisical + syncOptions: + refreshInterval: 60s + instantUpdates: false + sources: + - projectId: 01c4ebe5-8c73-475c-ba29-56709483b0f1 + environmentSlug: prod + secretPath: / + recursive: false + targets: + - name: url-shortener-edge-prod + namespace: tekton-ci + kind: Secret + creationPolicy: Owner + secretType: Opaque + template: + engineVersion: v1 + data: + INGEST_HMAC_SECRET: "{{ .INGEST_HMAC_SECRET.Value }}" diff --git a/ci/url-shortener/pipeline.yaml b/ci/url-shortener/pipeline.yaml index f313fec..9c06aaa 100644 --- a/ci/url-shortener/pipeline.yaml +++ b/ci/url-shortener/pipeline.yaml @@ -118,3 +118,69 @@ spec: workspace: source - name: credentials workspace: gitea-credentials +--- +apiVersion: tekton.dev/v1 +kind: Pipeline +metadata: + name: url-shortener-prod +spec: + description: Manually promote a matching source revision and immutable image digest from a successful staging run, deploying production Cloudflare resources and updating production GitOps. + params: + - name: git-revision + type: string + description: Full source commit SHA from the successful staging run. + - name: image-digest + type: string + description: Matching image-digest result from the same successful staging run. + results: + - name: image-digest + value: $(params.image-digest) + - name: gitops-commit + value: $(tasks.update-gitops.results.gitops-commit) + workspaces: + - name: source + - name: gitea-credentials + tasks: + - name: clone + taskRef: + name: url-shortener-clone + params: + - name: revision + value: $(params.git-revision) + workspaces: + - name: source + workspace: source + - name: credentials + workspace: gitea-credentials + - name: deploy-cloudflare + runAfter: + - clone + taskRef: + name: url-shortener-deploy-cloudflare + params: + - name: revision + value: $(params.git-revision) + - name: environment + value: prod + workspaces: + - name: source + workspace: source + - name: credentials + workspace: gitea-credentials + - name: update-gitops + runAfter: + - deploy-cloudflare + taskRef: + name: url-shortener-update-gitops + params: + - name: revision + value: $(params.git-revision) + - name: environment + value: prod + - name: image-digest + value: $(params.image-digest) + workspaces: + - name: source + workspace: source + - name: credentials + workspace: gitea-credentials diff --git a/ci/url-shortener/start-prod.ps1 b/ci/url-shortener/start-prod.ps1 new file mode 100644 index 0000000..e15372d --- /dev/null +++ b/ci/url-shortener/start-prod.ps1 @@ -0,0 +1,57 @@ +param( + [Parameter(Mandatory = $true)] + [string]$StagingRun +) + +$ErrorActionPreference = 'Stop' +$runJson = kubectl -n tekton-ci get pipelinerun $StagingRun -o json +if ($LASTEXITCODE -ne 0) { throw 'Could not read the staging PipelineRun.' } +$run = $runJson | ConvertFrom-Json +if ($run.spec.pipelineRef.name -ne 'url-shortener-staging' -or + -not ($run.status.conditions | Where-Object { $_.type -eq 'Succeeded' -and $_.status -eq 'True' })) { + throw 'Select a successful url-shortener-staging PipelineRun.' +} +$revision = ($run.spec.params | Where-Object name -eq 'git-revision').value +$digest = ($run.status.results | Where-Object name -eq 'image-digest').value +if ($revision -notmatch '^[0-9a-f]{40}$' -or $digest -notmatch '^sha256:[0-9a-f]{64}$') { + throw 'The staging run has no valid source revision or image digest.' +} + +$pipelineRun = @{ + apiVersion = 'tekton.dev/v1' + kind = 'PipelineRun' + metadata = @{ + generateName = 'url-shortener-prod-' + namespace = 'tekton-ci' + labels = @{ 'app.kubernetes.io/name' = 'url-shortener'; 'app.kubernetes.io/part-of' = 'url-shortener-ci' } + annotations = @{ 'url-shortener.dahyeon.us/staging-run' = $StagingRun } + } + spec = @{ + pipelineRef = @{ name = 'url-shortener-prod' } + params = @( + @{ name = 'git-revision'; value = $revision } + @{ name = 'image-digest'; value = $digest } + ) + taskRunTemplate = @{ + serviceAccountName = 'url-shortener-pipeline' + podTemplate = @{ + nodeSelector = @{ 'kubernetes.io/arch' = 'amd64' } + securityContext = @{ fsGroup = 1000; fsGroupChangePolicy = 'OnRootMismatch' } + } + } + workspaces = @( + @{ + name = 'source' + volumeClaimTemplate = @{ spec = @{ + accessModes = @('ReadWriteOnce') + storageClassName = 'local-path' + resources = @{ requests = @{ storage = '5Gi' } } + } } + } + @{ name = 'gitea-credentials'; secret = @{ secretName = 'url-shortener-gitea' } } + ) + timeouts = @{ pipeline = '30m'; tasks = '25m' } + } +} +$pipelineRun | ConvertTo-Json -Depth 12 | kubectl create -f - +if ($LASTEXITCODE -ne 0) { throw 'Could not create the production PipelineRun.' } diff --git a/ci/url-shortener/tasks.yaml b/ci/url-shortener/tasks.yaml index cf5a310..cac7c29 100644 --- a/ci/url-shortener/tasks.yaml +++ b/ci/url-shortener/tasks.yaml @@ -396,15 +396,44 @@ kind: Task metadata: name: url-shortener-deploy-cloudflare spec: - description: Recheck main, migrate D1, deploy the staging Redirect Worker, then build and deploy static admin Pages from the same revision. + description: Deploy D1, the Redirect Worker and admin Pages from the selected revision and environment. params: - name: revision type: string + - name: environment + type: string + default: staging workspaces: - name: source - name: credentials readOnly: true steps: + - name: prepare-dependencies + image: docker.io/library/node:26.13.0-bookworm@sha256:fa52d1669505e29b58c042875b1d65c229e0787a3610fd52869b22b68e480853 + 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)/.cache/ci/npm + script: | + #!/bin/sh + set -eu + # Staging's quality task already installed these dependencies. + if [ -x .cache/ci/pnpm/bin/pnpm ] && [ -d node_modules ]; then + exit 0 + fi + umask 0002 + mkdir -p .cache/ci/pnpm .cache/ci/npm + npm install --global --prefix "$PWD/.cache/ci/pnpm" pnpm@12.16.1 + .cache/ci/pnpm/bin/pnpm install --frozen-lockfile - name: deploy image: docker.io/library/node:26.13.0-bookworm@sha256:fa52d1669505e29b58c042875b1d65c229e0787a3610fd52869b22b68e480853 workingDir: $(workspaces.source.path) @@ -422,6 +451,8 @@ spec: value: https://git.dahyeon.us/dahyeon/url-shortener.git - name: REVISION value: $(params.revision) + - name: DEPLOY_ENV + value: $(params.environment) - name: GITEA_CREDENTIALS_DIR value: $(workspaces.credentials.path) - name: PATH @@ -436,10 +467,11 @@ spec: - secretRef: name: url-shortener-cloudflare - secretRef: - name: url-shortener-edge-staging + name: url-shortener-edge-$(params.environment) script: | #!/bin/sh set -eu + case "$DEPLOY_ENV" in staging|prod) ;; *) echo 'Invalid deployment environment' >&2; exit 1;; esac printf '%s\n' "$REVISION" | grep -Eq '^[0-9a-f]{40}$' AUTH_DIR="$(mktemp -d)" trap 'rm -rf "$AUTH_DIR"' EXIT @@ -454,26 +486,31 @@ spec: chmod 0700 "$AUTH_DIR/askpass" export GIT_ASKPASS="$AUTH_DIR/askpass" GIT_TERMINAL_PROMPT=0 test "$(git rev-parse HEAD)" = "$REVISION" - REMOTE_REVISION="$(git ls-remote "$REPO_URL" refs/heads/main | awk 'NR == 1 {print $1}')" - if [ "$REMOTE_REVISION" != "$REVISION" ]; then - echo 'Source revision was superseded before Cloudflare deployment.' >&2 - exit 1 + if [ "$DEPLOY_ENV" = staging ]; then + REMOTE_REVISION="$(git ls-remote "$REPO_URL" refs/heads/main | awk 'NR == 1 {print $1}')" + if [ "$REMOTE_REVISION" != "$REVISION" ]; then + echo 'Source revision was superseded before Cloudflare deployment.' >&2 + exit 1 + fi fi command -v pnpm >/dev/null - pnpm deploy:cloudflare -- staging - pnpm deploy:pages -- staging + pnpm deploy:cloudflare -- "$DEPLOY_ENV" + pnpm deploy:pages -- "$DEPLOY_ENV" --- apiVersion: tekton.dev/v1 kind: Task metadata: name: url-shortener-update-gitops spec: - description: Publish the staging app digest and matching D1 identifiers, bootstrapping at least two replicas without reducing a larger configured count. + description: Publish the selected environment's app digest and D1 identifiers, bootstrapping at least two replicas without reducing a larger configured count. params: - name: revision type: string - name: image-digest type: string + - name: environment + type: string + default: staging results: - name: gitops-commit description: Commit pushed to service-state main. @@ -497,6 +534,8 @@ spec: value: https://git.dahyeon.us/dahyeon/url-shortener.git - name: REVISION value: $(params.revision) + - name: DEPLOY_ENV + value: $(params.environment) - name: STATE_DIR value: $(workspaces.source.path)/.cache/gitops-state - name: GITEA_CREDENTIALS_DIR @@ -547,7 +586,7 @@ spec: - name: DEPLOYMENT_NAME value: url-shortener - name: CLOUDFLARE_PROFILE - value: $(workspaces.source.path)/deploy/cloudflare/staging.json + value: $(workspaces.source.path)/deploy/cloudflare/$(params.environment).json - name: CLOUDFLARE_ACCOUNT_ID valueFrom: secretKeyRef: @@ -556,15 +595,16 @@ spec: script: | #!/bin/sh set -eu + case "$DEPLOY_ENV" in staging|prod) ;; *) echo 'Invalid deployment environment' >&2; exit 1;; esac printf '%s\n' "$IMAGE_DIGEST" | grep -Eq '^sha256:[0-9a-f]{64}$' printf '%s\n' "$CLOUDFLARE_ACCOUNT_ID" | grep -Eq '^[0-9a-fA-F]{32}$' - test "$(yq -r '.environment' "$CLOUDFLARE_PROFILE")" = staging + test "$(yq -r '.environment' "$CLOUDFLARE_PROFILE")" = "$DEPLOY_ENV" CLOUDFLARE_D1_DATABASE_ID="$(yq -r '.databaseId' "$CLOUDFLARE_PROFILE")" printf '%s\n' "$CLOUDFLARE_D1_DATABASE_ID" | grep -Eq '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$' test "$CLOUDFLARE_D1_DATABASE_ID" != '00000000-0000-0000-0000-000000000000' export CLOUDFLARE_D1_DATABASE_ID - MANIFEST="$STATE_DIR/apps/url-shortener/overlays/staging/kustomization.yaml" - CONFIGMAP="$STATE_DIR/apps/url-shortener/overlays/staging/configmap.yaml" + MANIFEST="$STATE_DIR/apps/url-shortener/overlays/$DEPLOY_ENV/kustomization.yaml" + CONFIGMAP="$STATE_DIR/apps/url-shortener/overlays/$DEPLOY_ENV/configmap.yaml" test "$(yq '[.images[] | select(.name == strenv(APP_IMAGE))] | length' "$MANIFEST")" = 1 test "$(yq '[.replicas[] | select(.name == strenv(DEPLOYMENT_NAME))] | length' "$MANIFEST")" = 1 REPLICAS="$(yq '.replicas[] | select(.name == strenv(DEPLOYMENT_NAME)) | .count' "$MANIFEST")" @@ -614,12 +654,14 @@ spec: git -C "$STATE_DIR" rev-parse HEAD >"$GITOPS_RESULT_PATH" exit 0 fi - git -C "$STATE_DIR" add apps/url-shortener/overlays/staging/kustomization.yaml apps/url-shortener/overlays/staging/configmap.yaml - git -C "$STATE_DIR" commit -m "chore(url-shortener): deploy staging $REVISION" - REMOTE_REVISION="$(git ls-remote "$SOURCE_REPO_URL" refs/heads/main | awk 'NR == 1 {print $1}')" - if [ "$REMOTE_REVISION" != "$REVISION" ]; then - echo 'Source revision was superseded before GitOps push.' >&2 - exit 1 + git -C "$STATE_DIR" add "apps/url-shortener/overlays/$DEPLOY_ENV/kustomization.yaml" "apps/url-shortener/overlays/$DEPLOY_ENV/configmap.yaml" + git -C "$STATE_DIR" commit -m "chore(url-shortener): deploy $DEPLOY_ENV $REVISION" + if [ "$DEPLOY_ENV" = staging ]; then + REMOTE_REVISION="$(git ls-remote "$SOURCE_REPO_URL" refs/heads/main | awk 'NR == 1 {print $1}')" + if [ "$REMOTE_REVISION" != "$REVISION" ]; then + echo 'Source revision was superseded before GitOps push.' >&2 + exit 1 + fi fi # A concurrent state update causes a non-fast-forward failure, never an overwrite. git -C "$STATE_DIR" push origin HEAD:main