diff --git a/ci/lolympus-tournament-support/pipeline.yaml b/ci/lolympus-tournament-support/pipeline.yaml index cdfb86d..ac602da 100644 --- a/ci/lolympus-tournament-support/pipeline.yaml +++ b/ci/lolympus-tournament-support/pipeline.yaml @@ -11,8 +11,8 @@ spec: type: string results: - name: image-digest - description: Immutable API image digest. - value: $(tasks.build-api.results.image-digest) + description: Immutable multi-platform API image index digest. + value: $(tasks.create-image-index.results.image-digest) - name: gitops-commit description: service-state commit applied to staging. value: $(tasks.update-gitops.results.gitops-commit) @@ -40,22 +40,63 @@ spec: workspaces: - name: source workspace: source - - name: build-api + - name: build-api-amd64 runAfter: - quality taskRef: - name: lolympus-build-image + name: lolympus-build-platform + params: + - name: revision + value: $(params.git-revision) + - name: platform + value: linux/amd64 + - name: image + value: registry.dahyeon.us/lolympus/lolympus-tournament-support:$(params.image-tag)-$(context.pipelineRun.uid)-amd64 + workspaces: + - name: credentials + workspace: gitea-credentials + - name: dockerconfig + workspace: harbor-dockerconfig + - name: build-api-arm64 + runAfter: + - quality + taskRef: + name: lolympus-build-platform + params: + - name: revision + value: $(params.git-revision) + - name: platform + value: linux/arm64 + - name: image + value: registry.dahyeon.us/lolympus/lolympus-tournament-support:$(params.image-tag)-$(context.pipelineRun.uid)-arm64 + workspaces: + - name: credentials + workspace: gitea-credentials + - name: dockerconfig + workspace: harbor-dockerconfig + - name: create-image-index + runAfter: + - build-api-amd64 + - build-api-arm64 + taskRef: + name: lolympus-create-image-index params: - name: image - value: registry.dahyeon.us/lolympus/lolympus-tournament-support:$(params.image-tag) + value: registry.dahyeon.us/lolympus/lolympus-tournament-support:$(params.image-tag)-$(context.pipelineRun.uid) + - name: amd64-image + value: $(tasks.build-api-amd64.results.image-url) + - name: amd64-digest + value: $(tasks.build-api-amd64.results.image-digest) + - name: arm64-image + value: $(tasks.build-api-arm64.results.image-url) + - name: arm64-digest + value: $(tasks.build-api-arm64.results.image-digest) workspaces: - - name: source - workspace: source - name: dockerconfig workspace: harbor-dockerconfig - name: assert-latest runAfter: - - build-api + - create-image-index taskRef: name: lolympus-assert-latest params: @@ -80,8 +121,8 @@ spec: params: - name: revision value: $(params.git-revision) - - name: image-tag - value: $(params.image-tag) + - name: image-digest + value: $(tasks.create-image-index.results.image-digest) workspaces: - name: source workspace: source diff --git a/ci/lolympus-tournament-support/tasks.yaml b/ci/lolympus-tournament-support/tasks.yaml index 036f9ed..e050014 100644 --- a/ci/lolympus-tournament-support/tasks.yaml +++ b/ci/lolympus-tournament-support/tasks.yaml @@ -116,10 +116,14 @@ spec: apiVersion: tekton.dev/v1 kind: Task metadata: - name: lolympus-build-image + name: lolympus-build-platform spec: - description: Build and push the API image with rootless BuildKit. + description: Clone an exact revision and build one native API platform with rootless BuildKit. params: + - name: revision + type: string + - name: platform + type: string - name: image type: string results: @@ -128,16 +132,64 @@ spec: - name: image-digest description: Immutable digest returned by the registry. workspaces: - - name: source + - 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-source + env: + - name: REPO_URL + value: https://git.dahyeon.us/dahyeon/lolympus-tournament-support.git + script: | + #!/bin/sh + set -eu + SOURCE_DIR=/workspace/build-source + REVISION="$(params.revision)" + AUTH_HOME="$(mktemp -d)" + trap 'rm -rf "$AUTH_HOME"' EXIT + USERNAME="$(cat "$(workspaces.credentials.path)/username")" + PASSWORD="$(cat "$(workspaces.credentials.path)/password")" + cat >"$AUTH_HOME/.netrc" <&2 + exit 1 + fi - name: build-and-push image: docker.io/moby/buildkit:v0.31.1-rootless@sha256:946cf909534b789c9b84af75e6d4f8cb23c3b9cb387daec5ba0f4878a3e4647c - workingDir: $(workspaces.source.path) + workingDir: /workspace/build-source computeResources: requests: {cpu: "1", memory: 2Gi} limits: {cpu: "4", memory: 8Gi} @@ -151,7 +203,11 @@ spec: type: Unconfined appArmorProfile: type: Unconfined + workspaces: + - name: dockerconfig volumeMounts: + - name: build-source + mountPath: /workspace/build-source - name: buildkit-state mountPath: /home/user/.local/share/buildkit env: @@ -162,19 +218,38 @@ spec: script: | #!/bin/sh set -eu + case "$(params.platform)" in + linux/amd64|linux/arm64) ;; + *) + echo "Unsupported platform: $(params.platform)" >&2 + exit 1 + ;; + esac buildctl-daemonless.sh build \ --frontend dockerfile.v0 \ --local context=. \ --local dockerfile=. \ --opt filename=Dockerfile \ + --opt platform=$(params.platform) \ --output "type=image,name=$(params.image),push=true" \ --metadata-file "$(results.image-digest.path)" - name: record-digest image: docker.io/mikefarah/yq:4.47.2@sha256:76def1f56f456ecc1c3173ea275218ee17139bc2018c5a07887b15afd88ec03e - workingDir: $(workspaces.source.path) + workingDir: /workspace/build-source 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-source script: | #!/bin/sh set -eu @@ -188,6 +263,114 @@ spec: --- apiVersion: tekton.dev/v1 kind: Task +metadata: + name: lolympus-create-image-index +spec: + description: Publish an OCI image index from the native amd64 and arm64 images. + 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: Pushed multi-platform image repository and tag. + - name: image-digest + description: Immutable OCI image index digest returned by the registry. + workspaces: + - name: dockerconfig + readOnly: true + volumes: + - name: regctl-home + 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: regctl-home + mountPath: /home/appuser + env: + - name: HOME + value: /home/appuser + - 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) + script: | + #!/bin/sh + set -eu + mkdir -p "$HOME/.docker" + cp "$(workspaces.dockerconfig.path)/config.json" "$HOME/.docker/config.json" + chmod 0600 "$HOME/.docker/config.json" + + for DIGEST in "$AMD64_DIGEST" "$ARM64_DIGEST"; do + if ! printf '%s\n' "$DIGEST" | grep -Eq '^sha256:[0-9a-f]{64}$'; then + echo "Invalid image digest: $DIGEST" >&2 + exit 1 + fi + done + + RESOLVED_AMD64="$(regctl image digest "$AMD64_IMAGE")" + RESOLVED_ARM64="$(regctl image digest "$ARM64_IMAGE")" + if [ "$RESOLVED_AMD64" != "$AMD64_DIGEST" ]; then + echo "amd64 tag resolved to $RESOLVED_AMD64, expected $AMD64_DIGEST" >&2 + exit 1 + fi + if [ "$RESOLVED_ARM64" != "$ARM64_DIGEST" ]; then + echo "arm64 tag resolved to $RESOLVED_ARM64, expected $ARM64_DIGEST" >&2 + exit 1 + fi + + 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)" + EXPECTED_PLATFORMS="$(printf 'linux/amd64\nlinux/arm64')" + if [ "$PLATFORMS" != "$EXPECTED_PLATFORMS" ]; then + echo "Unexpected image index platforms:" >&2 + printf '%s\n' "$PLATFORMS" >&2 + exit 1 + fi + + INDEX_DIGEST="$(regctl image digest "$FINAL_IMAGE")" + if ! printf '%s\n' "$INDEX_DIGEST" | grep -Eq '^sha256:[0-9a-f]{64}$'; then + echo "Invalid image index digest: $INDEX_DIGEST" >&2 + exit 1 + fi + printf '%s' "$FINAL_IMAGE" >"$(results.image-url.path)" + printf '%s' "$INDEX_DIGEST" >"$(results.image-digest.path)" +--- +apiVersion: tekton.dev/v1 +kind: Task metadata: name: lolympus-assert-latest spec: @@ -271,11 +454,11 @@ kind: Task metadata: name: lolympus-update-gitops spec: - description: Update the staging image tag in service-state. + description: Update the staging image index digest in service-state. params: - name: revision type: string - - name: image-tag + - name: image-digest type: string results: - name: gitops-commit @@ -330,11 +513,15 @@ spec: env: - name: IMAGE_NAME value: registry.dahyeon.us/lolympus/lolympus-tournament-support - - name: IMAGE_TAG - value: $(params.image-tag) + - name: IMAGE_DIGEST + value: $(params.image-digest) script: | #!/bin/sh set -eu + if ! printf '%s\n' "$IMAGE_DIGEST" | grep -Eq '^sha256:[0-9a-f]{64}$'; then + echo "Invalid image index digest: $IMAGE_DIGEST" >&2 + exit 1 + fi MANIFEST="$(workspaces.source.path)/.gitops/apps/lolympus-tournament-support/overlays/staging/kustomization.yaml" MATCHES="$(yq '[.images[] | select(.name == strenv(IMAGE_NAME))] | length' "$MANIFEST")" if [ "$MATCHES" != "1" ]; then @@ -342,7 +529,7 @@ spec: exit 1 fi yq -i \ - '(.images[] | select(.name == strenv(IMAGE_NAME))).newTag = strenv(IMAGE_TAG)' \ + '(.images[] | select(.name == strenv(IMAGE_NAME))) |= (del(.newTag) | .digest = strenv(IMAGE_DIGEST))' \ "$MANIFEST" - name: commit-and-push image: docker.io/alpine/git:2.49.1@sha256:c0280cf9572316299b08544065d3bf35db65043d5e3963982ec50647d2746e26 @@ -377,7 +564,7 @@ spec: fi git -C "$STATE_DIR" add apps/lolympus-tournament-support/overlays/staging/kustomization.yaml git -C "$STATE_DIR" commit \ - -m "chore(lolympus): deploy staging $(params.image-tag)" + -m "chore(lolympus): deploy staging $(params.image-digest)" git -C "$STATE_DIR" pull --rebase origin main git -C "$STATE_DIR" push origin HEAD:main git -C "$STATE_DIR" rev-parse HEAD >"$(results.gitops-commit.path)" diff --git a/ci/lolympus-tournament-support/trigger.yaml b/ci/lolympus-tournament-support/trigger.yaml index b9ab4b8..1b8f7d8 100644 --- a/ci/lolympus-tournament-support/trigger.yaml +++ b/ci/lolympus-tournament-support/trigger.yaml @@ -36,6 +36,15 @@ spec: securityContext: fsGroup: 1000 fsGroupChangePolicy: OnRootMismatch + taskRunSpecs: + - pipelineTaskName: build-api-amd64 + podTemplate: + nodeSelector: + kubernetes.io/arch: amd64 + - pipelineTaskName: build-api-arm64 + podTemplate: + nodeSelector: + kubernetes.io/arch: arm64 workspaces: - name: source volumeClaimTemplate: