2025-02-17 10:14:40 +00:00
package com . freeleaps . devops
class ArgoApplicationVersionUpdater {
def steps
def workspace
2025-02-17 14:13:52 +00:00
def configurations
2025-02-17 10:14:40 +00:00
2025-02-17 14:13:52 +00:00
ArgoApplicationVersionUpdater ( steps , configurations ) {
2025-02-17 10:14:40 +00:00
this . steps = steps
2025-02-17 14:13:52 +00:00
this . configurations = configurations
2025-02-17 13:01:03 +00:00
this . workspace = steps . env . WORKSPACE
2025-02-17 10:14:40 +00:00
}
def update ( environmentSlug , component ) {
2025-02-17 14:13:52 +00:00
steps . log . info ( "ArgoApplicationVersionUpdater" , "[${environmentSlug}] Update Argo application image version to ${steps.env.BUILD_IMAGE_VERSION} for ${component.name}..." )
2025-06-23 07:41:38 +00:00
def userSpecifiedArgoControlledRepo = false
2025-06-23 08:03:13 +00:00
def argoControlledRepo = component . argoControlledRepo
2025-06-23 07:41:38 +00:00
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..." )
2025-02-17 10:14:40 +00:00
2025-02-17 17:31:47 +00:00
steps . dir ( "${workspace}" ) {
2025-06-23 07:41:38 +00:00
steps . git branch: repoBranch , credentialsId: repoCredentialsId , url: repoUrl
2025-02-17 17:31:47 +00:00
}
steps . dir ( "${workspace}" ) {
2025-03-06 17:31:58 +00:00
steps . log . info ( "ArgoApplicationVersionUpdater" , "[${environmentSlug}] Update ${valuesFile}..." )
2025-02-17 18:33:12 +00:00
def data = steps . readYaml ( file: valuesFile )
2025-02-17 18:18:04 +00:00
data [ component . name ] . image . registry = steps . env . BUILD_IMAGE_REGISTRY
data [ component . name ] . image . repository = steps . env . BUILD_IMAGE_REPOSITORY
data [ component . name ] . image . tag = steps . env . BUILD_IMAGE_VERSION
data [ component . name ] . image . name = steps . env . BUILD_IMAGE_NAME
2025-02-17 18:54:19 +00:00
steps . writeYaml ( file: valuesFile , data: data , overwrite: true )
2025-02-17 17:31:47 +00:00
2025-06-23 07:41:38 +00:00
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}"
}
2025-02-17 17:31:47 +00:00
steps . sh "" "
2025-03-06 09:42:35 +00:00
# ! /bin/ bash
2025-02-17 19:25:33 +00:00
echo "Set ${workspace} as a safe directory..."
git config - - global - - add safe . directory $ { workspace }
2025-03-06 09:18:12 +00:00
2025-02-17 19:15:14 +00:00
echo "Configure git user..."
2025-02-17 17:31:47 +00:00
git config user . name "freeleaps-gitops-bot"
git config user . email "gitops@mathmast.com"
2025-03-06 09:18:12 +00:00
2025-02-17 19:15:14 +00:00
echo "Add and commit changes..."
2025-06-23 07:41:38 +00:00
git remote add ci_origin $ { ciOriginUrl }
2025-03-06 09:18:12 +00:00
2025-02-17 17:31:47 +00:00
git add $ { valuesFile }
2025-03-06 09:18:12 +00:00
if ! git diff - - cached - - quiet ; then
git commit - m "ci(bump): bump ${component.name} image version for ${environmentSlug} to ${steps.env.BUILD_IMAGE_VERSION}"
echo "Detected changes, commit created."
else
echo "No changes detected, skipping commit."
fi
2025-02-17 19:15:14 +00:00
echo "Push changes to freeleaps-ops repository..."
2025-06-23 07:41:38 +00:00
git push ci_origin HEAD: $ { repoBranch }
2025-03-06 09:18:12 +00:00
2025-02-17 19:15:14 +00:00
echo "Done."
2025-02-17 17:31:47 +00:00
"" "
steps . log . info ( "ArgoApplicationVersionUpdater" , "[${environmentSlug}] ${component.name} image version bump to ${steps.env.BUILD_IMAGE_VERSION}" )
}
2025-02-17 10:14:40 +00:00
}
}
}