From a02e372e855783f8d6cfa83bf16272f3174df260 Mon Sep 17 00:00:00 2001 From: Park Dahyeon Date: Wed, 4 Nov 2026 14:19:16 +0900 Subject: [PATCH] feat(url-shortener): deploy staging and prod branches automatically --- apps/url-shortener/README.md | 4 +- ci/url-shortener/pipeline.yaml | 85 +++++++++++++++++++++++++++++---- ci/url-shortener/start-prod.ps1 | 57 ---------------------- ci/url-shortener/tasks.yaml | 59 ++++++++--------------- ci/url-shortener/trigger.yaml | 15 ++++-- 5 files changed, 110 insertions(+), 110 deletions(-) delete mode 100644 ci/url-shortener/start-prod.ps1 diff --git a/apps/url-shortener/README.md b/apps/url-shortener/README.md index 91f12d9..df0916e 100644 --- a/apps/url-shortener/README.md +++ b/apps/url-shortener/README.md @@ -5,5 +5,5 @@ - 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합니다. +앱 저장소의 `staging`/`prod` 브랜치 push → 해당 Tekton Pipeline → 환경별 GitOps 갱신 → Argo CD 자동 Sync. +이 저장소는 `main`의 환경별 overlay를 유지하며, 앱의 `main` push는 배포하지 않습니다. diff --git a/ci/url-shortener/pipeline.yaml b/ci/url-shortener/pipeline.yaml index f523822..ef97533 100644 --- a/ci/url-shortener/pipeline.yaml +++ b/ci/url-shortener/pipeline.yaml @@ -3,7 +3,7 @@ kind: Pipeline metadata: name: url-shortener-staging spec: - description: Validate an exact main revision, publish a multiarch Rust API image, deploy the staging Redirect Worker and static admin Pages, then update staging GitOps with matching D1 identifiers. + description: Validate an exact staging branch revision, publish a multiarch Rust API image, deploy staging Cloudflare resources, then update staging GitOps. params: - name: git-revision type: string @@ -23,6 +23,8 @@ spec: params: - name: revision value: $(params.git-revision) + - name: environment + value: staging workspaces: - name: source workspace: source @@ -98,6 +100,8 @@ spec: params: - name: revision value: $(params.git-revision) + - name: environment + value: staging workspaces: - name: source workspace: source @@ -111,6 +115,8 @@ spec: params: - name: revision value: $(params.git-revision) + - name: environment + value: staging - name: image-digest value: $(tasks.create-image-index.results.image-digest) workspaces: @@ -124,20 +130,19 @@ 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. + description: Validate an exact prod branch revision, publish a multiarch Rust API image, deploy production Cloudflare resources, then update 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: $(tasks.create-image-index.results.image-digest) - name: gitops-commit value: $(tasks.update-gitops.results.gitops-commit) workspaces: - name: source - name: gitea-credentials + - name: harbor-dockerconfig tasks: - name: clone taskRef: @@ -145,14 +150,78 @@ spec: params: - name: revision value: $(params.git-revision) + - name: environment + value: prod workspaces: - name: source workspace: source - name: credentials workspace: gitea-credentials - - name: deploy-cloudflare + - name: quality runAfter: - clone + taskRef: + name: url-shortener-quality + workspaces: + - name: source + workspace: source + - name: build-amd64 + runAfter: + - quality + taskRef: + name: url-shortener-build-platform + params: + - name: revision + value: $(params.git-revision) + - name: platform + value: linux/amd64 + - name: image + value: registry.dahyeon.us/dahyeon/url-shortener:sha-$(params.git-revision)-$(context.pipelineRun.uid)-amd64 + workspaces: + - name: credentials + workspace: gitea-credentials + - name: dockerconfig + workspace: harbor-dockerconfig + - name: build-arm64 + runAfter: + - quality + taskRef: + name: url-shortener-build-platform + params: + - name: revision + value: $(params.git-revision) + - name: platform + value: linux/arm64 + - name: image + value: registry.dahyeon.us/dahyeon/url-shortener:sha-$(params.git-revision)-$(context.pipelineRun.uid)-arm64 + workspaces: + - name: credentials + workspace: gitea-credentials + - name: dockerconfig + workspace: harbor-dockerconfig + - name: create-image-index + runAfter: + - build-amd64 + - build-arm64 + taskRef: + name: url-shortener-create-image-index + params: + - name: image + value: registry.dahyeon.us/dahyeon/url-shortener:sha-$(params.git-revision)-$(context.pipelineRun.uid) + - name: amd64-image + value: $(tasks.build-amd64.results.image-url) + - name: amd64-digest + value: $(tasks.build-amd64.results.image-digest) + - name: arm64-image + value: $(tasks.build-arm64.results.image-url) + - name: arm64-digest + value: $(tasks.build-arm64.results.image-digest) + workspaces: + - name: dockerconfig + workspace: harbor-dockerconfig + - name: deploy-cloudflare + runAfter: + - create-image-index taskRef: name: url-shortener-deploy-cloudflare params: @@ -176,7 +245,7 @@ spec: - name: environment value: prod - name: image-digest - value: $(params.image-digest) + value: $(tasks.create-image-index.results.image-digest) workspaces: - name: source workspace: source diff --git a/ci/url-shortener/start-prod.ps1 b/ci/url-shortener/start-prod.ps1 deleted file mode 100644 index e15372d..0000000 --- a/ci/url-shortener/start-prod.ps1 +++ /dev/null @@ -1,57 +0,0 @@ -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 cac7c29..40800d2 100644 --- a/ci/url-shortener/tasks.yaml +++ b/ci/url-shortener/tasks.yaml @@ -3,10 +3,13 @@ kind: Task metadata: name: url-shortener-clone spec: - description: Clone and verify an exact non-deletion source revision from Gitea. + description: Clone the pushed source revision and verify the selected deployment branch. params: - name: revision type: string + - name: environment + type: string + default: staging workspaces: - name: source - name: credentials @@ -26,6 +29,8 @@ spec: value: https://git.dahyeon.us/dahyeon/url-shortener.git - name: REVISION value: $(params.revision) + - name: DEPLOY_ENV + value: $(params.environment) - name: SOURCE_DIR value: $(workspaces.source.path) - name: GITEA_CREDENTIALS_DIR @@ -34,6 +39,7 @@ spec: #!/bin/sh set -eu umask 0002 + case "$DEPLOY_ENV" in staging|prod) ;; *) echo 'Invalid deployment environment' >&2; exit 1;; esac printf '%s\n' "$REVISION" | grep -Eq '^[0-9a-f]{40}$' test "$REVISION" != '0000000000000000000000000000000000000000' case "$SOURCE_DIR" in /workspace/*) ;; *) echo 'Unexpected workspace path' >&2; exit 1;; esac @@ -49,6 +55,11 @@ spec: EOF chmod 0700 "$AUTH_DIR/askpass" export GIT_ASKPASS="$AUTH_DIR/askpass" GIT_TERMINAL_PROMPT=0 + REMOTE_REVISION="$(git ls-remote "$REPO_URL" "refs/heads/$DEPLOY_ENV" | awk 'NR == 1 {print $1}')" + if [ "$REMOTE_REVISION" != "$REVISION" ]; then + echo 'Source revision was superseded before checkout.' >&2 + exit 1 + fi 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" @@ -408,32 +419,6 @@ spec: - 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) @@ -486,12 +471,10 @@ spec: chmod 0700 "$AUTH_DIR/askpass" export GIT_ASKPASS="$AUTH_DIR/askpass" GIT_TERMINAL_PROMPT=0 test "$(git rev-parse HEAD)" = "$REVISION" - 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 + REMOTE_REVISION="$(git ls-remote "$REPO_URL" "refs/heads/$DEPLOY_ENV" | awk 'NR == 1 {print $1}')" + if [ "$REMOTE_REVISION" != "$REVISION" ]; then + echo 'Source revision was superseded before Cloudflare deployment.' >&2 + exit 1 fi command -v pnpm >/dev/null pnpm deploy:cloudflare -- "$DEPLOY_ENV" @@ -656,12 +639,10 @@ spec: fi 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 + REMOTE_REVISION="$(git ls-remote "$SOURCE_REPO_URL" "refs/heads/$DEPLOY_ENV" | awk 'NR == 1 {print $1}')" + if [ "$REMOTE_REVISION" != "$REVISION" ]; then + echo 'Source revision was superseded before GitOps push.' >&2 + exit 1 fi # A concurrent state update causes a non-fast-forward failure, never an overwrite. git -C "$STATE_DIR" push origin HEAD:main diff --git a/ci/url-shortener/trigger.yaml b/ci/url-shortener/trigger.yaml index cc2f4e9..0284a4a 100644 --- a/ci/url-shortener/trigger.yaml +++ b/ci/url-shortener/trigger.yaml @@ -6,6 +6,8 @@ spec: params: - name: git-revision value: $(body.after) + - name: environment + value: $(extensions.environment) --- apiVersion: triggers.tekton.dev/v1beta1 kind: TriggerTemplate @@ -14,17 +16,18 @@ metadata: spec: params: - name: git-revision + - name: environment resourcetemplates: - apiVersion: tekton.dev/v1 kind: PipelineRun metadata: - generateName: url-shortener-staging- + generateName: url-shortener-$(tt.params.environment)- labels: app.kubernetes.io/name: url-shortener app.kubernetes.io/part-of: url-shortener-ci spec: pipelineRef: - name: url-shortener-staging + name: url-shortener-$(tt.params.environment) params: - name: git-revision value: $(tt.params.git-revision) @@ -79,7 +82,7 @@ metadata: spec: serviceAccountName: url-shortener-event-listener triggers: - - name: main-push + - name: branch-push interceptors: - ref: name: github @@ -95,7 +98,11 @@ spec: name: cel params: - name: filter - value: body.repository.full_name == 'dahyeon/url-shortener' && body.ref == 'refs/heads/main' && body.after.matches('^[0-9a-f]{40}$') && body.after != '0000000000000000000000000000000000000000' + value: body.repository.full_name == 'dahyeon/url-shortener' && body.ref in ['refs/heads/staging', 'refs/heads/prod'] && body.after.matches('^[0-9a-f]{40}$') && body.after != '0000000000000000000000000000000000000000' + - name: overlays + value: + - key: environment + expression: "body.ref.split('/')[2]" bindings: - ref: url-shortener-main template: