Compare commits

..

No commits in common. "master" and "feature/23952" have entirely different histories.

7 changed files with 113 additions and 610 deletions

214
Jenkinsfile vendored
View File

@ -1,9 +1,8 @@
#!groovy
import org.yaml.snakeyaml.Yaml
import groovy.json.JsonSlurper
/**
* Deploy gCube components on D4Science Infrastructure (dev and preprod environments)
* Deploy gCube components on D4Science Infrastructure (dev-only)
*
* Roberto Cirillo (ISTI-CNR)
*/
@ -58,7 +57,7 @@ pipeline {
AGENT_ROOT_FOLDER = "${agent_root_folder}"
// folder where all the pending deployment will be added
PENDING_DEPLOY_FOLDER="${agent_root_folder}/CD/"
ANSIBLE_ROOT_FOLDER = "${agent_root_folder}/ansible-repos/ansible-playbooks/d4science-smartgears-services"
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"
@ -71,22 +70,18 @@ pipeline {
TRIGGER_HOST="${params.TRIGGER_HOST}"
// enable the ContinuousDeployment if TRUE
TRIGGER_CD="${params.TRIGGER_CD}"
// generic parameter that can be passed to the ansible job
TRIGGER_FOO="${params.TRIGGER_FOO}"
// Useful to identify the remote YAML file
GCUBE_RELEASE_NUMBER = "${params.gCube_release_version}"
//Category name of the components candidates to the remote deployment
DEPLOY_CATEGORY="${env.DEPLOY_CATEGORY}"
//Job name of the release pipeline
RELEASE_JOB='Pipeline-gCubeBuilder'
RELEASE_JOB='gCubeBuilder'
//URL to the YAML file. Used alternatively to RELEASE_JOB
RELEASE_URL= "${params.RELEASE_URL}"
// username of ReleaseManager. Used for limiting the preproduction deployments
RELEASE_MANAGER_USER='giancarlo.panichi'
RELEASE_MANAGER_USER='roberto.cirillo'
// name of Smartgears distribution group, defined in the yaml file ( only for release deployments)
DISTRIBUTION_CATEGORY='Distribution'
// name of Portlets group, defined in the yaml file ( only for release deployments)
PORTLET_CATEGORY='Portlets'
}
parameters {
@ -102,12 +97,12 @@ pipeline {
booleanParam(name: 'TRIGGER_CD',
defaultValue: true,
description: 'Set to false to avoid current deploy')
string(name: 'TRIGGER_FOO',
defaultValue: '',
description: 'Additional parameter to pass to the ansible job. Leave blank if not used')
string(name: 'gCube_release_version',
defaultValue: '',
description: 'The number of the current gCube release. Leave blank if executed outside gCube release.')
description: 'The number of the gCube release. Leave blank if executed outside gCube release.')
string(name: 'report_number',
defaultValue: '',
description: 'The build report number on Git to pull the notes. Leave blank if executed outside gCube release.')
string(name: 'RELEASE_URL',
defaultValue: '',
description: 'URL to the yaml file. Leave blank if executed outside gCube release.')
@ -129,12 +124,12 @@ pipeline {
anyOf{
allOf{
triggeredBy 'TimerTrigger'
environment name: 'IS_SCHEDULED', value: 'True'
environment name: 'IS_SCHEDULED_TEST', value: 'True'
}
}
}
steps {
echo 'Cron build enabled. \033[31;1;4mDeploy from system ongoing\033[0m'
echo 'Cron build enabled. Deploy from system ongoing'
script {
echo "pipeline was triggered by ${params.TRIGGER_JOB}"
println("Going to deploy all the pending deployments")
@ -146,19 +141,22 @@ pipeline {
}
stage('Nothing to do ') {
when{
anyOf{
allOf{
triggeredBy 'TimerTrigger'
environment name: 'IS_SCHEDULED', value: 'False'
environment name: 'IS_SCHEDULED_TEST', value: 'False'
}
}
}
steps {
echo '\033[31;1;4mGoing to sleep\033[0m'
sh ' exit 0; '
echo 'Going to sleep'
sh ' exit 1; '
}
}
stage('New pending deploy ') {
when{
environment name: 'IS_SCHEDULED', value: 'True'
environment name: 'IS_SCHEDULED_TEST', value: 'True'
anyOf{
triggeredBy 'BuildUpstreamCause'
triggeredBy 'UpstreamCause'
@ -168,8 +166,7 @@ pipeline {
}
steps {
sh '''
echo '\033[31;1;4mNew pending deploy/033[0m'
echo "Cron build enabled. Adding ${TRIGGER_JOB} - ${TRIGGER_VERSION} to the queue"
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."
@ -188,7 +185,7 @@ pipeline {
}
stage('Deploy from job ') {
when{
environment name: 'IS_SCHEDULED', value: 'False';
environment name: 'IS_SCHEDULED_TEST', value: 'False';
expression {
env.TRIGGER_CD.toBoolean()
}
@ -200,17 +197,11 @@ pipeline {
}
steps {
script{
echo '\033[31;1;4mNew upstream deploy ongoing\033[0m'
echo "Cron build disabled. Component: ${params.TRIGGER_JOB} - ${params.TRIGGER_VERSION}"
ansiColor('xterm') {
catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') {
echo "Cron build disabled. New deploy of ${params.TRIGGER_JOB} - ${params.TRIGGER_VERSION} ongoing"
ansiColor("xterm") {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
checkup("${params.TRIGGER_JOB}", "${params.TRIGGER_VERSION}", "${params.TRIGGER_HOST}");
}
if (currentBuild.result == "UNSTABLE") {
return;
}
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
deploy("${params.TRIGGER_JOB}", "${params.TRIGGER_VERSION}", "${params.TRIGGER_HOST}", "${params.TRIGGER_FOO}");
deploy("${params.TRIGGER_JOB}", "${params.TRIGGER_VERSION}", "${params.TRIGGER_HOST}");
}
}
}
@ -232,27 +223,29 @@ pipeline {
steps {
script{
if("${DEPLOY_CATEGORY}"){
echo '\033[31;1;4mGoing to deploy gCube Release \033[0m'
println("Going to deploy the Release components")
deployRelease("${DEPLOY_CATEGORY}", yamlConfig);
}else println ("CATEGORIES NOT FOUND. ENVIRONMENT NOT SET FOR RELEASE DEPLOYMENT. NOTHING TO DO");
}else { println ("CATEGORIES NOT FOUND ON YAML FILE. SKIP TO NEXT STAGE");}
}
}
}
stage('Deploy manually ') {
when{
allOf{
environment name: 'IS_SCHEDULED', value: 'False';
environment name: 'IS_SCHEDULED_TEST', value: 'False';
triggeredBy 'UserIdCause'
}
expression { params.TRIGGER_JOB != "${RELEASE_JOB}" }
}
steps {
echo '\033[31;1;4mNew deploy ongoing manually triggered\033[0m'
echo "Cron build disabled. Pipeline executed Manually. New deploy of ${params.TRIGGER_JOB} - ${params.TRIGGER_VERSION} ongoing"
ansiColor("xterm") {
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
deploy("${params.TRIGGER_JOB}", "${params.TRIGGER_VERSION}", "${params.TRIGGER_HOST}", "${params.TRIGGER_FOO}");
deploy("${params.TRIGGER_JOB}", "${params.TRIGGER_VERSION}", "${params.TRIGGER_HOST}");
}
}
}
}
}
post {
@ -267,23 +260,30 @@ pipeline {
echo 'The deploy pipeline worked!'
emailext attachLog: true,//attachmentsPattern: "**/${ACTION_DEPLOY_FILE}.csv",
to: 'jenkinsbuilds@d4science.org',
to: 'roberto.cirillo@isti.cnr.it',
subject: "[Jenkins-gCubeDeployer] Deploy report",
body: "${currentBuild.fullDisplayName}. Build time: ${currentBuild.durationString}. See ${env.BUILD_URL}. "
}
failure {
echo 'The deploy pipeline has failed'
emailext attachLog: true,
to: 'jenkinsbuilds@d4science.org',
to: 'roberto.cirillo@isti.cnr.it',
subject: "[Jenkins-gCubeDeployer] deploy ${currentBuild.fullDisplayName} failed",
body: "Something is wrong with ${env.BUILD_URL}"
}
}
}
/* CHECKUP METHODS */
//launch ansible deploy
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 $ANSIBLE_ROOT_FOLDER;git pull;./deployService.sh $service $version $host;", 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 checkup(String service, String version, String host){
@ -296,6 +296,17 @@ def checkup(String service, String version, String host){
""")
}
//launch ansible deploy
def deployRelease(String service, String version, String smartgears){
def now = new Date();
println("Going to deploy the service "+service+" with version: "+version+" on preproduction ");
def statusCode = sh( script: "cd $ANSIBLE_ROOT_FOLDER;git pull;./deployPreprodService.sh $service $version $smartgears ;", 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("""
@ -306,10 +317,6 @@ def checkupRelease(String service, String version){
esac
""")
}
/* DEPLOY METHODS*/
//parse all csv files found in the local folder and deploy the components defined inside
def parseDeployPendingJobs( def deployFolder){
println ("searching files in folder ${deployFolder}");
@ -330,7 +337,6 @@ def parseDeployPendingJobs( def deployFolder){
deployJobs(serviceList.unique())
}
}
//Deploy jobs from an input list with the following elements: serviceName,serviceVersion,targetHost. Create a new stage for any job
def deployJobs(def serviceList){
for (def record : serviceList) {
@ -339,49 +345,31 @@ def deployJobs(def serviceList){
println("Processing deploy: "+service[ 0 ])
catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') {
checkup(service[0], service[ 1 ], service[2]);
}
if (currentBuild.result == "UNSTABLE") {
return;
}
catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
deploy(service[0], service[ 1 ], service[2]);
}
}
}
}
//Run ansible deploy
def deploy(String service, String version, String host, String foo){
def now = new Date();
println("Going to deploy the service "+service+" with version: "+version+" on target: "+host+" with argument: "+foo);
dir("${ANSIBLE_ROOT_FOLDER}/CD/"){
sh("""
git pull;
""")
def statusCode = sh( script: "./deployService.sh $service $version $host $foo;", returnStatus: true);
sh("""
echo " last exit code \$?";
""")
println("Deploy statusCode value: "+statusCode);
if (statusCode != 0){
currentBuild.result = 'FAILURE'
sh("""
exit $statusCode;
""")
//Deploy only release components. All the component are passed as: componentName,componentVersion
def deployReleaseJobs(def serviceList, String smartgears){
for (def record : serviceList) {
service=record.split(",");
stage(service[0]){
println("Processing RELEASE deploy: service "+service[ 0 ]+" with version "+service[ 1 ]+" and SG "+smartgears)
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
checkupRelease(service[0], service[ 1 ]);
if (smartgears)
deployRelease(service[0], service[ 1 ], smartgears);
else
deployRelease(service[0], service[ 1 ], null);
}
}
}
/* DEPLOY RELEASE METHODS*/
// collects all the components defined inside one or more yaml categories matching categoryList and invoke the deploy
}
// collects all the components defined inside one or more yaml categories matching categoryList
def deployRelease(def categoryList, def yaml){
def isPortlet= false
def releaseList = []
def portlets = []
String smartgears
def categories=categoryList.split(',')
yaml.gCube_release.Components.each { group_name, component_list ->
@ -389,16 +377,9 @@ def deployRelease(def categoryList, def yaml){
for ( def category : categories){
if("${group_name}" == "$category"){
if (category == "${DISTRIBUTION_CATEGORY}"){
//searching smartgears-distribution (os smartgears-distribution-legacy) version in Distribution component defined inside the yaml file
def sg= component_list?.collect{ if ("$it.name" == 'smartgears-distribution' || "$it.name" == 'smartgears-distribution-legacy' ){ return "$it.version"; }}
smartgears=(sg !=null ) ? sg[0] : ''
}else if (category =="${PORTLET_CATEGORY}"){
portlets += component_list?.collect{ if ("${it.group_id}" != "null") return "{\"group_id\": \"$it.group_id\", \"name\": \"$it.name\", \"version\": \"$it.version\", \"extension\": \"war\"} "; else return "{\"name\": \"$it.name\", \"version\": \"$it.version\", \"extension\": \"war\"}"}
def jsonContent= "{\"generic_portlets\": "+portlets+"}"
echo "json: $jsonContent"
writeJSON file: 'portlets.json', json: jsonContent
def output = sh(returnStdout: true, returnStdoutTrim: true, script: "mv portlets.json $ANSIBLE_ROOT_FOLDER")
isPortlet= true
//searching smartgears-distribution version in Distribution component of defined inside the yaml file
def sg= component_list?.collect{ if ("$it.name" == 'smartgears-distribution'){ return "$it.version"; }}
smartgears=(sg !=null ) ? sg[0] : null
}else{
releaseList += component_list?.collect {return "$it.name,$it.version" }
}
@ -407,58 +388,5 @@ def deployRelease(def categoryList, def yaml){
}
}
}
deployReleaseJobs(releaseList, smartgears, isPortlet);
}
//Deploy only release components, starting from a servicelist. All the component are passed as: componentName,componentVersion
def deployReleaseJobs(def serviceList, String smartgears, boolean isPortlet){
if (serviceList || isPortlet){
println("Going to deploy release components");
for (def record : serviceList) {
service=record.split(",");
stage(service[0]){
println("Processing RELEASE deploy: service "+service[ 0 ]+" with version "+service[ 1 ]+" and SG "+smartgears)
catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
checkupRelease(service[0], service[ 1 ]);
if (smartgears)
deployReleaseJob(service[0], service[ 1 ], smartgears);
else
deployReleaseJob(service[0], service[ 1 ], '');
}
}
}
if (isPortlet){
stage("portlets"){
deployReleaseJob('generic-portlet', '','');
}
}
}else{
println("Release components not found. Skip this stage");
}
}
//Run ansible deploy related to a single service
def deployReleaseJob(String service, String version, String smartgears){
def now = new Date();
println("Going to deploy "+service+" "+version+" on preproduction ");
dir("${ANSIBLE_ROOT_FOLDER}/CD/"){
sh("""
git pull;
""")
def statusCode = sh( script: "./deployPreprodService.sh $service $version $smartgears ;", returnStatus: true);
sh("""
echo " last exit code \$?";
""")
println("Deploy ended with status: "+statusCode);
if (statusCode != 0){
currentBuild.result = 'FAILURE'
sh("""
exit $statusCode;
""")
}
}
deployReleaseJobs(releaseList, smartgears);
}

File diff suppressed because one or more lines are too long

View File

@ -9,42 +9,20 @@ Jenkins Pipeline script to manage one or more gCube components deployment
* [Pipeline: Basic Steps](https://plugins.jenkins.io/workflow-basic-steps)
* [Email Extension](https://plugins.jenkins.io/email-ext) (to send emails with attachments)
* Jenkins agent(s) configured with Ansible ver. 2.9.27 or newer
* One or more Jenkins agents labeled as 'ansible'
* One or more Jenkins agents labeled as 'CD'
## Expected usage
* ContinuousDeployment(CD): configured as post step build on jenkinsjob
* Scheduled by system: In this case all the pending deployments (CD) will be managed by a temporized trigger
* Manually from Jenkins GUI
## Expected environment variables
The variables below must be specified as environment variables on the agent where the deployments will be executed:
* IS_SCHEDULED (True | False): if true all the deployment requests will be performed periodically as specified by cron
* ENVIRONMENT: ( DEV | PREPROD | PROD) Indicates the target environment where will be deployed all the artifacts
* DEPLOY_CATEGORY: (Related to release build) one or more Component group (separated by commas) on yaml file where search the components to deploy
* CD: (Continuous Deployment: True | False) if True the remote deploy will be performed
## Expected usage
## References
* [Jenkins Pipeline](https://jenkins.io/doc/book/pipeline/)
* [Pipeline Syntax](https://jenkins.io/doc/book/pipeline/syntax/)
## Examples
* When configured as post step build on jenkinsjob, there are some variables to pass as triggered parameters. All the values are dinamically set as shown below:
[[images/gCubeDeployer-JenkinsJob.png]]
* The pipeline can run manually. This option is conceived for debug purpose:
[[images/gCubeDeployer-manualRun.png]]
* Follows a stage view example when the pipeline was scheduled by system, but there were any action to do:
[[images/gCubeDeployer-stageview.png]]
## Flow Chart
[[images/FlowChartgCubeDeployer.jpeg]]
## Wiki doc
## License

Binary file not shown.

Before

Width:  |  Height:  |  Size: 98 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 253 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 194 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 403 KiB