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

+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