variables refactoring; add new methods related to the release deployment

tasks;
This commit is contained in:
Roberto Cirillo 2022-10-10 13:31:52 +02:00
parent d1a687f021
commit 43a402ab16
1 changed files with 96 additions and 28 deletions

124
Jenkinsfile vendored
View File

@ -10,20 +10,20 @@
def agent_root_folder = '/var/lib/jenkins' def agent_root_folder = '/var/lib/jenkins'
if ("${params.report_number}"){ if ("${params.gCube_release_version}"){
// load the build report's content if present //locate the release file
def text String releaseURL = "https://code-repo.d4science.org/gCubeCI/gCubeReleases/raw/branch/master/open/gcube-${gCube_release_version}.yaml"
String reportURL = "https://code-repo.d4science.org/gCubeCI/gCubeReleases/raw/branch/master/open/${gCube_release_version}/build_commits.${params.report_number}.csv" //load the release file
if (reportURL){ def text = releaseURL.toURL().getText()
println "Pulling the report from Git at ${reportURL}" //parsing
text = reportURL.toURL().getText() def jsonConfig = new Yaml().load(text)
// parse the report and extract the data if (verbose)
def components = parseBuildCommits(text) println jsonConfig.inspect()
assert 0 < components.size(): "No component found in build_commits.${report_number}.csv" assert jsonConfig.gCube_release.Version == params.gCube_release_version: "Release versions do not match!"
for (component in components) { echo "Building gCube v. ${jsonConfig.gCube_release.Version}"
// here we need to filter on keywords matching component's section if (verbose) {
println " $component" echo "Found components:"
} jsonConfig.gCube_release.Components.each { println it.key }
} }
} }
@ -52,18 +52,27 @@ pipeline {
environment { environment {
AGENT_ROOT_FOLDER = "${agent_root_folder}" AGENT_ROOT_FOLDER = "${agent_root_folder}"
// folder where all the pending deployment will be added
PENDING_DEPLOY_FOLDER="${agent_root_folder}/CD/" PENDING_DEPLOY_FOLDER="${agent_root_folder}/CD/"
CD_ROOT_FOLDER = "${agent_root_folder}/ansible-repos/ansible-playbooks/d4science-ghn-cluster/CD" ANSIBLE_ROOT_FOLDER = "${agent_root_folder}/ansible-repos/ansible-playbooks/d4science-ghn-cluster/CD"
PIPELINE_BUILD_NUMBER = "${env.BUILD_NUMBER}" PIPELINE_BUILD_NUMBER = "${env.BUILD_NUMBER}"
// deploy file related to the current pipeline job
DEPLOY_FILE = "${PENDING_DEPLOY_FOLDER}deploy.${PIPELINE_BUILD_NUMBER}.csv" DEPLOY_FILE = "${PENDING_DEPLOY_FOLDER}deploy.${PIPELINE_BUILD_NUMBER}.csv"
BACKUP_FILE = "${PENDING_DEPLOY_FOLDER}deploy.${PIPELINE_BUILD_NUMBER}.bck" BACKUP_FILE = "${PENDING_DEPLOY_FOLDER}deploy.${PIPELINE_BUILD_NUMBER}.bck"
// deploy folder that contains all the pending jobs associated to the current pipeline execution
DEPLOY_FOLDER= "${WORKSPACE}/CD-${PIPELINE_BUILD_NUMBER}" DEPLOY_FOLDER= "${WORKSPACE}/CD-${PIPELINE_BUILD_NUMBER}"
// artifacts coordinates
TRIGGER_JOB= "${params.TRIGGER_JOB}" TRIGGER_JOB= "${params.TRIGGER_JOB}"
TRIGGER_VERSION= "${params.TRIGGER_VERSION}" TRIGGER_VERSION= "${params.TRIGGER_VERSION}"
TRIGGER_HOST="${params.TRIGGER_HOST}" TRIGGER_HOST="${params.TRIGGER_HOST}"
// enable the ContinuousDeployment if TRUE
TRIGGER_CD="${params.TRIGGER_CD}" TRIGGER_CD="${params.TRIGGER_CD}"
// Useful to identify the remote YAML file
GCUBE_RELEASE_NUMBER = "${params.gCube_release_version}" GCUBE_RELEASE_NUMBER = "${params.gCube_release_version}"
REPORT_NUMBER = "${params.report_number}" //Category name of the components candidates to the remote deployment
DEPLOY_CATEGORY="${env.DEPLOY_CATEGORY}"
//Job name of the release pipeline
RELEASE_JOB='gCubeBuilder'
} }
@ -110,21 +119,27 @@ pipeline {
anyOf{ anyOf{
allOf{ allOf{
triggeredBy 'TimerTrigger' triggeredBy 'TimerTrigger'
// maybe we can add a new condition in order to consider the manual execution of this pipeline
environment name: 'IS_SCHEDULED_TEST', value: 'True' environment name: 'IS_SCHEDULED_TEST', value: 'True'
} }
// in this case the pipeline is triggered by the gCubeBuilder pipeline
equals(actual: "${params.TRIGGER_JOB}", expected: 'gCubeBuilder') equals(actual: "${params.TRIGGER_JOB}", expected: 'gCubeBuilder')
} }
} }
steps { steps {
echo 'Cron build enabled. Deploy from system ongoing' echo 'Cron build enabled. Deploy from system ongoing'
script { script {
echo "pipeline triggered by ${params.TRIGGER_JOB}" echo "pipeline was triggered by ${params.TRIGGER_JOB}"
if("${params.TRIGGER_JOB}" == "gCubeBuilder"){ if("${params.TRIGGER_JOB}" == "${RELEASE_JOB}"){
// do something println("Going to deploy the Release components")
println("The Job is gCubeBuilder") def releaseList = []
}else{ jsonConfig.gCube_release.Components.each { group_name, component_list ->
if("${group_name}" == "${DEPLOY_CATEGORY}"){
println("found ${it.name} with version: ${it.version}");
releaseList += "${it.name},${it.version}"
}
}
deployJobs(releaseList);
}else{
println("Going to deploy all the pending deployments")
def deployFolder="CD-${env.BUILD_NUMBER}"; def deployFolder="CD-${env.BUILD_NUMBER}";
parseDeployPendingJobs(deployFolder); parseDeployPendingJobs(deployFolder);
} }
@ -143,7 +158,8 @@ pipeline {
} }
steps { steps {
echo 'Do Nothing: cron build disabled' echo 'Going to sleep'
sh ' exit 1; '
} }
} }
stage('New pending deploy ') { stage('New pending deploy ') {
@ -157,7 +173,7 @@ pipeline {
} }
steps { steps {
sh ''' sh '''
echo "Cron build enabled. New deploy of ${TRIGGER_JOB} - ${TRIGGER_VERSION} will be added to the deploy file" echo "Cron build enabled. New deploy of ${TRIGGER_JOB} - ${TRIGGER_VERSION} will be added to the pending deploy file"
touch $DEPLOY_FILE; touch $DEPLOY_FILE;
if grep -q \"\${TRIGGER_JOB}\" \${DEPLOY_FILE}; then if grep -q \"\${TRIGGER_JOB}\" \${DEPLOY_FILE}; then
echo "component ${TRIGGER_JOB} already added. Nothing to add." echo "component ${TRIGGER_JOB} already added. Nothing to add."
@ -245,7 +261,7 @@ pipeline {
def deploy(String service, String version, String host){ def deploy(String service, String version, String host){
def now = new Date(); def now = new Date();
println("Going to deploy the service "+service+" with version: "+version+" on target: "+host); println("Going to deploy the service "+service+" with version: "+version+" on target: "+host);
def statusCode = sh( script: "cd $CD_ROOT_FOLDER;./deployService.sh $service $version $host;", returnStdout: true); def statusCode = sh( script: "cd $ANSIBLE_ROOT_FOLDER;./deployService.sh $service $version $host;", returnStdout: true);
sh(""" sh("""
echo " last exit code \$?"; echo " last exit code \$?";
""") """)
@ -263,6 +279,30 @@ def checkup(String service, String version, String host){
""") """)
} }
//launch ansible deploy
def deployRelease(String service, String version){
def now = new Date();
println("Going to deploy the service "+service+" with version: "+version+" on preproduction ");
def statusCode = sh( script: "cd $ANSIBLE_ROOT_FOLDER;./deployPreprodService.sh $service $version ;", returnStdout: true);
sh("""
echo " last exit code \$?";
""")
println("Deploy ended with status: "+statusCode);
}
//Implement a new method in order to check the input parameters
def checkupRelease(String service, String version){
sh("""
case "$version" in
**SNAPSHOT) echo "version contains SNAPSHOT" ; exit 1;;
**latest) echo "version contains latest" ;;
* ) echo "version without SNAPSHOT. EXIT WITHOUT DEPLOY "; exit 1;;
esac
""")
}
//parse all csv files found in the local folder and deploy the components defined inside //parse all csv files found in the local folder and deploy the components defined inside
def deployPendingJobs( def deployFolder){ def deployPendingJobs( def deployFolder){
println ("searching files in folder ${deployFolder}"); println ("searching files in folder ${deployFolder}");
@ -322,15 +362,43 @@ def deployJobs(def serviceList){
for (def record : serviceList) { for (def record : serviceList) {
println("Processing record: "+record) println("Processing record: "+record)
service=record.split(","); service=record.split(",");
stage(service[0]){ //check on target host
if(service[ 2 ]){
stage(service[0]){
catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') { catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') {
checkup(service[0], service[ 1 ], service[2]); checkup(service[0], service[ 1 ], service[2]);
deploy(service[0], service[ 1 ], service[2]); deploy(service[0], service[ 1 ], service[2]);
} }
} }
}else{
//this is a release build
stage(service[0]){
catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') {
checkupRelease(service[0], service[ 1 ], service[2]);
deployRelease(service[0], service[ 1 ], service[2]);
}
}
}
} }
} }
def parseBuildComponents( def text){
def components = []
"${text}".splitEachLine(',') { columns ->
if (columns[0].startsWith('#') || columns[0].startsWith('GroupID'))
return
components.add([
name : columns[1],
version : columns[2],
gitRepo : columns[3],
commitID: columns[4]
]
)
}
return components
}