2025-04-25 19:22:03 +00:00

91 lines
2.6 KiB
YAML

apiVersion: tekton.dev/v1
kind: Task
metadata:
name: git-replace-multi
annotations:
tekton.dev/pipelines.minVersion: "0.19.0"
tekton.dev/categories: GitOps
tekton.dev/tags: git, devops
tekton.dev/displayName: "replace multiple files/dirs in git repository"
tekton.dev/platforms: "linux/amd64"
spec:
description: |
Replaces multiple files or directories in a Git repository, committing and pushing the changes.
params:
- name: repositoryUrl
type: string
description: Source repository URL
- name: branch
type: string
default: main
description: Git branch to push to
- name: targetPaths
type: array
description: List of target paths in the repo (file or directory, e.g. dir/ or file)
- name: sourcePaths
type: array
description: List of source paths in the workspace (file or directory, e.g. dir/ or file)
- name: commitMessage
type: string
default: "chore(gitops): replace multiple files or dirs"
description: Commit message
workspaces:
- name: base
- name: tmp
steps:
- name: clone-repository
image: alpine/git:latest
script: |
#!/bin/sh
set -e
REPO_URL="$(params.repositoryUrl)"
BRANCH="$(params.branch)"
WORKSPACE_PATH="$(workspaces.tmp.path)"
git clone --branch "${BRANCH}" "${REPO_URL}" "${WORKSPACE_PATH}/repo"
cd "${WORKSPACE_PATH}/repo"
- name: sync-files
image: alpine:3.18
script: |
#!/bin/sh
set -e
TARGET_PATHS=($(params.targetPaths[*]))
SOURCE_PATHS=($(params.sourcePaths[*]))
BASE_PATH="$(workspaces.base.path)/source"
REPO_PATH="$(workspaces.tmp.path)/repo"
COUNT=${#TARGET_PATHS[@]}
if [ $COUNT -ne ${#SOURCE_PATHS[@]} ]; then
echo "targetPaths와 sourcePaths의 길이가 다릅니다."
exit 1
fi
for i in $(seq 0 $(($COUNT - 1))); do
TARGET="${REPO_PATH}/${TARGET_PATHS[$i]}"
SOURCE="${BASE_PATH}/${SOURCE_PATHS[$i]}"
[ -e "${TARGET}" ] && rm -rf "${TARGET}" || true
mkdir -p $(dirname "${TARGET}")
cp -r "${SOURCE}" "${TARGET}"
done
- name: commit-and-push
image: alpine/git:latest
script: |
#!/bin/sh
set -e
cd "$(workspaces.tmp.path)/repo"
git config --global user.name "tekton-bot"
git config --global user.email "tekton@example.com"
git add .
git commit -m "$(params.commitMessage)" || exit 0
git push origin HEAD:"$(params.branch)"
volumes:
- name: tmp
emptyDir: {}