diff --git a/tasks/gitops-repository/task.yaml b/tasks/gitops-repository/task.yaml index 9c16a45..76fd4a3 100644 --- a/tasks/gitops-repository/task.yaml +++ b/tasks/gitops-repository/task.yaml @@ -2,101 +2,98 @@ apiVersion: tekton.dev/v1beta1 kind: Task metadata: name: gitops-repository + annotations: + tekton.dev/pipelines.minVersion: "0.19.0" + tekton.dev/categories: GitOps + tekton.dev/tags: git, helm, devops + tekton.dev/displayName: "Update image tag in Helm values.yaml" + tekton.dev/platforms: "linux/amd64" spec: + description: | + Updates the image.tag field in a Helm values.yaml file and commits the change + to the corresponding GitOps repository derived from the application source repo. + params: - name: repositoryUrl type: string - description: | - Source repository URL (used to derive GitOps repo) - (e.g. git@github.com:org/app.git) - (e.g. https://github.com/org/app.git) + description: Source repository URL (used to derive GitOps repo) - name: branch type: string default: main - description: Branch to push to + description: Git branch to push to - name: imageUrl type: string - description: Full image URL (e.g. registry.com/app:v0.2.0) + description: Full image URL (e.g. registry/app:v0.2.0) - - name: kustomizationPath + - name: valuesPath type: string - default: overlays/staging/kustomization.yaml - description: Relative path to file to update + description: Path to Helm values file (e.g. overlays/staging/values-staging.yaml) - name: commitMessage type: string default: "chore(gitops): update image tag" - description: Commit message to use + description: Commit message workspaces: - name: ssh-directory optional: true - description: | - A .ssh directory with private key, known_hosts, config, etc. - Copied to the user's home before git commands are executed. + description: SSH credentials (private key, known_hosts) - name: basic-auth optional: true - description: | - A Workspace containing a .gitconfig and .git-credentials file. + description: .gitconfig and .git-credentials - name: ssl-ca-directory optional: true - description: | - A workspace containing CA certificates, used by Git for SSL verification. + description: Custom CA certificates (optional) steps: - - name: clone-update-push - image: alpine/git - env: - - name: HOME - value: /tekton/home + - name: update-and-push + image: alpine:3.19 + workingDir: /workspace script: | #!/bin/sh set -e - echo "πŸ” Git 인증 μ„€μ • 쀑..." - mkdir -p /tekton/home + echo "πŸ” Preparing Git authentication..." + mkdir -p /root/.ssh if [ -d /workspace/ssh-directory ]; then - mkdir -p /tekton/home/.ssh - cp -R /workspace/ssh-directory/* /tekton/home/.ssh/ - chmod 700 /tekton/home/.ssh + cp -R /workspace/ssh-directory/* /root/.ssh/ + chmod 700 /root/.ssh fi if [ -d /workspace/basic-auth ]; then - cp /workspace/basic-auth/.gitconfig /tekton/home/.gitconfig || true - cp /workspace/basic-auth/.git-credentials /tekton/home/.git-credentials || true + cp /workspace/basic-auth/.gitconfig /root/.gitconfig || true + cp /workspace/basic-auth/.git-credentials /root/.git-credentials || true fi if [ -d /workspace/ssl-ca-directory ]; then export GIT_SSL_CAINFO="/workspace/ssl-ca-directory/ca.crt" fi - echo "πŸ”§ GitOps μ €μž₯μ†Œ URL μžλ™ λ³€ν™˜" - SOURCE_REPO="$(params.repositoryUrl)" - GITOPS_REPO=$(echo "$SOURCE_REPO" | sed 's/\.git$/-ops.git/') - echo "🧩 Cloning GitOps repo: $GITOPS_REPO" + echo "πŸ“¦ Installing Git + yq..." + apk add --no-cache git yq openssh - TMP_DIR="/tmp/gitops" - rm -rf "$TMP_DIR" - git clone --branch "$(params.branch)" "$GITOPS_REPO" "$TMP_DIR" + REPO_URL="$(params.repositoryUrl)" + GITOPS_REPO=$(echo "$REPO_URL" | sed 's/\.git$/-ops.git/') + BRANCH="$(params.branch)" + VALUES_PATH="$(params.valuesPath)" + IMAGE="$(params.imageUrl)" + TAG=$(echo "$IMAGE" | cut -d: -f2) + + echo "πŸ“₯ Cloning $GITOPS_REPO..." + TMP_DIR=$(mktemp -d) + git clone --branch "$BRANCH" "$GITOPS_REPO" "$TMP_DIR" cd "$TMP_DIR" - echo "πŸ” Updating image tag in: $(params.kustomizationPath)" - IMAGE_FULL="$(params.imageUrl)" - NAME=$(echo "$IMAGE_FULL" | cut -d: -f1) - TAG=$(echo "$IMAGE_FULL" | cut -d: -f2) - sed -i "s|\(name: $NAME\s*newTag: \).*|\1$TAG|" "$(params.kustomizationPath)" || { - echo "❌ Failed to patch tag" - exit 1 - } + echo "πŸ›  Updating image.tag in $VALUES_PATH to $TAG" + yq e ".image.tag = \"$TAG\"" -i "$VALUES_PATH" - echo "βœ… Committing & pushing changes" git config user.name "tekton-ci" git config user.email "ci@example.com" - git add "$(params.kustomizationPath)" + git add "$VALUES_PATH" git commit -m "$(params.commitMessage)" || echo "No changes to commit." - git push origin "$(params.branch)" + git push origin "$BRANCH" - echo "🧹 Cleaning up" - rm -rf "$TMP_DIR" \ No newline at end of file + echo "🧹 Cleaning up..." + rm -rf "$TMP_DIR"