diff --git a/Jenkinsfile b/Jenkinsfile index 23b977a..2b681ab 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -10,20 +10,20 @@ def agent_root_folder = '/var/lib/jenkins' -if ("${params.report_number}"){ - // load the build report's content if present - def text - String reportURL = "https://code-repo.d4science.org/gCubeCI/gCubeReleases/raw/branch/master/open/${gCube_release_version}/build_commits.${params.report_number}.csv" - if (reportURL){ - println "Pulling the report from Git at ${reportURL}" - text = reportURL.toURL().getText() - // parse the report and extract the data - def components = parseBuildCommits(text) - assert 0 < components.size(): "No component found in build_commits.${report_number}.csv" - for (component in components) { - // here we need to filter on keywords matching component's section - println " $component" - } +if ("${params.gCube_release_version}"){ +//locate the release file + String releaseURL = "https://code-repo.d4science.org/gCubeCI/gCubeReleases/raw/branch/master/open/gcube-${gCube_release_version}.yaml" + //load the release file + def text = releaseURL.toURL().getText() + //parsing + def jsonConfig = new Yaml().load(text) + if (verbose) + println jsonConfig.inspect() + assert jsonConfig.gCube_release.Version == params.gCube_release_version: "Release versions do not match!" + echo "Building gCube v. ${jsonConfig.gCube_release.Version}" + if (verbose) { + echo "Found components:" + jsonConfig.gCube_release.Components.each { println it.key } } } @@ -52,18 +52,27 @@ pipeline { environment { AGENT_ROOT_FOLDER = "${agent_root_folder}" +// folder where all the pending deployment will be added 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}" +// deploy file related to the current pipeline job DEPLOY_FILE = "${PENDING_DEPLOY_FOLDER}deploy.${PIPELINE_BUILD_NUMBER}.csv" 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}" + // artifacts coordinates TRIGGER_JOB= "${params.TRIGGER_JOB}" TRIGGER_VERSION= "${params.TRIGGER_VERSION}" TRIGGER_HOST="${params.TRIGGER_HOST}" + // enable the ContinuousDeployment if TRUE TRIGGER_CD="${params.TRIGGER_CD}" + // Useful to identify the remote YAML file 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{ allOf{ 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' } - // in this case the pipeline is triggered by the gCubeBuilder pipeline equals(actual: "${params.TRIGGER_JOB}", expected: 'gCubeBuilder') } } steps { echo 'Cron build enabled. Deploy from system ongoing' script { - echo "pipeline triggered by ${params.TRIGGER_JOB}" - if("${params.TRIGGER_JOB}" == "gCubeBuilder"){ - // do something - println("The Job is gCubeBuilder") - }else{ + echo "pipeline was triggered by ${params.TRIGGER_JOB}" + if("${params.TRIGGER_JOB}" == "${RELEASE_JOB}"){ + println("Going to deploy the Release components") + def releaseList = [] + 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}"; parseDeployPendingJobs(deployFolder); } @@ -143,7 +158,8 @@ pipeline { } steps { - echo 'Do Nothing: cron build disabled' + echo 'Going to sleep' + sh ' exit 1; ' } } stage('New pending deploy ') { @@ -157,7 +173,7 @@ pipeline { } steps { 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; if grep -q \"\${TRIGGER_JOB}\" \${DEPLOY_FILE}; then echo "component ${TRIGGER_JOB} already added. Nothing to add." @@ -245,7 +261,7 @@ pipeline { def deploy(String service, String version, String host){ def now = new Date(); 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(""" 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 def deployPendingJobs( def deployFolder){ println ("searching files in folder ${deployFolder}"); @@ -322,15 +362,43 @@ def deployJobs(def serviceList){ for (def record : serviceList) { println("Processing record: "+record) service=record.split(","); - stage(service[0]){ +//check on target host + if(service[ 2 ]){ + stage(service[0]){ catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') { checkup(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 +}