Strange variable scoping behavior in Jenkinsfile

TL;DR variables defined with def in the main script body cannot be accessed from other methods. variables defined without def can be accessed directly by any method even from different scripts. It’s a bad practice. variables defined with def and @Field annotation can be accessed directly from methods defined in the same script. Explanation When … Read more

Is it possible to capture the stdout from the sh DSL command in the pipeline

Now, the sh step supports returning stdout by supplying the parameter returnStdout. // These should all be performed at the point where you’ve // checked out your sources on the slave. A ‘git’ executable // must be available. // Most typical, if you’re not cloning into a sub directory gitCommit = sh(returnStdout: true, script: ‘git … Read more

How do I make a Jenkins job start after multiple simultaneous upstream jobs succeed?

Pipeline plugin You can use the Pipeline Plugin (formerly workflow-plugin). It comes with many examples, and you can follow this tutorial. e.g. // build stage ‘build’ … // deploy stage ‘deploy’ … // run tests in parallel stage ‘test’ parallel ‘functional’: { … }, ‘performance’: { … } // promote artifacts stage ‘promote’ … Build … Read more

Jenkins HTML Publisher Plugin: No external links with Jenkins 1.643

The issue you’re seeing is likely related to recent security fixes. See the Configuring Content Security Policy wiki page for details on how to relax the Jenkins configuration. The CSP header sent by Jenkins can be modified by setting the system property hudson.model.DirectoryBrowserSupport.CSP: If its value is the empty string, e.g. java -Dhudson.model.DirectoryBrowserSupport.CSP= -jar jenkins.war … Read more

Jenkins REST API Create job

Jenkins by default has CSRF Protection enabled which prevents one-click attacks. To invoke the request, you need to obtain the crumb from /crumbIssuer/api/xml using your credentials and include it into your request. For example: CRUMB=$(curl -s ‘http://USER:TOKEN@localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,”:”,//crumb)’) Then you can create a job (by including the crumb into your header): curl -X POST -H “$CRUMB” … Read more

Show a Jenkins pipeline stage as failed without failing the whole job

This is now possible, even with declarative pipelines: pipeline { agent any stages { stage(‘1’) { steps { sh ‘exit 0’ } } stage(‘2’) { steps { catchError(buildResult: ‘SUCCESS’, stageResult: ‘FAILURE’) { sh “exit 1” } } } stage(‘3’) { steps { sh ‘exit 0’ } } } } In the example above, all stages … Read more

How can I trigger another job from a jenkins pipeline (jenkinsfile) with GitHub Org Plugin?

In addition to the above mentioned answers: I wanted to start a job with a simple parameter passed to a second pipeline and found the answer on http://web.archive.org/web/20160209062101/https://dzone.com/refcardz/continuous-delivery-with-jenkins-workflow So i used: stage (‘Starting ART job’) { build job: ‘RunArtInTest’, parameters: [[$class: ‘StringParameterValue’, name: ‘systemname’, value: systemname]] }