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 ...
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()....
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 (...
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 {
...
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 - ...
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.
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 :
...
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
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 ...
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
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 ...
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 ...
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 ...
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..
...
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 ...
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. ...
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
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 ...
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
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 ('...
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 = "...
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
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 ...
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() }
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: '...
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 ...
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 ...
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
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
groovy × 43jenkins-pipeline × 31
jenkins × 30
jenkinsfile × 6
jenkins-plugins × 5
continuous-integration × 2
python × 2
salt × 2
bash × 2
environment-variables × 2
pipeline × 2
docker × 1
kubernetes × 1
git × 1
automation × 1
build-pipeline × 1
yaml × 1
release-management × 1
shell-script × 1
java × 1
jenkins2 × 1
job-dsl × 1
credentials × 1
hudson × 1