gCubeActionExecutor/Jenkinsfile

184 lines
5.8 KiB
Plaintext
Raw Normal View History

2021-01-16 21:57:51 +01:00
#!groovy
/**
* Checkouts and executes actions over a list of repositories.
*
* Manuele Simi (ISTI-CNR)
*/
def agent_root_folder = '/var/lib/jenkins/.m2'
//locate the action file
2021-01-16 22:09:22 +01:00
String actionURL = "https://code-repo.d4science.org/manuele.simi/gCubeActions/raw/branch/main/actions/${action_file}"
2021-01-16 21:57:51 +01:00
println "Querying ${actionURL}"
//save the action code
def action_code = actionURL.toURL().getText()
2021-01-19 03:51:12 +01:00
println "Action code: ${action_code}"
2021-01-16 21:57:51 +01:00
//locate the repos file
2021-01-16 22:09:22 +01:00
String repoURL = "https://code-repo.d4science.org/manuele.simi/gCubeActions/raw/branch/main/repos/${repo_list}"
2021-01-16 21:57:51 +01:00
println "Querying ${repoURL}"
//save the action code
def repo_content = repoURL.toURL().getText()
def projects = parseProjectList(repo_content)
pipeline {
agent {
label 'CD'
}
environment {
AGENT_ROOT_FOLDER = "${agent_root_folder}"
PIPELINE_BUILD_NUMBER = "${env.BUILD_NUMBER}"
ACTION_REPORT = "${agent_root_folder}/actions.${env.BUILD_NUMBER}.csv"
ACTION_URL="${actionURL}"
2021-01-17 04:25:45 +01:00
REPO_ROOT="${repo_root}"
2021-01-16 21:57:51 +01:00
}
parameters {
2021-01-17 04:25:45 +01:00
string(name: 'repo_root',
2021-01-17 04:26:31 +01:00
defaultValue: 'https://code-repo.d4science.org/gCubeSystem/',
2021-01-17 04:25:45 +01:00
description: 'The root URL of the repositories')
2021-01-16 21:57:51 +01:00
string(name: 'repo_list',
defaultValue: 'default_list.txt',
description: 'The file with the list of repositories to update')
string(name: 'action_file',
defaultValue: '',
description: 'The file with the Bash fragment to execute.')
2021-01-17 04:25:45 +01:00
2021-01-16 21:57:51 +01:00
}
stages {
stage('initialize report') {
steps {
sh '''
date=`date`
echo "#Build ${PIPELINE_BUILD_NUMBER},," > $ACTION_REPORT
echo "#StartTime ${date},," >> $ACTION_REPORT
echo "Project,Repo,Result" >> $ACTION_REPORT
'''
}
}
2021-01-16 22:13:31 +01:00
stage('clone and exec') {
2021-01-16 21:57:51 +01:00
steps {
script {
for (int i = 0; i < projects.size(); i++) {
stage(projects[i]) {
echo "About to execute over ${projects[i]}"
checkout_and_exec(projects[i], action_code)
2021-01-24 05:18:57 +01:00
sh "echo -e ${projects[i]},"${repo_root}/${repo_name}"",Completed >> $ACTION_REPORT"
2021-01-16 21:57:51 +01:00
}
}
}
}
}
}
// post-build actions
post {
always {
script {
sh '''
cp $ACTION_REPORT ./actions.${PIPELINE_BUILD_NUMBER}.csv
cat ./actions.${PIPELINE_BUILD_NUMBER}.csv
'''
}
}
success {
echo 'The actions pipeline worked!'
emailext attachmentsPattern: "**/actions.${env.BUILD_NUMBER}.csv",
to: 'manuele.simi@isti.cnr.it',
subject: "Actions report(build #${PIPELINE_BUILD_NUMBER})",
body: "${currentBuild.fullDisplayName}. Build time: ${currentBuild.durationString}. See ${env.BUILD_URL}"
}
failure {
echo 'The actions pipeline has failed'
emailext attachLog: true,
to: 'manuele.simi@isti.cnr.it',
subject: "[Jenkins build D4S] build ${currentBuild.fullDisplayName} failed",
body: "Something is wrong with ${env.BUILD_URL}"
}
}
}
/**
* Clones the repository and executes the fragment
* NOTE: 'credentialsId' be manually configured in Jenkins to access all the repos
*/
def checkout_and_exec(repo_name, actions) {
2021-01-17 05:21:06 +01:00
def repo_url = "${repo_root}/${repo_name}"
2021-01-16 21:57:51 +01:00
sh(script: "rm -r ${repo_name} || true", returnStdout: true)?.trim()
checkout([
$class : 'GitSCM',
branches : [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions : [
[$class: 'RelativeTargetDirectory', relativeTargetDir: repo_name],
[$class: 'CloneOption', noTags: false, reference: '']
],
submoduleCfg : [],
userRemoteConfigs : [
[credentialsId: '88b54962-1c0e-49cb-8155-22276860f346', url: repo_url] //git.gcube credentials on jenkins
]
])
// just to show we can access the cloned repository
get_last_commit(repo_name)
//exec the action
exec(actions, repo_url, repo_name)
}
String get_last_commit(repo_name) {
String msg;
dir(repo_name) {
msg = sh(script: 'git rev-parse HEAD', returnStdout: true)?.trim()
}
return msg;
}
/**
Execs the bash fragment
*/
def exec(actions, repo_url, repo_name) {
dir(repo_name) {
withCredentials([usernamePassword(credentialsId: '88b54962-1c0e-49cb-8155-22276860f346', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
2021-01-18 04:46:13 +01:00
def complete_url = "${repo_url}.git"
2021-01-18 04:43:01 +01:00
def repository = complete_url.replaceFirst(".+://", "https://${GIT_USERNAME}:${GIT_PASSWORD}@")
2021-01-16 21:57:51 +01:00
sh("""
2021-01-18 04:43:01 +01:00
git remote set-url origin $repository
2021-01-18 04:39:37 +01:00
git remote -v
2021-01-18 04:48:34 +01:00
git config user.email "git.gcube@localhost"
git config user.name "git.gcube"
curl "${ACTION_URL}" -o actions.sh
chmod a+x actions.sh
source actions.sh
rm actions.sh
2021-01-24 05:18:57 +01:00
git push origin HEAD:master || true
2021-01-16 21:57:51 +01:00
""")
}
}
}
//a non CPS method is necessary for the usage of splitEachLine()
@NonCPS
def parseProjectList(def text) {
def projects = []
2021-01-17 05:18:33 +01:00
text.readLines().each { project ->
2021-01-16 21:57:51 +01:00
if (!project)
return
projects.add(project)
}
return projects
}