Files
service-state/ci/blog/tasks.yaml
T

471 lines
17 KiB
YAML

apiVersion: tekton.dev/v1
kind: Task
metadata:
name: blog-clone
spec:
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
readOnly: true
steps:
- name: clone
image: docker.io/alpine/git:2.49.1@sha256:c0280cf9572316299b08544065d3bf35db65043d5e3963982ec50647d2746e26
computeResources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: "1"
memory: 1Gi
env:
- name: REPO_URL
value: https://git.dahyeon.us/dahyeon/blog.git
- name: REVISION
value: $(params.revision)
- name: DEPLOY_ENV
value: $(params.environment)
- name: SOURCE_DIR
value: $(workspaces.source.path)
- name: GITEA_CREDENTIALS_DIR
value: $(workspaces.credentials.path)
script: |
#!/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
AUTH_DIR="$(mktemp -d)"
trap 'rm -rf "$AUTH_DIR"' EXIT
cat >"$AUTH_DIR/askpass" <<'EOF'
#!/bin/sh
case "$1" in
*Username*) cat "$GITEA_CREDENTIALS_DIR/username" ;;
*Password*) cat "$GITEA_CREDENTIALS_DIR/password" ;;
*) exit 1 ;;
esac
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"
test "$(git -C "$SOURCE_DIR" rev-parse HEAD)" = "$REVISION"
---
apiVersion: tekton.dev/v1
kind: Task
metadata:
name: blog-quality
spec:
description: Run the Dockerfile quality target with a branch-scoped registry cache and no runtime credentials.
params:
- name: environment
type: string
default: staging
workspaces:
- name: source
readOnly: true
- name: dockerconfig
readOnly: true
volumes:
- name: buildkit-state
emptyDir: {}
steps:
- name: verify-rust
image: docker.io/moby/buildkit:v0.31.1-rootless@sha256:946cf909534b789c9b84af75e6d4f8cb23c3b9cb387daec5ba0f4878a3e4647c
computeResources:
requests:
cpu: 500m
memory: 6Gi
limits:
cpu: "4"
memory: 20Gi
securityContext:
privileged: false
allowPrivilegeEscalation: true
runAsNonRoot: true
runAsUser: 1000
runAsGroup: 1000
seccompProfile:
type: Unconfined
appArmorProfile:
type: Unconfined
volumeMounts:
- name: buildkit-state
mountPath: /home/user/.local/share/buildkit
env:
- name: BUILDKITD_FLAGS
value: --oci-worker-no-process-sandbox
- name: DOCKER_CONFIG
value: $(workspaces.dockerconfig.path)
- name: SOURCE_DIR
value: $(workspaces.source.path)
- name: DEPLOY_ENV
value: $(params.environment)
- name: CACHE_REPOSITORY
value: registry.dahyeon.us/dahyeon/blog
script: |
#!/bin/sh
set -eu
case "$DEPLOY_ENV" in staging|prod) ;; *) echo 'Invalid deployment environment' >&2; exit 1;; esac
# Export dependencies before checking the app, so a failed check does
# not discard all the work performed by this ephemeral builder.
buildctl-daemonless.sh build \
--frontend dockerfile.v0 \
--local context="$SOURCE_DIR" \
--local dockerfile="$SOURCE_DIR" \
--opt filename=Dockerfile \
--opt target=quality-deps \
--opt platform=linux/arm64 \
--import-cache "type=registry,ref=$CACHE_REPOSITORY:cache-quality-deps-arm64-$DEPLOY_ENV" \
--import-cache "type=registry,ref=$CACHE_REPOSITORY:cache-quality-arm64-$DEPLOY_ENV" \
--import-cache "type=registry,ref=$CACHE_REPOSITORY:cache-release-arm64-$DEPLOY_ENV" \
--export-cache "type=registry,ref=$CACHE_REPOSITORY:cache-quality-deps-arm64-$DEPLOY_ENV,mode=max,image-manifest=true,oci-mediatypes=true"
buildctl-daemonless.sh build \
--frontend dockerfile.v0 \
--local context="$SOURCE_DIR" \
--local dockerfile="$SOURCE_DIR" \
--opt filename=Dockerfile \
--opt target=quality \
--opt platform=linux/arm64 \
--import-cache "type=registry,ref=$CACHE_REPOSITORY:cache-quality-deps-arm64-$DEPLOY_ENV" \
--import-cache "type=registry,ref=$CACHE_REPOSITORY:cache-quality-arm64-$DEPLOY_ENV" \
--import-cache "type=registry,ref=$CACHE_REPOSITORY:cache-release-arm64-$DEPLOY_ENV" \
--export-cache "type=registry,ref=$CACHE_REPOSITORY:cache-quality-arm64-$DEPLOY_ENV,mode=max,image-manifest=true,oci-mediatypes=true"
---
apiVersion: tekton.dev/v1
kind: Task
metadata:
name: blog-build-arm64
spec:
description: Build and push the linux/arm64 full-stack Leptos image natively using rootless BuildKit.
params:
- name: revision
type: string
- name: image
type: string
- name: environment
type: string
default: staging
results:
- name: image-url
description: Published repository and unique tag.
- name: image-digest
description: Immutable linux/arm64 image digest.
workspaces:
- name: credentials
readOnly: true
- name: dockerconfig
readOnly: true
volumes:
- name: build-source
emptyDir: {}
- name: buildkit-state
emptyDir: {}
steps:
- name: clone-source
image: docker.io/alpine/git:2.49.1@sha256:c0280cf9572316299b08544065d3bf35db65043d5e3963982ec50647d2746e26
computeResources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: "1"
memory: 1Gi
securityContext:
runAsNonRoot: true
runAsUser: 1000
runAsGroup: 1000
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
workspaces:
- name: credentials
volumeMounts:
- name: build-source
mountPath: /workspace/build
env:
- name: REPO_URL
value: https://git.dahyeon.us/dahyeon/blog.git
- name: REVISION
value: $(params.revision)
- name: GITEA_CREDENTIALS_DIR
value: $(workspaces.credentials.path)
script: |
#!/bin/sh
set -eu
umask 0002
printf '%s\n' "$REVISION" | grep -Eq '^[0-9a-f]{40}$'
test "$REVISION" != '0000000000000000000000000000000000000000'
AUTH_DIR="$(mktemp -d)"
trap 'rm -rf "$AUTH_DIR"' EXIT
cat >"$AUTH_DIR/askpass" <<'EOF'
#!/bin/sh
case "$1" in
*Username*) cat "$GITEA_CREDENTIALS_DIR/username" ;;
*Password*) cat "$GITEA_CREDENTIALS_DIR/password" ;;
*) exit 1 ;;
esac
EOF
chmod 0700 "$AUTH_DIR/askpass"
export GIT_ASKPASS="$AUTH_DIR/askpass" GIT_TERMINAL_PROMPT=0
git clone --quiet --no-tags "$REPO_URL" /workspace/build/source
git -C /workspace/build/source checkout --quiet --detach "$REVISION"
test "$(git -C /workspace/build/source rev-parse HEAD)" = "$REVISION"
- name: build-and-push
image: docker.io/moby/buildkit:v0.31.1-rootless@sha256:946cf909534b789c9b84af75e6d4f8cb23c3b9cb387daec5ba0f4878a3e4647c
computeResources:
requests:
cpu: "1"
memory: 2Gi
limits:
cpu: "4"
memory: 20Gi
securityContext:
privileged: false
allowPrivilegeEscalation: true
runAsNonRoot: true
runAsUser: 1000
runAsGroup: 1000
seccompProfile:
type: Unconfined
appArmorProfile:
type: Unconfined
workspaces:
- name: dockerconfig
volumeMounts:
- name: build-source
mountPath: /workspace/build
- name: buildkit-state
mountPath: /home/user/.local/share/buildkit
env:
- name: BUILDKITD_FLAGS
value: --oci-worker-no-process-sandbox
- name: DOCKER_CONFIG
value: $(workspaces.dockerconfig.path)
- name: IMAGE
value: $(params.image)
- name: DEPLOY_ENV
value: $(params.environment)
- name: CACHE_REPOSITORY
value: registry.dahyeon.us/dahyeon/blog
- name: METADATA_FILE
value: /workspace/build/build-metadata.json
script: |
#!/bin/sh
set -eu
printf '%s\n' "$IMAGE" | grep -Eq '^registry\.dahyeon\.us/dahyeon/blog:sha-[0-9a-f]{40}-[a-zA-Z0-9-]+$'
case "$DEPLOY_ENV" in staging|prod) ;; *) echo 'Invalid deployment environment' >&2; exit 1;; esac
buildctl-daemonless.sh build \
--frontend dockerfile.v0 \
--local context=/workspace/build/source \
--local dockerfile=/workspace/build/source \
--opt filename=Dockerfile \
--opt target=runtime \
--opt platform=linux/arm64 \
--import-cache "type=registry,ref=$CACHE_REPOSITORY:cache-release-arm64-$DEPLOY_ENV" \
--import-cache "type=registry,ref=$CACHE_REPOSITORY:cache-quality-deps-arm64-$DEPLOY_ENV" \
--import-cache "type=registry,ref=$CACHE_REPOSITORY:cache-quality-arm64-$DEPLOY_ENV" \
--export-cache "type=registry,ref=$CACHE_REPOSITORY:cache-release-arm64-$DEPLOY_ENV,mode=max,image-manifest=true,oci-mediatypes=true" \
--output "type=image,name=$IMAGE,push=true" \
--metadata-file "$METADATA_FILE"
- name: record-digest
image: docker.io/mikefarah/yq:4.47.2@sha256:76def1f56f456ecc1c3173ea275218ee17139bc2018c5a07887b15afd88ec03e
computeResources:
requests:
cpu: 50m
memory: 64Mi
limits:
cpu: 250m
memory: 256Mi
securityContext:
runAsNonRoot: true
runAsUser: 1000
runAsGroup: 1000
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
volumeMounts:
- name: build-source
mountPath: /workspace/build
readOnly: true
env:
- name: IMAGE
value: $(params.image)
- name: METADATA_FILE
value: /workspace/build/build-metadata.json
- name: DIGEST_RESULT_PATH
value: $(results.image-digest.path)
- name: IMAGE_RESULT_PATH
value: $(results.image-url.path)
script: |
#!/bin/sh
set -eu
DIGEST="$(yq -r '."containerimage.digest"' "$METADATA_FILE")"
printf '%s\n' "$DIGEST" | grep -Eq '^sha256:[0-9a-f]{64}$'
printf '%s' "$IMAGE" >"$IMAGE_RESULT_PATH"
printf '%s' "$DIGEST" >"$DIGEST_RESULT_PATH"
---
apiVersion: tekton.dev/v1
kind: Task
metadata:
name: blog-update-gitops
spec:
description: Publish the selected environment's app digest to service-state keeping replicas at 1.
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.
workspaces:
- name: source
- name: credentials
readOnly: true
stepTemplate:
securityContext:
runAsNonRoot: true
runAsUser: 1000
runAsGroup: 1000
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
env:
- name: STATE_REPO_URL
value: https://git.dahyeon.us/dahyeon/service-state.git
- name: SOURCE_REPO_URL
value: https://git.dahyeon.us/dahyeon/blog.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
value: $(workspaces.credentials.path)
steps:
- name: clone-state
image: docker.io/alpine/git:2.49.1@sha256:c0280cf9572316299b08544065d3bf35db65043d5e3963982ec50647d2746e26
computeResources:
requests:
cpu: 50m
memory: 64Mi
limits:
cpu: 500m
memory: 512Mi
script: |
#!/bin/sh
set -eu
printf '%s\n' "$REVISION" | grep -Eq '^[0-9a-f]{40}$'
case "$STATE_DIR" in /workspace/*/.cache/gitops-state) ;; *) echo 'Unexpected GitOps path' >&2; exit 1;; esac
AUTH_DIR="$(mktemp -d)"
trap 'rm -rf "$AUTH_DIR"' EXIT
cat >"$AUTH_DIR/askpass" <<'EOF'
#!/bin/sh
case "$1" in
*Username*) cat "$GITEA_CREDENTIALS_DIR/username" ;;
*Password*) cat "$GITEA_CREDENTIALS_DIR/password" ;;
*) exit 1 ;;
esac
EOF
chmod 0700 "$AUTH_DIR/askpass"
export GIT_ASKPASS="$AUTH_DIR/askpass" GIT_TERMINAL_PROMPT=0
rm -rf "$STATE_DIR"
git clone --quiet --depth=1 --branch main "$STATE_REPO_URL" "$STATE_DIR"
- name: update-manifest
image: docker.io/mikefarah/yq:4.47.2@sha256:76def1f56f456ecc1c3173ea275218ee17139bc2018c5a07887b15afd88ec03e
computeResources:
requests:
cpu: 50m
memory: 64Mi
limits:
cpu: 250m
memory: 256Mi
env:
- name: APP_IMAGE
value: registry.dahyeon.us/dahyeon/blog
- name: IMAGE_DIGEST
value: $(params.image-digest)
- name: DEPLOYMENT_NAME
value: blog
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}$'
MANIFEST="$STATE_DIR/apps/blog/overlays/$DEPLOY_ENV/kustomization.yaml"
test "$(yq '[.images[] | select(.name == strenv(APP_IMAGE))] | length' "$MANIFEST")" = 1
test "$(yq '[.replicas[] | select(.name == strenv(DEPLOYMENT_NAME))] | length' "$MANIFEST")" = 1
yq -i '(.images[] | select(.name == strenv(APP_IMAGE))) |= (del(.newTag) | .digest = strenv(IMAGE_DIGEST))' "$MANIFEST"
yq -i '(.replicas[] | select(.name == strenv(DEPLOYMENT_NAME))).count = 1' "$MANIFEST"
yq -i '.commonAnnotations."blog.dahyeon.us/source-revision" = strenv(REVISION)' "$MANIFEST"
- name: commit-and-push
image: docker.io/alpine/git:2.49.1@sha256:c0280cf9572316299b08544065d3bf35db65043d5e3963982ec50647d2746e26
computeResources:
requests:
cpu: 50m
memory: 64Mi
limits:
cpu: 500m
memory: 512Mi
env:
- name: GITOPS_RESULT_PATH
value: $(results.gitops-commit.path)
script: |
#!/bin/sh
set -eu
AUTH_DIR="$(mktemp -d)"
trap 'rm -rf "$AUTH_DIR"' EXIT
cat >"$AUTH_DIR/askpass" <<'EOF'
#!/bin/sh
case "$1" in
*Username*) cat "$GITEA_CREDENTIALS_DIR/username" ;;
*Password*) cat "$GITEA_CREDENTIALS_DIR/password" ;;
*) exit 1 ;;
esac
EOF
chmod 0700 "$AUTH_DIR/askpass"
export GIT_ASKPASS="$AUTH_DIR/askpass" GIT_TERMINAL_PROMPT=0
BASE_COMMIT="$(git -C "$STATE_DIR" rev-parse HEAD)"
REMOTE_STATE="$(git ls-remote "$STATE_REPO_URL" refs/heads/main | awk 'NR == 1 {print $1}')"
if [ "$REMOTE_STATE" != "$BASE_COMMIT" ]; then
echo 'service-state advanced; rerun from the latest source. No automatic rebase or force push.' >&2
exit 1
fi
git -C "$STATE_DIR" config user.name blog-tekton
git -C "$STATE_DIR" config user.email tekton@dahyeon.us
if git -C "$STATE_DIR" diff --quiet; then
git -C "$STATE_DIR" rev-parse HEAD >"$GITOPS_RESULT_PATH"
exit 0
fi
git -C "$STATE_DIR" add "apps/blog/overlays/$DEPLOY_ENV/kustomization.yaml"
git -C "$STATE_DIR" commit -m "chore(blog): deploy $DEPLOY_ENV $REVISION"
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
git -C "$STATE_DIR" push origin HEAD:main
git -C "$STATE_DIR" rev-parse HEAD >"$GITOPS_RESULT_PATH"