From 00f1e7b86948d9aaf3ff04adde18ce0a0daeb8d0 Mon Sep 17 00:00:00 2001 From: Roberto Cirillo Date: Thu, 28 Apr 2022 10:27:36 +0200 Subject: [PATCH] add cleanup deploy file --- Jenkinsfile | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index ec515d6..3092ad6 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -10,8 +10,10 @@ def agent_root_folder = '/var/lib/jenkins' def agent_deploy_filename = 'deploy.csv' +def agent_deploy_backup_filename = 'deploy.bck' def deployList +def backupList if (params.deployFile) { println "Using custom deploy file" deployList = params.deployFile @@ -21,6 +23,7 @@ if (params.deployFile) { deployList = agent_root_folder+'/'+agent_deploy_filename; println "Load from local file ${deployList}" } +backupList = agent_root_folder+'/'+agent_deploy_backup_filename; pipeline { agent { @@ -40,6 +43,7 @@ pipeline { environment { AGENT_ROOT_FOLDER = "${agent_root_folder}" DEPLOY_FILE = "${agent_root_folder}/${agent_deploy_filename}" + BACKUP_FILE = "${agent_root_folder}/${agent_deploy_backup_filename}" TRIGGER_JOB= "${params.TRIGGER_JOB}" TRIGGER_VERSION= "${params.TRIGGER_VERSION}" } @@ -69,8 +73,16 @@ pipeline { for (component in components) { println "Deploy on going of component: $component" } - new File(${DEPLOY_FILE}).delete(); + } + sh ''' + echo "cleanup $DEPLOY_FILE" + if [ -f ${DEPLOY_BACKUP ]; then + echo "backup found: $DEPLOY_BACKUP going to replace it" + rm ${DEPLOY_BACKUP} + mv $DEPLOY_FILE $DEPLOY_BACKUP + touch $DEPLOY_FILE + ''' } } stage('Nothing to do by System ') { @@ -95,6 +107,7 @@ pipeline { steps { sh ''' echo "Cron build enabled. New deploy of ${TRIGGER_JOB} - ${TRIGGER_VERSION} will be added to the deploy file" + if grep -q \"\${TRIGGER_JOB}\" \${DEPLOY_FILE}; then echo "component ${TRIGGER_JOB} already added. Nothing to add." else @@ -173,3 +186,17 @@ def appendToFile(String fileName, String line) { writeFile file: fileName, text: current + "\n" + line } +/** + * Appends the footer to the deploy file. + */ +def appendFooter( def File) { + def now = new Date() + sh(""" + echo "" >> $File + echo "---" >> $File + echo "*generated by the gCubeDeploy pipeline >> $File + echo "" >> $File + echo "*last update $now*" >> $File + """) +} +