diff --git a/ci/blog/README.md b/ci/blog/README.md index 2ef98fc..d27fea1 100644 --- a/ci/blog/README.md +++ b/ci/blog/README.md @@ -8,8 +8,8 @@ signed Gitea push -> blog-clone (정확한 SHA와 브랜치 최신 커밋 확인) -> blog-quality (Dockerfile quality: fmt + SSR tests/architecture + WASM check) - -> blog-build-arm64 (rootless BuildKit -> Harbor) - -> blog-update-gitops (환경별 digest/source revision/replica 커밋) + -> blog-build-arm64 (CDN 도구 검사 -> BuildKit image+rootfs -> CDN package/upload/HEAD 검증) + -> blog-update-gitops (환경별 digest/source revision/CDN release/replica 커밋) -> Argo CD 자동 Sync ``` @@ -48,6 +48,30 @@ hydration 변경 시 필요한 Playwright 검증은 앱 README의 별도 절차 이미지에는 SSR 바이너리·site·라이선스가 들어가며 Node와 dev-ui는 포함하지 않습니다. DB/Entra ID Secret은 quality, Docker build, WASM 번들에서 사용하지 않습니다. +`cdn.yaml`은 공용 버킷 `rabbitmaus`(`ap-southeast-2`)와 `https://cdn.rabbitmaus.com`을 +사용합니다. prod 프로젝트는 `blog`, staging은 `blog-staging`이므로 HTTPS 원본도 서로 다른 +prefix로 선택할 수 있습니다. CI에서 source SHA + LF + PipelineRun UID의 SHA-256 앞 16자를 +계산하고 같은 CDN 설정을 SSR·WASM에 컴파일합니다. BuildKit은 단일 solve에서 image와 local +rootfs를 함께 내보냅니다. `/app/site`를 다시 빌드하지 않고 앱 저장소 `tools/cdn`으로 포장합니다. + +`pkg/`는 원본·br·gzip, `assets/`는 WOFF2 원본만 S3에 게시합니다. 나머지 공개 파일은 +프로젝트 HTTPS 원본이 제공합니다. 모든 객체를 조건부 PUT 후 checksum-enabled HEAD로 +검사하며, 완료 매니페스트가 성공해야 CDN Task 결과와 후속 GitOps 갱신을 허용합니다. +GitOps annotation에는 release ID와 S3 완료 키도 남깁니다. 충돌하는 객체는 덮어쓰지 않고 +실패하며 이전 릴리즈·부분 업로드를 자동 삭제하지 않습니다. + +Infisical 플랫폼 프로젝트의 `prod` 환경, 기존 `/platform/tekton/blog` 경로에 +`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`를 등록합니다. 기존 `blog-ci-credentials`가 +공용 Kubernetes Secret `blog-cdn`으로 동기화하며 prod/staging의 `publish-cdn` step만 받습니다. +이 공용 CI 주체에는 버킷의 `blog/*`, `blog-staging/*` 두 prefix에 대한 +`s3:PutObject`·`s3:GetObject` 권한이 필요합니다. 임시 자격 증명은 +`AWS_SESSION_TOKEN`을 Secret template에 추가합니다. 앱 Pod에는 S3 쓰기 권한이 없습니다. + +CloudFront 리소스와 Function은 이 변경에 포함하지 않습니다. 두 S3 behavior와 +`/blog/*` → prod HTTPS, `/blog-staging/*` → staging HTTPS, CORS·OAC·압축 선택을 먼저 구성해야 +합니다. 업로드 성공은 CF 설정의 정확성까지 보장하지 않습니다. 앱 저장소의 +`tools/cdn/README.md`에 객체·메타데이터·참조 변환과 로컬 검증 계약을 정리했습니다. + 두 BuildKit 작업은 `registry.dahyeon.us/dahyeon/blog`의 `cache-quality-deps-arm64-`, `cache-quality-arm64-`, `cache-release-arm64-` 태그를 `mode=max`로 저장합니다. @@ -65,7 +89,7 @@ GitOps push 직전에 소스 브랜치 최신 SHA를 다시 확인합니다. ser ## 처음 연결할 때 -1. 앱의 배포 브랜치에 `Dockerfile`, `.dockerignore`, `tools/ci/check.sh`와 컴파일에 +1. 앱의 배포 브랜치에 `Dockerfile`, `.dockerignore`, `tools/ci/check.sh`, `tools/cdn`(잠금 파일 포함)과 컴파일에 필요한 생성 자산이 커밋되어 있어야 합니다. 로컬 미커밋 파일은 CI에 전달되지 않습니다. 2. Harbor `dahyeon/blog` 접근 권한을 준비합니다. CI에는 push robot을, `blog-staging`/`blog-prod`의 `harbor-registry`에는 pull 권한을 준비합니다. @@ -79,6 +103,8 @@ GitOps push 직전에 소스 브랜치 최신 SHA를 다시 확인합니다. ser | `GITEA_TOKEN` | 해당 bot의 토큰 | | `HARBOR_DOCKER_CONFIG_JSON` | CI push용 Docker config JSON | | `GITEA_WEBHOOK_SECRET` | push 서명 검증 | + | `AWS_ACCESS_KEY_ID` | prod/staging 공용 S3 게시 자격 증명 | + | `AWS_SECRET_ACCESS_KEY` | 해당 AWS 키의 비밀 키 | 4. [앱 런타임 Secret](../../apps/blog/README.md)의 staging/prod 6개 키와 공유 `infisical-auth` identity의 프로젝트·환경·경로 권한을 확인합니다. diff --git a/ci/blog/cdn.yaml b/ci/blog/cdn.yaml new file mode 100644 index 0000000..bfe058a --- /dev/null +++ b/ci/blog/cdn.yaml @@ -0,0 +1,20 @@ +# One distribution and bucket; separate project prefixes select the HTTPS origin. +apiVersion: v1 +kind: ConfigMap +metadata: + name: blog-cdn-prod +data: + CDN_PROJECT: blog + CDN_BUCKET: rabbitmaus + AWS_REGION: ap-southeast-2 + CDN_BASE_URL: https://cdn.rabbitmaus.com +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: blog-cdn-staging +data: + CDN_PROJECT: blog-staging + CDN_BUCKET: rabbitmaus + AWS_REGION: ap-southeast-2 + CDN_BASE_URL: https://cdn.rabbitmaus.com diff --git a/ci/blog/infisical-secret.yaml b/ci/blog/infisical-secret.yaml index 4be8547..5d3574b 100644 --- a/ci/blog/infisical-secret.yaml +++ b/ci/blog/infisical-secret.yaml @@ -43,3 +43,13 @@ spec: engineVersion: v1 data: secretToken: "{{ .GITEA_WEBHOOK_SECRET.Value }}" + - name: blog-cdn + namespace: tekton-ci + kind: Secret + creationPolicy: Owner + secretType: Opaque + template: + engineVersion: v1 + data: + AWS_ACCESS_KEY_ID: "{{ .AWS_ACCESS_KEY_ID.Value }}" + AWS_SECRET_ACCESS_KEY: "{{ .AWS_SECRET_ACCESS_KEY.Value }}" diff --git a/ci/blog/kustomization.yaml b/ci/blog/kustomization.yaml index ff514ca..e5e21b6 100644 --- a/ci/blog/kustomization.yaml +++ b/ci/blog/kustomization.yaml @@ -4,6 +4,7 @@ namespace: tekton-ci resources: - foundation.yaml - infisical-secret.yaml + - cdn.yaml - tasks.yaml - pipeline.yaml - trigger.yaml diff --git a/ci/blog/pipeline.yaml b/ci/blog/pipeline.yaml index cbd0bae..eec7c70 100644 --- a/ci/blog/pipeline.yaml +++ b/ci/blog/pipeline.yaml @@ -3,13 +3,17 @@ kind: Pipeline metadata: name: blog-staging spec: - description: Validate an exact staging branch revision, publish an arm64 full-stack Leptos image, then update staging GitOps. + description: Validate an exact staging revision, publish the arm64 image and verify its S3 CDN release, then update staging GitOps. params: - name: git-revision type: string results: - name: image-digest value: $(tasks.build-arm64.results.image-digest) + - name: cdn-release-id + value: $(tasks.build-arm64.results.cdn-release-id) + - name: cdn-manifest-key + value: $(tasks.build-arm64.results.cdn-manifest-key) - name: gitops-commit value: $(tasks.update-gitops.results.gitops-commit) workspaces: @@ -53,6 +57,8 @@ spec: value: $(params.git-revision) - name: image value: registry.dahyeon.us/dahyeon/blog:sha-$(params.git-revision)-$(context.pipelineRun.uid) + - name: pipeline-run-uid + value: $(context.pipelineRun.uid) - name: environment value: staging workspaces: @@ -72,6 +78,10 @@ spec: value: staging - name: image-digest value: $(tasks.build-arm64.results.image-digest) + - name: cdn-release-id + value: $(tasks.build-arm64.results.cdn-release-id) + - name: cdn-manifest-key + value: $(tasks.build-arm64.results.cdn-manifest-key) workspaces: - name: source workspace: source @@ -83,13 +93,17 @@ kind: Pipeline metadata: name: blog-prod spec: - description: Validate an exact prod branch revision, publish an arm64 full-stack Leptos image, then update production GitOps. + description: Validate an exact prod revision, publish the arm64 image and verify its S3 CDN release, then update production GitOps. params: - name: git-revision type: string results: - name: image-digest value: $(tasks.build-arm64.results.image-digest) + - name: cdn-release-id + value: $(tasks.build-arm64.results.cdn-release-id) + - name: cdn-manifest-key + value: $(tasks.build-arm64.results.cdn-manifest-key) - name: gitops-commit value: $(tasks.update-gitops.results.gitops-commit) workspaces: @@ -133,6 +147,8 @@ spec: value: $(params.git-revision) - name: image value: registry.dahyeon.us/dahyeon/blog:sha-$(params.git-revision)-$(context.pipelineRun.uid) + - name: pipeline-run-uid + value: $(context.pipelineRun.uid) - name: environment value: prod workspaces: @@ -152,6 +168,10 @@ spec: value: prod - name: image-digest value: $(tasks.build-arm64.results.image-digest) + - name: cdn-release-id + value: $(tasks.build-arm64.results.cdn-release-id) + - name: cdn-manifest-key + value: $(tasks.build-arm64.results.cdn-manifest-key) workspaces: - name: source workspace: source diff --git a/ci/blog/tasks.yaml b/ci/blog/tasks.yaml index 88f48dc..4cde64d 100644 --- a/ci/blog/tasks.yaml +++ b/ci/blog/tasks.yaml @@ -151,12 +151,14 @@ kind: Task metadata: name: blog-build-arm64 spec: - description: Build and push the linux/arm64 full-stack Leptos image natively using rootless BuildKit. + description: Build one runtime image and filesystem, then package, conditionally upload and verify its CDN release before GitOps can run. params: - name: revision type: string - name: image type: string + - name: pipeline-run-uid + type: string - name: environment type: string default: staging @@ -165,6 +167,10 @@ spec: description: Published repository and unique tag. - name: image-digest description: Immutable linux/arm64 image digest. + - name: cdn-release-id + description: Verified immutable CDN release ID. + - name: cdn-manifest-key + description: S3 completion manifest key, written only after every object passes verification. workspaces: - name: credentials readOnly: true @@ -175,6 +181,34 @@ spec: emptyDir: {} - name: buildkit-state emptyDir: {} + stepTemplate: + env: + - name: SOURCE_REVISION + value: $(params.revision) + - name: PIPELINE_RUN_UID + value: $(params.pipeline-run-uid) + - name: DEPLOY_ENV + value: $(params.environment) + - name: CDN_PROJECT + valueFrom: + configMapKeyRef: + name: blog-cdn-$(params.environment) + key: CDN_PROJECT + - name: CDN_BASE_URL + valueFrom: + configMapKeyRef: + name: blog-cdn-$(params.environment) + key: CDN_BASE_URL + - name: CDN_BUCKET + valueFrom: + configMapKeyRef: + name: blog-cdn-$(params.environment) + key: CDN_BUCKET + - name: AWS_REGION + valueFrom: + configMapKeyRef: + name: blog-cdn-$(params.environment) + key: AWS_REGION steps: - name: clone-source image: docker.io/alpine/git:2.49.1@sha256:c0280cf9572316299b08544065d3bf35db65043d5e3963982ec50647d2746e26 @@ -226,6 +260,35 @@ spec: 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: prepare-cdn + image: docker.io/library/node:26.7.0-trixie-slim@sha256:5758d367d7b4f48b73a9bb3530e687e47efb289f3b43f9c0450a25225ae0db5d + workingDir: /workspace/build/source + computeResources: + requests: + cpu: 100m + memory: 256Mi + limits: + cpu: "1" + memory: 1Gi + securityContext: + runAsNonRoot: true + runAsUser: 1000 + runAsGroup: 1000 + allowPrivilegeEscalation: false + capabilities: + drop: [ALL] + volumeMounts: + - name: build-source + mountPath: /workspace/build + env: + - name: npm_config_cache + value: /tmp/npm-cache + script: | + #!/bin/sh + set -eu + npm ci --prefix tools/cdn --ignore-scripts --no-audit --no-fund + npm test --prefix tools/cdn + node tools/cdn/cli.mjs check-config --release-result /workspace/build/release-id - name: build-and-push image: docker.io/moby/buildkit:v0.31.1-rootless@sha256:946cf909534b789c9b84af75e6d4f8cb23c3b9cb387daec5ba0f4878a3e4647c computeResources: @@ -270,6 +333,8 @@ spec: 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 + CDN_RELEASE="$(cat /workspace/build/release-id)" + printf '%s\n' "$CDN_RELEASE" | grep -Eq '^[0-9a-f]{16}$' buildctl-daemonless.sh build \ --frontend dockerfile.v0 \ --local context=/workspace/build/source \ @@ -277,11 +342,15 @@ spec: --opt filename=Dockerfile \ --opt target=runtime \ --opt platform=linux/arm64 \ + --opt "build-arg:BLOG_CDN_BASE=$CDN_BASE_URL" \ + --opt "build-arg:BLOG_CDN_PROJECT=$CDN_PROJECT" \ + --opt "build-arg:BLOG_CDN_RELEASE=$CDN_RELEASE" \ --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" \ + --output type=local,dest=/workspace/build/runtime,platform-split=false \ --metadata-file "$METADATA_FILE" - name: record-digest image: docker.io/mikefarah/yq:4.47.2@sha256:76def1f56f456ecc1c3173ea275218ee17139bc2018c5a07887b15afd88ec03e @@ -320,6 +389,47 @@ spec: printf '%s\n' "$DIGEST" | grep -Eq '^sha256:[0-9a-f]{64}$' printf '%s' "$IMAGE" >"$IMAGE_RESULT_PATH" printf '%s' "$DIGEST" >"$DIGEST_RESULT_PATH" + - name: publish-cdn + image: docker.io/library/node:26.7.0-trixie-slim@sha256:5758d367d7b4f48b73a9bb3530e687e47efb289f3b43f9c0450a25225ae0db5d + workingDir: /workspace/build/source + computeResources: + requests: + cpu: 250m + memory: 512Mi + limits: + cpu: "2" + memory: 2Gi + securityContext: + runAsNonRoot: true + runAsUser: 1000 + runAsGroup: 1000 + allowPrivilegeEscalation: false + capabilities: + drop: [ALL] + volumeMounts: + - name: build-source + mountPath: /workspace/build + envFrom: + - secretRef: + name: blog-cdn + env: + - name: AWS_EC2_METADATA_DISABLED + value: "true" + - name: RELEASE_RESULT_PATH + value: $(results.cdn-release-id.path) + - name: MANIFEST_RESULT_PATH + value: $(results.cdn-manifest-key.path) + script: | + #!/bin/sh + set -eu + node tools/cdn/cli.mjs package \ + --site-root /workspace/build/runtime/app/site \ + --output-root /workspace/build/cdn \ + --image-metadata /workspace/build/build-metadata.json + node tools/cdn/cli.mjs publish \ + --output-root /workspace/build/cdn \ + --release-result "$RELEASE_RESULT_PATH" \ + --manifest-result "$MANIFEST_RESULT_PATH" --- apiVersion: tekton.dev/v1 kind: Task @@ -332,6 +442,10 @@ spec: type: string - name: image-digest type: string + - name: cdn-release-id + type: string + - name: cdn-manifest-key + type: string - name: environment type: string default: staging @@ -407,6 +521,10 @@ spec: value: registry.dahyeon.us/dahyeon/blog - name: IMAGE_DIGEST value: $(params.image-digest) + - name: CDN_RELEASE + value: $(params.cdn-release-id) + - name: CDN_MANIFEST_KEY + value: $(params.cdn-manifest-key) - name: DEPLOYMENT_NAME value: blog script: | @@ -414,12 +532,17 @@ spec: 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' "$CDN_RELEASE" | grep -Eq '^[0-9a-f]{16}$' + case "$DEPLOY_ENV" in prod) CDN_PROJECT=blog ;; staging) CDN_PROJECT=blog-staging ;; esac + test "$CDN_MANIFEST_KEY" = "$CDN_PROJECT/_releases/$CDN_RELEASE/complete.json" 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" + yq -i '.commonAnnotations."blog.dahyeon.us/cdn-release" = strenv(CDN_RELEASE)' "$MANIFEST" + yq -i '.commonAnnotations."blog.dahyeon.us/cdn-manifest" = strenv(CDN_MANIFEST_KEY)' "$MANIFEST" - name: commit-and-push image: docker.io/alpine/git:2.49.1@sha256:c0280cf9572316299b08544065d3bf35db65043d5e3963982ec50647d2746e26 computeResources: