50 lines
2.1 KiB
Groovy
50 lines
2.1 KiB
Groovy
// CI yahan khatam: image ka naya tag gitops repo me commit ho jaata hai.
|
|
// Aage ka kaam ArgoCD ka hai - wahi cluster ko git wali state par le jaata hai.
|
|
// Jenkins ke paas cluster ka koi access nahi chahiye. Yahi GitOps ka asli fayda.
|
|
def call(Map cfg) {
|
|
withCredentials([usernamePassword(credentialsId: 'gitea-user',
|
|
usernameVariable: 'GIT_USER',
|
|
passwordVariable: 'GIT_PASS')]) {
|
|
withEnv(["GITOPS_REPO=${cfg.gitopsRepo}", "GITOPS_PATH=${cfg.gitopsPath}"]) {
|
|
sh '''
|
|
set +x
|
|
WORK=$(mktemp -d); trap "rm -rf $WORK" EXIT
|
|
AUTH_URL=$(printf '%s' "$GITOPS_REPO" | sed "s|https://|https://$GIT_USER:$GIT_PASS@|")
|
|
git clone -q --depth 1 "$AUTH_URL" "$WORK/repo"
|
|
cd "$WORK/repo"
|
|
|
|
python3 - "$GITOPS_PATH" "$REGISTRY/$IMAGE_NAME" "$IMAGE_TAG" <<'PY'
|
|
import sys, pathlib
|
|
path, image, tag = sys.argv[1], sys.argv[2], sys.argv[3]
|
|
changed = []
|
|
for f in sorted(pathlib.Path(path).rglob("*.yaml")):
|
|
lines = f.read_text().splitlines(True)
|
|
out, hit = [], False
|
|
for ln in lines:
|
|
if ln.strip().startswith("image:") and (image + ":") in ln:
|
|
indent = ln[: len(ln) - len(ln.lstrip())]
|
|
out.append(indent + "image: " + image + ":" + tag + "\n")
|
|
hit = True
|
|
else:
|
|
out.append(ln)
|
|
if hit:
|
|
f.write_text("".join(out))
|
|
changed.append(str(f))
|
|
print(" updated:", ", ".join(changed) if changed else "kuch nahi mila")
|
|
PY
|
|
|
|
git config user.email "jenkins@platform.local"
|
|
git config user.name "jenkins"
|
|
git add -A
|
|
if git diff --cached --quiet; then
|
|
echo " tag pehle se wahi hai, commit ki zarurat nahi"
|
|
exit 0
|
|
fi
|
|
git commit -q -m "$MODULE: image -> $IMAGE_TAG (jenkins build $BUILD_NUMBER)"
|
|
git push -q "$AUTH_URL" HEAD:main
|
|
echo " gitops repo commit ho gaya - ArgoCD sync karega"
|
|
'''
|
|
}
|
|
}
|
|
}
|