feat(url-shortener): deploy staging and prod branches automatically

This commit is contained in:
dahyeon committed 2026-11-04 14:19:16 +09:00
1 parent 3ec9732190
commit a02e372e85
5 files changed
+110 -110

No files matched your search

+77 -8
View File
@@ -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
-57
View File
@@ -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.' }
+20 -39
View File
@@ -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
+11 -4
View File
@@ -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: