feat(argo): enhance ArgoApplicationVersionUpdater to support user-specified repositories and dynamic branch handling

Signed-off-by: zhenyus <zhenyus@mathmast.com>
This commit is contained in:
zhenyus 2025-06-23 15:41:38 +08:00
parent f5eb5a0cee
commit 410bf462a9

View File

@ -13,14 +13,35 @@ class ArgoApplicationVersionUpdater {
def update(environmentSlug, component) { def update(environmentSlug, component) {
steps.log.info("ArgoApplicationVersionUpdater", "[${environmentSlug}] Update Argo application image version to ${steps.env.BUILD_IMAGE_VERSION} for ${component.name}...") steps.log.info("ArgoApplicationVersionUpdater", "[${environmentSlug}] Update Argo application image version to ${steps.env.BUILD_IMAGE_VERSION} for ${component.name}...")
steps.log.info("ArgoApplicationVersionUpdater", "[${environmentSlug}] Pull freeleaps-ops repository to workspace...")
def userSpecifiedArgoControlledRepo = false
def argoControlledRepo = configurations.argoControlledRepo
if (argoControlledRepo != null && !argoControlledRepo.isEmpty()) {
userSpecifiedArgoControlledRepo = true
}
def repoUrl = "https://gitea.freeleaps.mathmast.com/freeleaps/freeleaps-ops.git"
def repoCredentialsId = "freeleaps-ops-credentials"
def repoBranch = "master"
def valuesFile = "./${configurations.serviceName}/helm-pkg/${component.name}/values.${environmentSlug}.yaml"
if (userSpecifiedArgoControlledRepo) {
repoUrl = component.argoControlledRepo
repoCredentialsId = component.argoControlledRepoCredentialsId
repoBranch = component.argoControlledRepoBranch
valuesFile = ".freeleaps/devops/helm-pkg/values.${environmentSlug}.yaml"
steps.log.info("ArgoApplicationVersionUpdater", "[${environmentSlug}] User specified argo controlled repo: ${argoControlledRepo}")
} else {
steps.log.info("ArgoApplicationVersionUpdater", "[${environmentSlug}] Using default argo controlled repo: freeleaps-ops")
}
steps.log.info("ArgoApplicationVersionUpdater", "[${environmentSlug}] Pull argo controlled repo from ${repoUrl} to workspace...")
steps.dir("${workspace}") { steps.dir("${workspace}") {
steps.git branch: 'master', credentialsId: 'freeleaps-ops-credentials', url: 'https://gitea.freeleaps.mathmast.com/freeleaps/freeleaps-ops.git' steps.git branch: repoBranch, credentialsId: repoCredentialsId, url: repoUrl
} }
steps.dir("${workspace}") { steps.dir("${workspace}") {
def valuesFile = "./${configurations.serviceName}/helm-pkg/${component.name}/values.${environmentSlug}.yaml"
steps.log.info("ArgoApplicationVersionUpdater", "[${environmentSlug}] Update ${valuesFile}...") steps.log.info("ArgoApplicationVersionUpdater", "[${environmentSlug}] Update ${valuesFile}...")
def data = steps.readYaml(file: valuesFile) def data = steps.readYaml(file: valuesFile)
data[component.name].image.registry = steps.env.BUILD_IMAGE_REGISTRY data[component.name].image.registry = steps.env.BUILD_IMAGE_REGISTRY
@ -29,7 +50,13 @@ class ArgoApplicationVersionUpdater {
data[component.name].image.name = steps.env.BUILD_IMAGE_NAME data[component.name].image.name = steps.env.BUILD_IMAGE_NAME
steps.writeYaml(file: valuesFile, data: data, overwrite: true) steps.writeYaml(file: valuesFile, data: data, overwrite: true)
steps.withCredentials([steps.usernamePassword(credentialsId: 'freeleaps-ops-credentials', passwordVariable: 'OPS_GIT_PASSWORD', usernameVariable: 'OPS_GIT_USERNAME')]) { steps.withCredentials([steps.usernamePassword(credentialsId: repoCredentialsId, passwordVariable: 'OPS_GIT_PASSWORD', usernameVariable: 'OPS_GIT_USERNAME')]) {
ciOriginUrl = "https://${steps.env.OPS_GIT_USERNAME}:${steps.env.OPS_GIT_PASSWORD}@gitea.freeleaps.mathmast.com/freeleaps/freeleaps-ops.git"
if (userSpecifiedArgoControlledRepo) {
// argoControlledRepo is a git url, so we need add username and password to the url
def argoControlledRepoUrlObj = new URL(component.argoControlledRepo)
ciOriginUrl = "${argoControlledRepoUrlObj.protocol}://${steps.env.OPS_GIT_USERNAME}:${steps.env.OPS_GIT_PASSWORD}@${argoControlledRepoUrlObj.host}${argoControlledRepoUrlObj.path}"
}
steps.sh """ steps.sh """
#!/bin/bash #!/bin/bash
@ -41,7 +68,7 @@ class ArgoApplicationVersionUpdater {
git config user.email "gitops@mathmast.com" git config user.email "gitops@mathmast.com"
echo "Add and commit changes..." echo "Add and commit changes..."
git remote add ci_origin https://${steps.env.OPS_GIT_USERNAME}:${steps.env.OPS_GIT_PASSWORD}@gitea.freeleaps.mathmast.com/freeleaps/freeleaps-ops.git git remote add ci_origin ${ciOriginUrl}
git add ${valuesFile} git add ${valuesFile}
@ -53,7 +80,7 @@ class ArgoApplicationVersionUpdater {
fi fi
echo "Push changes to freeleaps-ops repository..." echo "Push changes to freeleaps-ops repository..."
git push ci_origin HEAD:master git push ci_origin HEAD:${repoBranch}
echo "Done." echo "Done."
""" """