Hot answers tagged

88 votes
Accepted

Cleanest way to prematurely exit a Jenkins Pipeline job as a success?

Please note: The following works for scripted pipelines only. For a solution for declarative pipelines see @kgriffs' answer. --- Figured it out. Outside of any stages (otherwise this will just end the ...
  • 4,282
21 votes

Cleanest way to prematurely exit a Jenkins Pipeline job as a success?

The Executor.interrupt(Result) method is the cleanest, most direct way I could find to stop a build prematurely and mark it as a success. script { currentBuild.getRawBuild().getExecutor()....
  • 321
21 votes

Cleanest way to prematurely exit a Jenkins Pipeline job as a success?

You can also use error to exit the current stage, then you don't have to consider the current stage hierarchy and similar stuff: def autoCancelled = false try { stage('checkout') { ... if (...
  • 311
9 votes

Cleanest way to prematurely exit a Jenkins Pipeline job as a success?

If it is acceptable to set the build status to ABORTED instead of SUCCESS, the following snippet may be used within a pipeline stage to short-circuit the build: steps { script { ...
  • 191
5 votes
Accepted

Jenkins Declarative pipeline: Execute stage conditional on execution of previous stage

A pretty simple solution is to declare a global variable on top of your pipeline, and check it's value in your last conditional stage: def flag = false; pipeline { stages { stage('1 - ...
  • 716
5 votes
Accepted

Jenkins UUIDs ... oh the humanity

def suiteRunId = UUID.randomUUID().toString() worked at the top of the Jenkinsfile. Thanks all for your answers.
  • 1,273
5 votes

Jenkins libraries configuration in Jenkinsfile

You should look at Global Shared Libraries, which will allow you to reuse logic across different Jenkinsfiles. Rather than depending on the environment, it's probably safer to explicitly pass ...
4 votes

Jenkins Pipeline downstream job; exit unless it hasn't run in the last x hours

To synthesize Alex and Steve Johnson's answers, you could put this at the top of your downstream job: lastBuild = currentBuild.getPreviousBuild() delay = 2 if Date(lastBuild.timestamp) >= new ...
4 votes

Jenkins Groovy to parse console output and mark build failure

This question seems similar to another on StackOverflow, but seems to be a close, but not exact duplicate of https://stackoverflow.com/questions/37018509/jenkinsfile-build-log To quote those : ...
  • 3,241
4 votes

Jenkins UUIDs ... oh the humanity

The block needs to be resolved for each stage, otherwise you could not use stage dependent methods in the block, so there would need to be some check on what methods to use and what to not use, it ...
  • 5,727
4 votes
Accepted

Is it possible to combine failure stage from groovy with try catch from pipeline?

You may use and update a flag to false in the catch and then use that flag in the when directive of other stage(s) that you want to skip because of any failure in the given stage. Example: boolean ...
  • 56
4 votes
Accepted

Jenkins pipeline lock resources

I found exactly what I was looking for- [Jenkins plugin] (https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin) Which let you create a lock between, slaves , different jobs and more.. ...
  • 115
4 votes
Accepted

Why does Jenkins Pipeline script returnStatus value not match status examined in shell?

There's a few things to note. First is that in the absense of a shebang being supplied, the actual interpreter that will be used is actually "sh", not "bash". Taking a look at the ...
  • 140
3 votes
Accepted

Is it possible to send email notification for Input (GO/NOGO) from a pipeline

You don't need any plugins. You can send an e-mail from your Pipeline using the mail() step. You can respond to the input step by using the REST API as documented in this answer on Stack Overflow. ...
  • 2,822
3 votes

Get changes of the parameterized pipeline on the fly in Jenkins

Currently what you are asking for is not possible. This is due to the way that the Jenkinsfile is loaded into the pipeline. I think dolphy said it best on this StackOverflow answer (I saved this link ...
3 votes

How to check return status of parallel branches in jenkins pipeline

I tried storing the slave job part status in a file and stashing it on node and then unstashing it back on master. It works but I am looking for a cleaner way. Following is the current approach i am ...
3 votes

How to check return status of parallel branches in jenkins pipeline

You may want to try the Join plugin. Some more details about this plugin (from the linked page): This plugin allows a job to be run after all the immediate downstream jobs have completed. In this ...
  • 7,083
2 votes

Cleanest way to prematurely exit a Jenkins Pipeline job as a success?

I was almost able to accomplish this with declarative pipeline. This allows you to exit the before the pipeline starts based on the outcome of a shell script. I was not able to get this working within ...
2 votes
Accepted

Jenkins Pipeline downstream job; exit unless it hasn't run in the last x hours

Took a while and it's a little on the hacky side since I'm no Groovy expert, but I got it working with this; long now = System.currentTimeMillis() node('jenkins2_dedicated_slave'){ stage ('...
  • 4,282
2 votes
Accepted

jenkins parameter behaves differently from internally defined variable

It seems that the jenkinsParam is holding the output after escaping the special characters. You are assuming that jenkinsParam = "test\ntest" and however it looks like it stores it as jenkinsParam = "...
  • 991
2 votes

It is not allowed to use stages more than once [Jenkins_Declarative_Pipeline]

Got answer from StackOverflow and here is the link to the answer. stage('Test') { parallel { stage("Test_A") { stages { stage("Tests_A") { steps { echo 'from ...
2 votes
Accepted

Unable to use the class defined in src in vars of jenkins shared library

It looks like Groovy can't find your class at the path specified. Try renaming the file common.groovy to match the class name and move it to the common folder: src/org/common/CommonFuncs.groovy
1 vote

Jenkinsfile script to waitUntil a folder is created

Your block expression doesn't return a value in all cases. Try this instead: waitUntil { folder.exists() }
  • 2,822
1 vote
Accepted

How to save groovy output to pipeline variable

Have you tried setting returnStdout to true in the sh() command per the Jenkins documentation? You can then assign that output to a variable within your pipeline: def artifactsList = sh(label: '...
  • 968
1 vote
Accepted

How to pass groovy output to pipeline variable

You can send the output of your python script to stdout and capture it in a environment variable and that variable can be passed to other stages in the pipeline. Also, the environment variables can be ...
  • 991
1 vote

How to assign saltstack pillar values from Jenkins comma separated input parameters?

You didn't say how the sls file is executed. One way would be to pass pillars on command line: salt '*' state.apply ftpsync pillar='{"ftpusername": "test", "ftppassword": "0ydyfww3giq8"}' Or you ...
  • 253
1 vote

Does Jenkins job create Jenkinsfile? On execution

No, Jenkins file is not created at execution. Actually Jenkins file is a suite of all steps or activity which you want to build as a job or a groovy code. Jenkins file come from code repository, if ...
  • 1,381
1 vote

Groovy script to show only selected item

You can use the Jenkins pipeline syntax to get the groovy script. For example below code will generate radio buttons for active choice parameter. you can generate scripts like this from your jenkins ...
1 vote

Jenkins Groovy to parse console output and mark build failure

I solved this problem by following this answer on StackOverflow. You can use currentBuild.rawBuild.getLog(10) (the 10 specifies that you want the last 10 lines) to get the last few lines of the log ...
1 vote

Jenkins libraries configuration in Jenkinsfile

I also have the same idea(different config files for different pipeline). I use the pipeline.properties to store my variable. properties = readProperties file: 'pipeline.properties' echo "Immediate ...

Only top scored, non community-wiki answers of a minimum length are eligible