feat(url-shortener): add production promotion pipeline
This commit is contained in:
1 parent
aae1888bea
commit
b941de2e0e
5 files changed
+214
-19
No files matched your search
@@ -4,3 +4,6 @@
|
|||||||
- 이미지: `registry.dahyeon.us/dahyeon/url-shortener`
|
- 이미지: `registry.dahyeon.us/dahyeon/url-shortener`
|
||||||
- Harbor pull Secret: `harbor-registry`
|
- Harbor pull Secret: `harbor-registry`
|
||||||
- 앱 Secret: `url-shortener-secrets` (Infisical)
|
- 앱 Secret: `url-shortener-secrets` (Infisical)
|
||||||
|
|
||||||
|
운영 승격: `./ci/url-shortener/start-prod.ps1 -StagingRun <성공한 staging PipelineRun>` (저장소 루트에서 실행).
|
||||||
|
`url-shortener-prod` 완료 후 Argo CD의 `url-shortener-prod`를 수동 Sync합니다.
|
||||||
@@ -80,3 +80,30 @@ spec:
|
|||||||
engineVersion: v1
|
engineVersion: v1
|
||||||
data:
|
data:
|
||||||
INGEST_HMAC_SECRET: "{{ .INGEST_HMAC_SECRET.Value }}"
|
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 }}"
|
||||||
@@ -118,3 +118,69 @@ spec:
|
|||||||
workspace: source
|
workspace: source
|
||||||
- name: credentials
|
- name: credentials
|
||||||
workspace: gitea-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
|
||||||
@@ -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.' }
|
||||||
+61
-19
@@ -396,15 +396,44 @@ kind: Task
|
|||||||
metadata:
|
metadata:
|
||||||
name: url-shortener-deploy-cloudflare
|
name: url-shortener-deploy-cloudflare
|
||||||
spec:
|
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:
|
params:
|
||||||
- name: revision
|
- name: revision
|
||||||
type: string
|
type: string
|
||||||
|
- name: environment
|
||||||
|
type: string
|
||||||
|
default: staging
|
||||||
workspaces:
|
workspaces:
|
||||||
- name: source
|
- name: source
|
||||||
- name: credentials
|
- name: credentials
|
||||||
readOnly: true
|
readOnly: true
|
||||||
steps:
|
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
|
- name: deploy
|
||||||
image: docker.io/library/node:26.13.0-bookworm@sha256:fa52d1669505e29b58c042875b1d65c229e0787a3610fd52869b22b68e480853
|
image: docker.io/library/node:26.13.0-bookworm@sha256:fa52d1669505e29b58c042875b1d65c229e0787a3610fd52869b22b68e480853
|
||||||
workingDir: $(workspaces.source.path)
|
workingDir: $(workspaces.source.path)
|
||||||
@@ -422,6 +451,8 @@ spec:
|
|||||||
value: https://git.dahyeon.us/dahyeon/url-shortener.git
|
value: https://git.dahyeon.us/dahyeon/url-shortener.git
|
||||||
- name: REVISION
|
- name: REVISION
|
||||||
value: $(params.revision)
|
value: $(params.revision)
|
||||||
|
- name: DEPLOY_ENV
|
||||||
|
value: $(params.environment)
|
||||||
- name: GITEA_CREDENTIALS_DIR
|
- name: GITEA_CREDENTIALS_DIR
|
||||||
value: $(workspaces.credentials.path)
|
value: $(workspaces.credentials.path)
|
||||||
- name: PATH
|
- name: PATH
|
||||||
@@ -436,10 +467,11 @@ spec:
|
|||||||
- secretRef:
|
- secretRef:
|
||||||
name: url-shortener-cloudflare
|
name: url-shortener-cloudflare
|
||||||
- secretRef:
|
- secretRef:
|
||||||
name: url-shortener-edge-staging
|
name: url-shortener-edge-$(params.environment)
|
||||||
script: |
|
script: |
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
set -eu
|
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}$'
|
printf '%s\n' "$REVISION" | grep -Eq '^[0-9a-f]{40}$'
|
||||||
AUTH_DIR="$(mktemp -d)"
|
AUTH_DIR="$(mktemp -d)"
|
||||||
trap 'rm -rf "$AUTH_DIR"' EXIT
|
trap 'rm -rf "$AUTH_DIR"' EXIT
|
||||||
@@ -454,26 +486,31 @@ spec:
|
|||||||
chmod 0700 "$AUTH_DIR/askpass"
|
chmod 0700 "$AUTH_DIR/askpass"
|
||||||
export GIT_ASKPASS="$AUTH_DIR/askpass" GIT_TERMINAL_PROMPT=0
|
export GIT_ASKPASS="$AUTH_DIR/askpass" GIT_TERMINAL_PROMPT=0
|
||||||
test "$(git rev-parse HEAD)" = "$REVISION"
|
test "$(git rev-parse HEAD)" = "$REVISION"
|
||||||
REMOTE_REVISION="$(git ls-remote "$REPO_URL" refs/heads/main | awk 'NR == 1 {print $1}')"
|
if [ "$DEPLOY_ENV" = staging ]; then
|
||||||
if [ "$REMOTE_REVISION" != "$REVISION" ]; then
|
REMOTE_REVISION="$(git ls-remote "$REPO_URL" refs/heads/main | awk 'NR == 1 {print $1}')"
|
||||||
echo 'Source revision was superseded before Cloudflare deployment.' >&2
|
if [ "$REMOTE_REVISION" != "$REVISION" ]; then
|
||||||
exit 1
|
echo 'Source revision was superseded before Cloudflare deployment.' >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
command -v pnpm >/dev/null
|
command -v pnpm >/dev/null
|
||||||
pnpm deploy:cloudflare -- staging
|
pnpm deploy:cloudflare -- "$DEPLOY_ENV"
|
||||||
pnpm deploy:pages -- staging
|
pnpm deploy:pages -- "$DEPLOY_ENV"
|
||||||
---
|
---
|
||||||
apiVersion: tekton.dev/v1
|
apiVersion: tekton.dev/v1
|
||||||
kind: Task
|
kind: Task
|
||||||
metadata:
|
metadata:
|
||||||
name: url-shortener-update-gitops
|
name: url-shortener-update-gitops
|
||||||
spec:
|
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:
|
params:
|
||||||
- name: revision
|
- name: revision
|
||||||
type: string
|
type: string
|
||||||
- name: image-digest
|
- name: image-digest
|
||||||
type: string
|
type: string
|
||||||
|
- name: environment
|
||||||
|
type: string
|
||||||
|
default: staging
|
||||||
results:
|
results:
|
||||||
- name: gitops-commit
|
- name: gitops-commit
|
||||||
description: Commit pushed to service-state main.
|
description: Commit pushed to service-state main.
|
||||||
@@ -497,6 +534,8 @@ spec:
|
|||||||
value: https://git.dahyeon.us/dahyeon/url-shortener.git
|
value: https://git.dahyeon.us/dahyeon/url-shortener.git
|
||||||
- name: REVISION
|
- name: REVISION
|
||||||
value: $(params.revision)
|
value: $(params.revision)
|
||||||
|
- name: DEPLOY_ENV
|
||||||
|
value: $(params.environment)
|
||||||
- name: STATE_DIR
|
- name: STATE_DIR
|
||||||
value: $(workspaces.source.path)/.cache/gitops-state
|
value: $(workspaces.source.path)/.cache/gitops-state
|
||||||
- name: GITEA_CREDENTIALS_DIR
|
- name: GITEA_CREDENTIALS_DIR
|
||||||
@@ -547,7 +586,7 @@ spec:
|
|||||||
- name: DEPLOYMENT_NAME
|
- name: DEPLOYMENT_NAME
|
||||||
value: url-shortener
|
value: url-shortener
|
||||||
- name: CLOUDFLARE_PROFILE
|
- name: CLOUDFLARE_PROFILE
|
||||||
value: $(workspaces.source.path)/deploy/cloudflare/staging.json
|
value: $(workspaces.source.path)/deploy/cloudflare/$(params.environment).json
|
||||||
- name: CLOUDFLARE_ACCOUNT_ID
|
- name: CLOUDFLARE_ACCOUNT_ID
|
||||||
valueFrom:
|
valueFrom:
|
||||||
secretKeyRef:
|
secretKeyRef:
|
||||||
@@ -556,15 +595,16 @@ spec:
|
|||||||
script: |
|
script: |
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
set -eu
|
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' "$IMAGE_DIGEST" | grep -Eq '^sha256:[0-9a-f]{64}$'
|
||||||
printf '%s\n' "$CLOUDFLARE_ACCOUNT_ID" | grep -Eq '^[0-9a-fA-F]{32}$'
|
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")"
|
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}$'
|
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'
|
test "$CLOUDFLARE_D1_DATABASE_ID" != '00000000-0000-0000-0000-000000000000'
|
||||||
export CLOUDFLARE_D1_DATABASE_ID
|
export CLOUDFLARE_D1_DATABASE_ID
|
||||||
MANIFEST="$STATE_DIR/apps/url-shortener/overlays/staging/kustomization.yaml"
|
MANIFEST="$STATE_DIR/apps/url-shortener/overlays/$DEPLOY_ENV/kustomization.yaml"
|
||||||
CONFIGMAP="$STATE_DIR/apps/url-shortener/overlays/staging/configmap.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 '[.images[] | select(.name == strenv(APP_IMAGE))] | length' "$MANIFEST")" = 1
|
||||||
test "$(yq '[.replicas[] | select(.name == strenv(DEPLOYMENT_NAME))] | length' "$MANIFEST")" = 1
|
test "$(yq '[.replicas[] | select(.name == strenv(DEPLOYMENT_NAME))] | length' "$MANIFEST")" = 1
|
||||||
REPLICAS="$(yq '.replicas[] | select(.name == strenv(DEPLOYMENT_NAME)) | .count' "$MANIFEST")"
|
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"
|
git -C "$STATE_DIR" rev-parse HEAD >"$GITOPS_RESULT_PATH"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
git -C "$STATE_DIR" add apps/url-shortener/overlays/staging/kustomization.yaml apps/url-shortener/overlays/staging/configmap.yaml
|
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 staging $REVISION"
|
git -C "$STATE_DIR" commit -m "chore(url-shortener): deploy $DEPLOY_ENV $REVISION"
|
||||||
REMOTE_REVISION="$(git ls-remote "$SOURCE_REPO_URL" refs/heads/main | awk 'NR == 1 {print $1}')"
|
if [ "$DEPLOY_ENV" = staging ]; then
|
||||||
if [ "$REMOTE_REVISION" != "$REVISION" ]; then
|
REMOTE_REVISION="$(git ls-remote "$SOURCE_REPO_URL" refs/heads/main | awk 'NR == 1 {print $1}')"
|
||||||
echo 'Source revision was superseded before GitOps push.' >&2
|
if [ "$REMOTE_REVISION" != "$REVISION" ]; then
|
||||||
exit 1
|
echo 'Source revision was superseded before GitOps push.' >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
# A concurrent state update causes a non-fast-forward failure, never an overwrite.
|
# A concurrent state update causes a non-fast-forward failure, never an overwrite.
|
||||||
git -C "$STATE_DIR" push origin HEAD:main
|
git -C "$STATE_DIR" push origin HEAD:main
|
||||||
|
|||||||
Reference in new issue
Block a user