Skip to main content
96 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 ...
Alex's user avatar
  • 4,552
25 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()....
Ben Amos's user avatar
  • 371
22 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 (...
CSchulz's user avatar
  • 321
13 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 { ...
kgriffs's user avatar
  • 231
7 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 - ...
Hedi Nasr's user avatar
  • 746
6 votes
Accepted

Jenkins UUIDs ... oh the humanity

def suiteRunId = UUID.randomUUID().toString() worked at the top of the Jenkinsfile. Thanks all for your answers.
David West's user avatar
  • 1,503
5 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 : ...
Bruce Becker's user avatar
  • 3,693
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 ...
Michael Pereira's user avatar
4 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 ...
Katrina Brock's user avatar
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 ...
lawnmowerlatte's user avatar
4 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 ...
Anirudh Singh's user avatar
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 ...
Jiri Klouda's user avatar
  • 5,817
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 ...
Sid's user avatar
  • 154
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.. ...
amichib's user avatar
  • 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 ...
Kyanar's user avatar
  • 150
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. ...
jayhendren's user avatar
  • 2,952
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 ...
Inbar Rose's user avatar
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 ...
Pierre.Vriens's user avatar
  • 7,205
3 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
Guido Vaccarella's user avatar
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 ('...
Alex's user avatar
  • 4,552
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 = "...
Samit's user avatar
  • 1,021
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 ...
Auto-learner's user avatar
2 votes

In Jenkins, how can I disable "Discard old builds" option via pipeline?

We can't do it by freestyle or manually but we can achieve this by writing a pipeline script for all the jobs in one and running it. Example code: pipeline { agent any stages { stage('Disable ...
AV Reddy's user avatar
  • 101
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() }
jayhendren's user avatar
  • 2,952
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: '...
Argyle's user avatar
  • 1,026
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 ...
Samit's user avatar
  • 1,021
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 ...
bgdnlp's user avatar
  • 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 ...
Subhash's user avatar
  • 1,556
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 ...
vishwas gautam's user avatar
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 ...
Fearnbuster's user avatar

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