Merge branch 'catch-output'

This commit is contained in:
Manuele Simi 2021-02-03 20:51:07 -05:00
commit aad91b6b55
2 changed files with 38 additions and 22 deletions

58
Jenkinsfile vendored
View File

@ -34,6 +34,7 @@ pipeline {
AGENT_ROOT_FOLDER = "${agent_root_folder}" AGENT_ROOT_FOLDER = "${agent_root_folder}"
PIPELINE_BUILD_NUMBER = "${env.BUILD_NUMBER}" PIPELINE_BUILD_NUMBER = "${env.BUILD_NUMBER}"
ACTION_REPORT = "${agent_root_folder}/actions.${env.BUILD_NUMBER}.csv" ACTION_REPORT = "${agent_root_folder}/actions.${env.BUILD_NUMBER}.csv"
ACTION_OUTPUT = "${agent_root_folder}/action-output.${env.BUILD_NUMBER}.xml"
ACTION_URL="${actionURL}" ACTION_URL="${actionURL}"
REPO_ROOT="${git_root}" REPO_ROOT="${git_root}"
@ -71,13 +72,16 @@ pipeline {
stage('clone and exec') { stage('clone and exec') {
steps { steps {
script { script {
def start_el = "<actions from=\"${actionURL}\">"
sh "echo -e '${start_el}' >> $ACTION_OUTPUT"
for (int i = 0; i < projects.size(); i++) { for (int i = 0; i < projects.size(); i++) {
stage(projects[i]) { stage(projects[i]) {
echo "About to execute over ${projects[i]}" echo "About to execute over ${projects[i]}"
checkout_and_exec(projects[i], action_code) checkout_and_exec(projects[i])
sh "echo -e ${projects[i]},${git_root}/${projects[i]},Completed >> $ACTION_REPORT" sh "echo -e ${projects[i]},${git_root}/${projects[i]},Completed >> $ACTION_REPORT"
} }
} }
sh "echo -e '</actions>' >> $ACTION_OUTPUT"
} }
} }
} }
@ -88,23 +92,25 @@ pipeline {
always { always {
script { script {
sh ''' sh '''
cp $ACTION_REPORT ./actions.${PIPELINE_BUILD_NUMBER}.csv cp $ACTION_REPORT ./action.${PIPELINE_BUILD_NUMBER}.csv
cat ./actions.${PIPELINE_BUILD_NUMBER}.csv cat ./action.${PIPELINE_BUILD_NUMBER}.csv
cp $ACTION_OUTPUT ./action-output.${PIPELINE_BUILD_NUMBER}.xml
cat ./action-output.${PIPELINE_BUILD_NUMBER}.xml
''' '''
} }
} }
success { success {
echo 'The actions pipeline worked!' echo 'The actions pipeline worked!'
emailext attachmentsPattern: "**/actions.${env.BUILD_NUMBER}.csv", emailext attachmentsPattern: "**/action*${env.BUILD_NUMBER}.*",
to: 'manuele.simi@isti.cnr.it', to: 'manuele.simi@isti.cnr.it,roberto.cirillo@isti.cnr.it',
subject: "Actions report(build #${PIPELINE_BUILD_NUMBER})", subject: "Actions report(build #${PIPELINE_BUILD_NUMBER})",
body: "${currentBuild.fullDisplayName}. Build time: ${currentBuild.durationString}. See ${env.BUILD_URL}" body: "${currentBuild.fullDisplayName}. Build time: ${currentBuild.durationString}. See ${env.BUILD_URL}"
} }
failure { failure {
echo 'The actions pipeline has failed' echo 'The actions pipeline has failed'
emailext attachLog: true, emailext attachLog: true,
to: 'manuele.simi@isti.cnr.it', to: 'manuele.simi@isti.cnr.it,roberto.cirillo@isti.cnr.it',
subject: "[Jenkins build D4S] build ${currentBuild.fullDisplayName} failed", subject: "[Jenkins build D4S] build ${currentBuild.fullDisplayName} failed",
body: "Something is wrong with ${env.BUILD_URL}" body: "Something is wrong with ${env.BUILD_URL}"
} }
@ -117,7 +123,7 @@ pipeline {
* Clones the repository and executes the fragment * Clones the repository and executes the fragment
* NOTE: 'credentialsId' be manually configured in Jenkins to access all the repos * NOTE: 'credentialsId' be manually configured in Jenkins to access all the repos
*/ */
def checkout_and_exec(repo_name, actions) { def checkout_and_exec(repo_name) {
def repo_url = "${git_root}/${repo_name}" def repo_url = "${git_root}/${repo_name}"
sh(script: "rm -r ${repo_name} || true", returnStdout: true)?.trim() sh(script: "rm -r ${repo_name} || true", returnStdout: true)?.trim()
checkout([ checkout([
@ -137,7 +143,7 @@ def checkout_and_exec(repo_name, actions) {
get_last_commit(repo_name) get_last_commit(repo_name)
//exec the action //exec the action
exec(actions, repo_url, repo_name) exec(repo_url, repo_name)
} }
@ -152,25 +158,35 @@ String get_last_commit(repo_name) {
/** /**
Execs the bash fragment Execs the bash fragment
*/ */
def exec(actions, repo_url, repo_name) { def exec(repo_url, repo_name) {
def output = '';
dir(repo_name) { dir(repo_name) {
withCredentials([usernamePassword(credentialsId: '88b54962-1c0e-49cb-8155-22276860f346', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) { withCredentials([usernamePassword(credentialsId: '88b54962-1c0e-49cb-8155-22276860f346', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
def complete_url = "${repo_url}.git" def complete_url = "${repo_url}.git"
def repository = complete_url.replaceFirst(".+://", "https://${GIT_USERNAME}:${GIT_PASSWORD}@") def repository = complete_url.replaceFirst(".+://", "https://${GIT_USERNAME}:${GIT_PASSWORD}@")
sh(""" def bashWrapper = """
git remote set-url origin $repository git remote set-url origin $repository
git remote -v git remote -v
git config user.email "git.gcube@localhost" git config user.email "git.gcube@localhost"
git config user.name "git.gcube" git config user.name "git.gcube"
curl "${ACTION_URL}" -o actions.sh curl "${ACTION_URL}" -o actions.sh
chmod a+x actions.sh chmod a+x actions.sh
source actions.sh source actions.sh
rm actions.sh rm actions.sh
git push --force origin HEAD:master || true git push --force origin HEAD:master || true
""") """
output = sh(script: bashWrapper, returnStdout: true)?.trim()
} }
} }
def xml_action = """
<action repo="${repo_url}">
<stdout>
${output}
</stdout>
</action>
"""
sh "echo -e '${xml_action}' >> $ACTION_OUTPUT"
} }
//a non CPS method is necessary for the usage of splitEachLine() //a non CPS method is necessary for the usage of splitEachLine()

View File

@ -59,7 +59,7 @@ fi
* [Pipeline Syntax](https://jenkins.io/doc/book/pipeline/syntax/) * [Pipeline Syntax](https://jenkins.io/doc/book/pipeline/syntax/)
* [Shared Libraries](https://jenkins.io/doc/book/pipeline/shared-libraries/) * [Shared Libraries](https://jenkins.io/doc/book/pipeline/shared-libraries/)
* [SCM Step](https://jenkins.io/doc/pipeline/steps/workflow-scm-step/) * [SCM Step](https://jenkins.io/doc/pipeline/steps/workflow-scm-step/)
* [Workflow Durable Task Step](https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/)
## Wiki doc ## Wiki doc
* [CI: gCube Actions Pipeline](https://wiki.gcube-system.org/gcube/Continuous_Integration:_Actions_Jenkins_Pipeline) * [CI: gCube Actions Pipeline](https://wiki.gcube-system.org/gcube/Continuous_Integration:_Actions_Jenkins_Pipeline)