diff --git a/README.md b/README.md index 9aa6695..47901dd 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,6 @@ Argo CD는 아래 경로에서 서비스 배포 대상을 찾습니다. - `lolympus-tournament-support` - `persona-lenz` +- `url-shortener` + +URL shortener의 앱 배포 설정은 [apps/url-shortener](apps/url-shortener/README.md), Tekton 설치·Infisical·webhook 설정은 [ci/url-shortener](ci/url-shortener)에 있습니다. CI 경로는 앱 overlay 자동 검색과 별도로 등록합니다. diff --git a/apps/url-shortener/README.md b/apps/url-shortener/README.md new file mode 100644 index 0000000..48b2a5e --- /dev/null +++ b/apps/url-shortener/README.md @@ -0,0 +1,6 @@ +# URL shortener + +- 환경: `overlays/staging`, `overlays/prod` +- 이미지: `registry.dahyeon.us/dahyeon/url-shortener` +- Harbor pull Secret: `harbor-registry` +- 앱 Secret: `url-shortener-secrets` (Infisical) diff --git a/apps/url-shortener/base/deployment.yaml b/apps/url-shortener/base/deployment.yaml new file mode 100644 index 0000000..ea7844e --- /dev/null +++ b/apps/url-shortener/base/deployment.yaml @@ -0,0 +1,89 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: url-shortener + labels: + app.kubernetes.io/component: app +spec: + replicas: 0 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 0 + maxSurge: 1 + selector: + matchLabels: + app.kubernetes.io/name: url-shortener + app.kubernetes.io/component: app + template: + metadata: + labels: + app.kubernetes.io/name: url-shortener + app.kubernetes.io/component: app + spec: + serviceAccountName: url-shortener + automountServiceAccountToken: false + imagePullSecrets: + - name: harbor-registry + terminationGracePeriodSeconds: 120 + securityContext: + runAsNonRoot: true + runAsUser: 65532 + runAsGroup: 65532 + fsGroup: 65532 + seccompProfile: + type: RuntimeDefault + affinity: + podAntiAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - weight: 100 + podAffinityTerm: + labelSelector: + matchLabels: + app.kubernetes.io/name: url-shortener + app.kubernetes.io/component: app + topologyKey: kubernetes.io/hostname + containers: + - name: app + image: registry.dahyeon.us/dahyeon/url-shortener + imagePullPolicy: IfNotPresent + securityContext: + allowPrivilegeEscalation: false + readOnlyRootFilesystem: true + capabilities: + drop: + - ALL + envFrom: + - configMapRef: + name: url-shortener-config + - secretRef: + name: url-shortener-secrets + ports: + - name: http + containerPort: 3000 + protocol: TCP + startupProbe: + httpGet: + path: /health/live + port: http + periodSeconds: 5 + failureThreshold: 24 + livenessProbe: + httpGet: + path: /health/live + port: http + periodSeconds: 15 + timeoutSeconds: 3 + readinessProbe: + httpGet: + path: /health/ready + port: http + periodSeconds: 10 + timeoutSeconds: 3 + resources: + requests: + cpu: 50m + memory: 128Mi + limits: + cpu: "1" + memory: 512Mi diff --git a/apps/url-shortener/base/kustomization.yaml b/apps/url-shortener/base/kustomization.yaml new file mode 100644 index 0000000..1b5aeda --- /dev/null +++ b/apps/url-shortener/base/kustomization.yaml @@ -0,0 +1,13 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: + - deployment.yaml + - services.yaml + - serviceaccount.yaml + - poddisruptionbudget.yaml +labels: + - includeSelectors: true + pairs: + app.kubernetes.io/name: url-shortener + app.kubernetes.io/part-of: url-shortener + app.kubernetes.io/managed-by: kustomize diff --git a/apps/url-shortener/base/poddisruptionbudget.yaml b/apps/url-shortener/base/poddisruptionbudget.yaml new file mode 100644 index 0000000..162a6a4 --- /dev/null +++ b/apps/url-shortener/base/poddisruptionbudget.yaml @@ -0,0 +1,10 @@ +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: url-shortener +spec: + minAvailable: 1 + selector: + matchLabels: + app.kubernetes.io/name: url-shortener + app.kubernetes.io/component: app diff --git a/apps/url-shortener/base/serviceaccount.yaml b/apps/url-shortener/base/serviceaccount.yaml new file mode 100644 index 0000000..36fc2e3 --- /dev/null +++ b/apps/url-shortener/base/serviceaccount.yaml @@ -0,0 +1,5 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: url-shortener +automountServiceAccountToken: false diff --git a/apps/url-shortener/base/services.yaml b/apps/url-shortener/base/services.yaml new file mode 100644 index 0000000..2c7ffcf --- /dev/null +++ b/apps/url-shortener/base/services.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Service +metadata: + name: url-shortener +spec: + type: ClusterIP + selector: + app.kubernetes.io/name: url-shortener + app.kubernetes.io/component: app + ports: + - name: http + port: 3000 + targetPort: http + protocol: TCP diff --git a/apps/url-shortener/overlays/prod/configmap.yaml b/apps/url-shortener/overlays/prod/configmap.yaml new file mode 100644 index 0000000..6a1ad88 --- /dev/null +++ b/apps/url-shortener/overlays/prod/configmap.yaml @@ -0,0 +1,20 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: url-shortener-config +data: + APP_ENV: prod + HOST: 0.0.0.0 + PORT: "3000" + DEV_AUTH_ENABLED: "false" + APP_ORIGIN: https://urls.dahyeon.us + API_ORIGIN: https://urls-api.dahyeon.us + SHORT_ORIGIN: https://rabbitma.us + SESSION_HOURS: "8" + # Set during promotion; database ID must match deploy/cloudflare/prod.json in the source revision. + CLOUDFLARE_ACCOUNT_ID: "" + CLOUDFLARE_D1_DATABASE_ID: "" + PUBLISH_WAIT_MS: "1500" + JOB_POLL_MS: "1000" + STATS_INTERVAL_MS: "60000" + API_RATE_LIMIT: "300" diff --git a/apps/url-shortener/overlays/prod/deployment-patch.yaml b/apps/url-shortener/overlays/prod/deployment-patch.yaml new file mode 100644 index 0000000..6fbeb72 --- /dev/null +++ b/apps/url-shortener/overlays/prod/deployment-patch.yaml @@ -0,0 +1,9 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: url-shortener +spec: + template: + metadata: + labels: + app.kubernetes.io/environment: prod diff --git a/apps/url-shortener/overlays/prod/infisical-secret.yaml b/apps/url-shortener/overlays/prod/infisical-secret.yaml new file mode 100644 index 0000000..5a8f408 --- /dev/null +++ b/apps/url-shortener/overlays/prod/infisical-secret.yaml @@ -0,0 +1,38 @@ +apiVersion: secrets.infisical.com/v1beta1 +kind: InfisicalStaticSecret +metadata: + name: url-shortener-secrets +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-secrets + namespace: url-shortener-prod + kind: Secret + creationPolicy: Owner + secretType: Opaque + template: + engineVersion: v1 + data: + MONGODB_CONNECTION_STRING: "{{ .MONGODB_CONNECTION_STRING.Value }}" + MICROSOFT_ENTRAID_TENANT_ID: "{{ .MICROSOFT_ENTRAID_TENANT_ID.Value }}" + MICROSOFT_ENTRAID_CLIENT_ID: "{{ .MICROSOFT_ENTRAID_CLIENT_ID.Value }}" + MICROSOFT_ENTRAID_CLIENT_SECRET: "{{ .MICROSOFT_ENTRAID_CLIENT_SECRET.Value }}" + MICROSOFT_ENTRAID_BOOTSTRAP_ADMIN_OIDS: "{{ .MICROSOFT_ENTRAID_BOOTSTRAP_ADMIN_OIDS.Value }}" + CLOUDFLARE_D1_API_TOKEN: "{{ .CLOUDFLARE_D1_API_TOKEN.Value }}" + INGEST_HMAC_SECRET: "{{ .INGEST_HMAC_SECRET.Value }}" + NEW_RELIC_LICENSE_KEY: "{{ .NEW_RELIC_LICENSE_KEY.Value }}" + MONGODB_DATABASE: "{{ .MONGODB_DATABASE.Value }}" + LOG_LEVEL: "{{ .LOG_LEVEL.Value }}" + NEW_RELIC_APP_NAME: "{{ .NEW_RELIC_APP_NAME.Value }}" + NEW_RELIC_ENABLED: "{{ .NEW_RELIC_ENABLED.Value }}" + NEW_RELIC_LOG_LEVEL: "{{ .NEW_RELIC_LOG_LEVEL.Value }}" diff --git a/apps/url-shortener/overlays/prod/ingress.yaml b/apps/url-shortener/overlays/prod/ingress.yaml new file mode 100644 index 0000000..4c9affe --- /dev/null +++ b/apps/url-shortener/overlays/prod/ingress.yaml @@ -0,0 +1,44 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: url-shortener-api + annotations: + cert-manager.io/cluster-issuer: letsencrypt-prod +spec: + ingressClassName: haproxy + tls: + - hosts: + - urls-api.dahyeon.us + secretName: url-shortener-api-prod-tls + rules: + - host: urls-api.dahyeon.us + http: + paths: + - path: /api + pathType: Prefix + backend: + service: + name: url-shortener + port: + number: 3000 + - path: /auth + pathType: Prefix + backend: + service: + name: url-shortener + port: + number: 3000 + - path: /service/v1 + pathType: Prefix + backend: + service: + name: url-shortener + port: + number: 3000 + - path: /ingest/v1/clicks + pathType: Exact + backend: + service: + name: url-shortener + port: + number: 3000 diff --git a/apps/url-shortener/overlays/prod/kustomization.yaml b/apps/url-shortener/overlays/prod/kustomization.yaml new file mode 100644 index 0000000..a38adb9 --- /dev/null +++ b/apps/url-shortener/overlays/prod/kustomization.yaml @@ -0,0 +1,17 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +namespace: url-shortener-prod +resources: + - ../../base + - configmap.yaml + - infisical-secret.yaml + - ingress.yaml +images: + - name: registry.dahyeon.us/dahyeon/url-shortener + newName: registry.dahyeon.us/dahyeon/url-shortener + newTag: bootstrap +replicas: + - name: url-shortener + count: 0 +patches: + - path: deployment-patch.yaml diff --git a/apps/url-shortener/overlays/staging/configmap.yaml b/apps/url-shortener/overlays/staging/configmap.yaml new file mode 100644 index 0000000..54b4cbe --- /dev/null +++ b/apps/url-shortener/overlays/staging/configmap.yaml @@ -0,0 +1,20 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: url-shortener-config +data: + APP_ENV: staging + HOST: 0.0.0.0 + PORT: "3000" + DEV_AUTH_ENABLED: "false" + APP_ORIGIN: https://urls-staging.dahyeon.us + API_ORIGIN: https://urls-api-staging.dahyeon.us + SHORT_ORIGIN: https://staging.rabbitma.us + SESSION_HOURS: "8" + # Tekton copies these public identifiers from its Cloudflare account and the source D1 profile. + CLOUDFLARE_ACCOUNT_ID: "" + CLOUDFLARE_D1_DATABASE_ID: "" + PUBLISH_WAIT_MS: "1500" + JOB_POLL_MS: "1000" + STATS_INTERVAL_MS: "60000" + API_RATE_LIMIT: "300" diff --git a/apps/url-shortener/overlays/staging/deployment-patch.yaml b/apps/url-shortener/overlays/staging/deployment-patch.yaml new file mode 100644 index 0000000..233de7b --- /dev/null +++ b/apps/url-shortener/overlays/staging/deployment-patch.yaml @@ -0,0 +1,9 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: url-shortener +spec: + template: + metadata: + labels: + app.kubernetes.io/environment: staging diff --git a/apps/url-shortener/overlays/staging/infisical-secret.yaml b/apps/url-shortener/overlays/staging/infisical-secret.yaml new file mode 100644 index 0000000..9e8fc22 --- /dev/null +++ b/apps/url-shortener/overlays/staging/infisical-secret.yaml @@ -0,0 +1,38 @@ +apiVersion: secrets.infisical.com/v1beta1 +kind: InfisicalStaticSecret +metadata: + name: url-shortener-secrets +spec: + infisicalAuthRef: + name: infisical-auth + namespace: infisical + syncOptions: + refreshInterval: 60s + instantUpdates: false + sources: + - projectId: 01c4ebe5-8c73-475c-ba29-56709483b0f1 + environmentSlug: staging + secretPath: / + recursive: false + targets: + - name: url-shortener-secrets + namespace: url-shortener-staging + kind: Secret + creationPolicy: Owner + secretType: Opaque + template: + engineVersion: v1 + data: + MONGODB_CONNECTION_STRING: "{{ .MONGODB_CONNECTION_STRING.Value }}" + MICROSOFT_ENTRAID_TENANT_ID: "{{ .MICROSOFT_ENTRAID_TENANT_ID.Value }}" + MICROSOFT_ENTRAID_CLIENT_ID: "{{ .MICROSOFT_ENTRAID_CLIENT_ID.Value }}" + MICROSOFT_ENTRAID_CLIENT_SECRET: "{{ .MICROSOFT_ENTRAID_CLIENT_SECRET.Value }}" + MICROSOFT_ENTRAID_BOOTSTRAP_ADMIN_OIDS: "{{ .MICROSOFT_ENTRAID_BOOTSTRAP_ADMIN_OIDS.Value }}" + CLOUDFLARE_D1_API_TOKEN: "{{ .CLOUDFLARE_D1_API_TOKEN.Value }}" + INGEST_HMAC_SECRET: "{{ .INGEST_HMAC_SECRET.Value }}" + NEW_RELIC_LICENSE_KEY: "{{ .NEW_RELIC_LICENSE_KEY.Value }}" + MONGODB_DATABASE: "{{ .MONGODB_DATABASE.Value }}" + LOG_LEVEL: "{{ .LOG_LEVEL.Value }}" + NEW_RELIC_APP_NAME: "{{ .NEW_RELIC_APP_NAME.Value }}" + NEW_RELIC_ENABLED: "{{ .NEW_RELIC_ENABLED.Value }}" + NEW_RELIC_LOG_LEVEL: "{{ .NEW_RELIC_LOG_LEVEL.Value }}" diff --git a/apps/url-shortener/overlays/staging/ingress.yaml b/apps/url-shortener/overlays/staging/ingress.yaml new file mode 100644 index 0000000..f6deb7e --- /dev/null +++ b/apps/url-shortener/overlays/staging/ingress.yaml @@ -0,0 +1,44 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: url-shortener-api + annotations: + cert-manager.io/cluster-issuer: letsencrypt-prod +spec: + ingressClassName: haproxy + tls: + - hosts: + - urls-api-staging.dahyeon.us + secretName: url-shortener-api-staging-tls + rules: + - host: urls-api-staging.dahyeon.us + http: + paths: + - path: /api + pathType: Prefix + backend: + service: + name: url-shortener + port: + number: 3000 + - path: /auth + pathType: Prefix + backend: + service: + name: url-shortener + port: + number: 3000 + - path: /service/v1 + pathType: Prefix + backend: + service: + name: url-shortener + port: + number: 3000 + - path: /ingest/v1/clicks + pathType: Exact + backend: + service: + name: url-shortener + port: + number: 3000 diff --git a/apps/url-shortener/overlays/staging/kustomization.yaml b/apps/url-shortener/overlays/staging/kustomization.yaml new file mode 100644 index 0000000..60c1da4 --- /dev/null +++ b/apps/url-shortener/overlays/staging/kustomization.yaml @@ -0,0 +1,17 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +namespace: url-shortener-staging +resources: + - ../../base + - configmap.yaml + - infisical-secret.yaml + - ingress.yaml +images: + - name: registry.dahyeon.us/dahyeon/url-shortener + newName: registry.dahyeon.us/dahyeon/url-shortener + newTag: bootstrap +replicas: + - name: url-shortener + count: 0 +patches: + - path: deployment-patch.yaml diff --git a/ci/url-shortener/foundation.yaml b/ci/url-shortener/foundation.yaml new file mode 100644 index 0000000..80e9b1e --- /dev/null +++ b/ci/url-shortener/foundation.yaml @@ -0,0 +1,68 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: url-shortener-pipeline +automountServiceAccountToken: false +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: url-shortener-event-listener +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: url-shortener-event-listener +rules: + - apiGroups: [tekton.dev] + resources: [pipelineruns] + verbs: [create, get, list] + - apiGroups: [triggers.tekton.dev] + resources: + [ + eventlisteners, + interceptors, + triggerbindings, + triggertemplates, + triggers, + ] + verbs: [get, list, watch] + - apiGroups: [""] + resources: [configmaps] + verbs: [get, list, watch] + - apiGroups: [""] + resources: [serviceaccounts] + resourceNames: [url-shortener-pipeline] + verbs: [impersonate] + - apiGroups: [""] + resources: [events] + verbs: [create, patch] + - apiGroups: [""] + resources: [secrets] + resourceNames: [url-shortener-webhook] + verbs: [get] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: url-shortener-event-listener +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: url-shortener-event-listener +subjects: + - kind: ServiceAccount + name: url-shortener-event-listener +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: url-shortener-event-listener-interceptors +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: tekton-triggers-eventlistener-clusterroles +subjects: + - kind: ServiceAccount + name: url-shortener-event-listener + namespace: tekton-ci diff --git a/ci/url-shortener/infisical-secret.yaml b/ci/url-shortener/infisical-secret.yaml new file mode 100644 index 0000000..656ff3a --- /dev/null +++ b/ci/url-shortener/infisical-secret.yaml @@ -0,0 +1,82 @@ +apiVersion: secrets.infisical.com/v1beta1 +kind: InfisicalStaticSecret +metadata: + name: url-shortener-ci-credentials +spec: + infisicalAuthRef: + name: infisical-auth + namespace: infisical + syncOptions: + refreshInterval: 60s + instantUpdates: false + sources: + - projectId: 8520b8c0-3796-4fd6-a03c-6f0508c8c42c + environmentSlug: prod + secretPath: /platform/tekton/url-shortener + recursive: false + targets: + - name: url-shortener-gitea + namespace: tekton-ci + kind: Secret + creationPolicy: Owner + secretType: kubernetes.io/basic-auth + template: + engineVersion: v1 + data: + username: "{{ .GITEA_USERNAME.Value }}" + password: "{{ .GITEA_TOKEN.Value }}" + - name: url-shortener-harbor + namespace: tekton-ci + kind: Secret + creationPolicy: Owner + secretType: kubernetes.io/dockerconfigjson + template: + engineVersion: v1 + data: + .dockerconfigjson: "{{ .HARBOR_DOCKER_CONFIG_JSON.Value }}" + - name: url-shortener-cloudflare + namespace: tekton-ci + kind: Secret + creationPolicy: Owner + secretType: Opaque + template: + engineVersion: v1 + data: + CLOUDFLARE_API_TOKEN: "{{ .CLOUDFLARE_API_TOKEN.Value }}" + CLOUDFLARE_ACCOUNT_ID: "{{ .CLOUDFLARE_ACCOUNT_ID.Value }}" + - name: url-shortener-webhook + namespace: tekton-ci + kind: Secret + creationPolicy: Owner + secretType: Opaque + template: + engineVersion: v1 + data: + secretToken: "{{ .GITEA_WEBHOOK_SECRET.Value }}" +--- +apiVersion: secrets.infisical.com/v1beta1 +kind: InfisicalStaticSecret +metadata: + name: url-shortener-ci-edge-staging +spec: + infisicalAuthRef: + name: infisical-auth + namespace: infisical + syncOptions: + refreshInterval: 60s + instantUpdates: false + sources: + - projectId: 01c4ebe5-8c73-475c-ba29-56709483b0f1 + environmentSlug: staging + secretPath: / + recursive: false + targets: + - name: url-shortener-edge-staging + namespace: tekton-ci + kind: Secret + creationPolicy: Owner + secretType: Opaque + template: + engineVersion: v1 + data: + INGEST_HMAC_SECRET: "{{ .INGEST_HMAC_SECRET.Value }}" diff --git a/ci/url-shortener/ingress.yaml b/ci/url-shortener/ingress.yaml new file mode 100644 index 0000000..6ee8e30 --- /dev/null +++ b/ci/url-shortener/ingress.yaml @@ -0,0 +1,21 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: url-shortener-main-webhook +spec: + ingressClassName: haproxy + tls: + - hosts: [tekton-hook.dahyeon.us] + # The existing lolympus Ingress owns issuance for this shared hostname. + secretName: lolympus-main-webhook-tls + rules: + - host: tekton-hook.dahyeon.us + http: + paths: + - path: /url-shortener + pathType: Prefix + backend: + service: + name: el-url-shortener-main + port: + number: 8080 diff --git a/ci/url-shortener/kustomization.yaml b/ci/url-shortener/kustomization.yaml new file mode 100644 index 0000000..3ef7c48 --- /dev/null +++ b/ci/url-shortener/kustomization.yaml @@ -0,0 +1,16 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +namespace: tekton-ci +resources: + - foundation.yaml + - infisical-secret.yaml + - tasks.yaml + - pipeline.yaml + - trigger.yaml + - ingress.yaml +labels: + - includeSelectors: false + pairs: + app.kubernetes.io/name: url-shortener + app.kubernetes.io/part-of: url-shortener-ci + app.kubernetes.io/managed-by: argocd diff --git a/ci/url-shortener/pipeline.yaml b/ci/url-shortener/pipeline.yaml new file mode 100644 index 0000000..f313fec --- /dev/null +++ b/ci/url-shortener/pipeline.yaml @@ -0,0 +1,120 @@ +apiVersion: tekton.dev/v1 +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. + params: + - name: git-revision + type: string + 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: + name: url-shortener-clone + params: + - name: revision + value: $(params.git-revision) + workspaces: + - name: source + workspace: source + - name: credentials + workspace: gitea-credentials + - 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: + - name: revision + value: $(params.git-revision) + 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: image-digest + value: $(tasks.create-image-index.results.image-digest) + workspaces: + - name: source + workspace: source + - name: credentials + workspace: gitea-credentials diff --git a/ci/url-shortener/tasks.yaml b/ci/url-shortener/tasks.yaml new file mode 100644 index 0000000..6774c75 --- /dev/null +++ b/ci/url-shortener/tasks.yaml @@ -0,0 +1,627 @@ +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: url-shortener-clone +spec: + description: Clone and verify an exact non-deletion source revision from Gitea. + params: + - name: revision + type: string + 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/url-shortener.git + - name: REVISION + value: $(params.revision) + - name: SOURCE_DIR + value: $(workspaces.source.path) + - 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' + 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 + 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: url-shortener-quality +spec: + description: Check the Rust API, React admin and Workers before credentials or publication are available. + workspaces: + - name: source + stepTemplate: + image: docker.io/library/node:26.13.0-bookworm@sha256:fa52d1669505e29b58c042875b1d65c229e0787a3610fd52869b22b68e480853 + workingDir: $(workspaces.source.path) + env: + - name: CI + value: "true" + - name: NEW_RELIC_ENABLED + value: "false" + - name: NPM_CONFIG_CACHE + value: $(workspaces.source.path)/.cache/ci/npm + - name: PATH + value: $(workspaces.source.path)/.cache/ci/pnpm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + - name: WRANGLER_SEND_METRICS + value: "false" + - name: WRANGLER_LOG_PATH + value: $(workspaces.source.path)/.cache/wrangler/logs + - name: XDG_CONFIG_HOME + value: $(workspaces.source.path)/.cache/wrangler/config + - name: MONGOMS_DOWNLOAD_DIR + value: $(workspaces.source.path)/.cache/mongodb + steps: + - name: install + computeResources: + requests: + cpu: 200m + memory: 512Mi + limits: + cpu: "2" + memory: 4Gi + script: | + #!/bin/sh + set -eu + umask 0002 + mkdir -p .cache/ci/pnpm .cache/ci/npm + npm install --global --prefix "$(workspaces.source.path)/.cache/ci/pnpm" pnpm@12.16.1 + pnpm install --frozen-lockfile + - name: verify + computeResources: + requests: + cpu: 500m + memory: 1Gi + limits: + cpu: "3" + memory: 6Gi + script: | + #!/bin/sh + set -eu + pnpm check:js + pnpm test:deployment + pnpm format:check:js + - name: verify-rust + image: docker.io/library/rust:1.99.1-trixie + computeResources: + requests: + cpu: 500m + memory: 1Gi + limits: + cpu: "3" + memory: 6Gi + env: + - name: PATH + value: /usr/local/cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + - name: CARGO_HOME + value: /usr/local/cargo + script: | + #!/bin/sh + set -eu + cargo fmt --all -- --check + cargo test --workspace --locked +--- +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: url-shortener-build-platform +spec: + description: Build one app architecture natively using rootless BuildKit and a fresh clone. + params: + - name: revision + type: string + - name: platform + type: string + - name: image + type: string + results: + - name: image-url + description: Published repository and unique tag. + - name: image-digest + description: Immutable platform 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/url-shortener.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 + workingDir: /workspace/build/source + computeResources: + requests: + cpu: "1" + memory: 2Gi + limits: + cpu: "4" + memory: 8Gi + 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: PLATFORM + value: $(params.platform) + - name: IMAGE + value: $(params.image) + - name: METADATA_FILE + value: /workspace/build/build-metadata.json + script: | + #!/bin/sh + set -eu + case "$PLATFORM" in linux/amd64|linux/arm64) ;; *) echo 'Unsupported platform' >&2; exit 1;; esac + printf '%s\n' "$IMAGE" | grep -Eq '^registry\.dahyeon\.us/dahyeon/url-shortener:sha-[0-9a-f]{40}-[a-zA-Z0-9-]+-(amd64|arm64)$' + case "$PLATFORM:$IMAGE" in + linux/amd64:*-amd64|linux/arm64:*-arm64) ;; + *) echo 'Platform tag mismatch' >&2; exit 1;; + esac + buildctl-daemonless.sh build \ + --frontend dockerfile.v0 \ + --local context=. \ + --local dockerfile=. \ + --opt filename=Dockerfile \ + --opt "platform=$PLATFORM" \ + --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: url-shortener-create-image-index +spec: + description: Verify native digests and publish an immutable amd64 plus arm64 OCI image index. + params: + - name: image + type: string + - name: amd64-image + type: string + - name: amd64-digest + type: string + - name: arm64-image + type: string + - name: arm64-digest + type: string + results: + - name: image-url + description: Published multi-platform image tag. + - name: image-digest + description: Immutable multi-platform index digest. + workspaces: + - name: dockerconfig + readOnly: true + volumes: + - name: registry-config + emptyDir: {} + steps: + - name: create-index + image: ghcr.io/regclient/regctl:v0.11.5-alpine@sha256:795e27a968ec0681cf1f4fd4bb83a880a1bc4698afb38efd5c961f9264820565 + computeResources: + requests: + cpu: 50m + memory: 64Mi + limits: + cpu: 500m + memory: 256Mi + securityContext: + runAsNonRoot: true + runAsUser: 1000 + runAsGroup: 1000 + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + volumeMounts: + - name: registry-config + mountPath: /workspace/registry-config + env: + - name: DOCKER_CONFIG + value: $(workspaces.dockerconfig.path) + - name: REGCTL_CONFIG + value: /workspace/registry-config/config.json + - name: FINAL_IMAGE + value: $(params.image) + - name: AMD64_IMAGE + value: $(params.amd64-image) + - name: AMD64_DIGEST + value: $(params.amd64-digest) + - name: ARM64_IMAGE + value: $(params.arm64-image) + - name: ARM64_DIGEST + value: $(params.arm64-digest) + - name: IMAGE_RESULT_PATH + value: $(results.image-url.path) + - name: DIGEST_RESULT_PATH + value: $(results.image-digest.path) + script: | + #!/bin/sh + set -eu + printf '%s\n' "$FINAL_IMAGE" | grep -Eq '^registry\.dahyeon\.us/dahyeon/url-shortener:sha-[0-9a-f]{40}-[a-zA-Z0-9-]+$' + test "$AMD64_IMAGE" = "$FINAL_IMAGE-amd64" + test "$ARM64_IMAGE" = "$FINAL_IMAGE-arm64" + for DIGEST in "$AMD64_DIGEST" "$ARM64_DIGEST"; do + printf '%s\n' "$DIGEST" | grep -Eq '^sha256:[0-9a-f]{64}$' + done + test "$(regctl image digest "$AMD64_IMAGE")" = "$AMD64_DIGEST" + test "$(regctl image digest "$ARM64_IMAGE")" = "$ARM64_DIGEST" + regctl index create "$FINAL_IMAGE" --ref "$AMD64_IMAGE@$AMD64_DIGEST" --desc-platform linux/amd64 + regctl index add "$FINAL_IMAGE" --ref "$ARM64_IMAGE@$ARM64_DIGEST" --desc-platform linux/arm64 + PLATFORMS="$(regctl manifest get "$FINAL_IMAGE" --format '{{range .Manifests}}{{printf "%s/%s\n" .Platform.OS .Platform.Architecture}}{{end}}' | sort)" + test "$PLATFORMS" = "$(printf 'linux/amd64\nlinux/arm64')" + INDEX_DIGEST="$(regctl image digest "$FINAL_IMAGE")" + printf '%s\n' "$INDEX_DIGEST" | grep -Eq '^sha256:[0-9a-f]{64}$' + printf '%s' "$FINAL_IMAGE" >"$IMAGE_RESULT_PATH" + printf '%s' "$INDEX_DIGEST" >"$DIGEST_RESULT_PATH" +--- +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: url-shortener-deploy-cloudflare +spec: + description: Recheck main, migrate D1, deploy the staging Redirect Worker, then build and deploy static admin Pages from the same revision. + params: + - name: revision + type: string + workspaces: + - name: source + - name: credentials + readOnly: true + steps: + - name: deploy + 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: REPO_URL + value: https://git.dahyeon.us/dahyeon/url-shortener.git + - name: REVISION + value: $(params.revision) + - name: GITEA_CREDENTIALS_DIR + value: $(workspaces.credentials.path) + - name: PATH + value: $(workspaces.source.path)/.cache/ci/pnpm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin + - name: WRANGLER_SEND_METRICS + value: "false" + - name: WRANGLER_LOG_PATH + value: $(workspaces.source.path)/.cache/wrangler/logs + - name: XDG_CONFIG_HOME + value: $(workspaces.source.path)/.cache/wrangler/config + envFrom: + - secretRef: + name: url-shortener-cloudflare + - secretRef: + name: url-shortener-edge-staging + script: | + #!/bin/sh + set -eu + printf '%s\n' "$REVISION" | grep -Eq '^[0-9a-f]{40}$' + 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 + test "$(git rev-parse HEAD)" = "$REVISION" + 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 + command -v pnpm >/dev/null + pnpm deploy:cloudflare -- staging + pnpm deploy:pages -- staging +--- +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: url-shortener-update-gitops +spec: + description: Publish the staging app digest and matching D1 identifiers, bootstrapping at least two replicas without reducing a larger configured count. + params: + - name: revision + type: string + - name: image-digest + type: string + 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/url-shortener.git + - name: REVISION + value: $(params.revision) + - 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/url-shortener + - name: IMAGE_DIGEST + value: $(params.image-digest) + - name: DEPLOYMENT_NAME + value: url-shortener + - name: CLOUDFLARE_PROFILE + value: $(workspaces.source.path)/deploy/cloudflare/staging.json + - name: CLOUDFLARE_ACCOUNT_ID + valueFrom: + secretKeyRef: + name: url-shortener-cloudflare + key: CLOUDFLARE_ACCOUNT_ID + script: | + #!/bin/sh + set -eu + 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}$' + test "$(yq -r '.environment' "$CLOUDFLARE_PROFILE")" = staging + 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}$' + test "$CLOUDFLARE_D1_DATABASE_ID" != '00000000-0000-0000-0000-000000000000' + export CLOUDFLARE_D1_DATABASE_ID + MANIFEST="$STATE_DIR/apps/url-shortener/overlays/staging/kustomization.yaml" + CONFIGMAP="$STATE_DIR/apps/url-shortener/overlays/staging/configmap.yaml" + test "$(yq '[.images[] | select(.name == strenv(APP_IMAGE))] | length' "$MANIFEST")" = 1 + test "$(yq '[.replicas[] | select(.name == strenv(DEPLOYMENT_NAME))] | length' "$MANIFEST")" = 1 + REPLICAS="$(yq '.replicas[] | select(.name == strenv(DEPLOYMENT_NAME)) | .count' "$MANIFEST")" + case "$REPLICAS" in ''|*[!0-9]*) echo 'Invalid replica count' >&2; exit 1;; esac + yq -i '(.images[] | select(.name == strenv(APP_IMAGE))) |= (del(.newTag) | .digest = strenv(IMAGE_DIGEST))' "$MANIFEST" + if [ "$REPLICAS" -lt 2 ]; then + yq -i '(.replicas[] | select(.name == strenv(DEPLOYMENT_NAME))).count = 2' "$MANIFEST" + fi + yq -i '.commonAnnotations."url-shortener.dahyeon.us/source-revision" = strenv(REVISION)' "$MANIFEST" + yq -i '.data.CLOUDFLARE_ACCOUNT_ID = strenv(CLOUDFLARE_ACCOUNT_ID) | .data.CLOUDFLARE_D1_DATABASE_ID = strenv(CLOUDFLARE_D1_DATABASE_ID)' "$CONFIGMAP" + - 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 url-shortener-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/url-shortener/overlays/staging/kustomization.yaml apps/url-shortener/overlays/staging/configmap.yaml + git -C "$STATE_DIR" commit -m "chore(url-shortener): deploy staging $REVISION" + 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 + # 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" rev-parse HEAD >"$GITOPS_RESULT_PATH" diff --git a/ci/url-shortener/trigger.yaml b/ci/url-shortener/trigger.yaml new file mode 100644 index 0000000..cc2f4e9 --- /dev/null +++ b/ci/url-shortener/trigger.yaml @@ -0,0 +1,102 @@ +apiVersion: triggers.tekton.dev/v1beta1 +kind: TriggerBinding +metadata: + name: url-shortener-main +spec: + params: + - name: git-revision + value: $(body.after) +--- +apiVersion: triggers.tekton.dev/v1beta1 +kind: TriggerTemplate +metadata: + name: url-shortener-main +spec: + params: + - name: git-revision + resourcetemplates: + - apiVersion: tekton.dev/v1 + kind: PipelineRun + metadata: + generateName: url-shortener-staging- + labels: + app.kubernetes.io/name: url-shortener + app.kubernetes.io/part-of: url-shortener-ci + spec: + pipelineRef: + name: url-shortener-staging + params: + - name: git-revision + value: $(tt.params.git-revision) + taskRunTemplate: + serviceAccountName: url-shortener-pipeline + podTemplate: + nodeSelector: + kubernetes.io/arch: amd64 + securityContext: + fsGroup: 1000 + fsGroupChangePolicy: OnRootMismatch + taskRunSpecs: + - pipelineTaskName: build-arm64 + podTemplate: + nodeSelector: + kubernetes.io/arch: arm64 + securityContext: + fsGroup: 1000 + fsGroupChangePolicy: OnRootMismatch + workspaces: + - name: source + volumeClaimTemplate: + spec: + accessModes: + - ReadWriteOnce + storageClassName: local-path + resources: + requests: + storage: 10Gi + - name: gitea-credentials + secret: + secretName: url-shortener-gitea + items: + - key: username + path: username + - key: password + path: password + - name: harbor-dockerconfig + secret: + secretName: url-shortener-harbor + items: + - key: .dockerconfigjson + path: config.json + timeouts: + pipeline: 60m + tasks: 55m +--- +apiVersion: triggers.tekton.dev/v1beta1 +kind: EventListener +metadata: + name: url-shortener-main +spec: + serviceAccountName: url-shortener-event-listener + triggers: + - name: main-push + interceptors: + - ref: + name: github + params: + - name: secretRef + value: + secretName: url-shortener-webhook + secretKey: secretToken + - name: eventTypes + value: + - push + - ref: + 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' + bindings: + - ref: url-shortener-main + template: + ref: url-shortener-main