Hot answers tagged

73

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 particular stage as a success) do the following; if( $VALUE1 == $VALUE2 ) { currentBuild.result = 'SUCCESS' return } return will stop the stage or node ...


17

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 (your condition) { autoCancelled = true error('Aborting the build.') } } } catch (e) { if (autoCancelled) { currentBuild.result = '...


13

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().interrupt(Result.SUCCESS) sleep(1) // Interrupt is not blocking and does not take effect immediately. } Pros: Works in a declarative pipeline just as well as a ...


5

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 parameters and returns values from the functions, which will make their behavior more explicit. Another side benefit is that you will be able to unit test them using


4

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 Date().minusHours(delay) { currentBuild.result = 'SUCCESS' echo "Less than ${delay} hours since the last build. Job will not run." return }


4

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.. (in contrast to the plugin description)


4

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 : if you just want to check, that your log contains string myTestString you can just call manager.logContains('.myTestString.') If you want to get some ...


4

def suiteRunId = UUID.randomUUID().toString() worked at the top of the Jenkinsfile. Thanks all for your answers.


4

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 would lead to even more problems. Further each stage can be executed on different node, so the code needs to re-resolve it. Otherwise it would just behave ...


4

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 flagStageOneSuccess = false pipeline { agent any stages { stage ('stage-1') { steps { try { // ...


4

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 - Always') { steps { sh './always.sh' } } stage('2 - Maybe') { when { condition } ...


3

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 { currentBuild.result = 'ABORTED' error("Aborting the build.") } } I tested setting the result to SUCCESS, but doing so resulted in a failed build (...


3

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. Put the two together and you should be able to craft an e-mail that contains buttons for responding to the input.


3

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 using: def branches = [:] def allNodes = Jenkins.getInstance().getNodes() for (int i =0; i < allNodes.size(); i++) { String nodeName = allNodes[i].name....


3

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 way, the execution can branch out and perform many steps in parallel, and then run a final aggregation step just once after all the parallel work is finished. ...


3

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 a while ago to follow it for changes, because I had the same questions): Jenkins does not know about the new parameters until it retrieves, parses, and runs ...


2

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 ('check time') { sh '''lastBuild=$(mktemp) curl -u [jenkins username]:[API key for user] "http://[jenkins URL]/[job name]/lastSuccessfulBuild/api/...


2

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 = "test\\ntest" See how it behaves if I use echo it these two different cases: (I have tested this in groovy shell) 1st Case: jenkinsParam = "test\ntest" print ...


2

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 A' } } stage("Archieve") { steps { echo 'from Archieve' } } } } stage("Test_B") { ... } ...


2

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 Jenkins source for the BourneShellScript task, what happens is that Jenkins does not actually return the exit code from the subshell - it monitors a file called ...


1

Your block expression doesn't return a value in all cases. Try this instead: waitUntil { folder.exists() }


1

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: 'Running amd_distribution_input transformation', returnStdout: true, script: 'python amd_distribution_input_transformation.py') and check what kind of object that ...


1

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 used inside the scripts. You can do something like this: def call(Map parameters) { def CREDENITAL_ID = parameters.secret def DOMAIN_DESCRIPTOR_URL = ...


1

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 one ${properties.repo}" Drawbacks: Due to the groovy early evaluation problems. The value will be null when you use the ${properties.repo} in some shared ...


1

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 can set the group name as a grain on the minion in advance. Yet another way would be to set the group name as a jinja variable in the sls formula, depending on ...


1

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 you added file in your project. Below is a Jenkins file example: pipeline { agent any stages { stage('Build') { steps { ...


1

try this one static void main(String[] args) { // Initializing 2 variables def x = 5; def y = 10; //Performing addition of 2 operands println(x+y); } } check this URL


1

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 instance at http://localhost:8080/pipeline-syntax/. More info is here https://jenkins.io/doc/book/pipeline/getting-started/#snippet-generator def ...


1

I would recommend against importing if at all possible. You can use built-in functionality to achieve the same results. Also be aware of "In-process Script Approval" https://stackoverflow.com/a/42662243/1678094 // GET def get = new URL("https://httpbin.org/get").openConnection(); def getRC = get.getResponseCode(); println(getRC); if(getRC.equals(200)) { ...


1

def repository = 'git@somerepo' import hudson.tasks.Shell job = Jenkins.instance.createProject(FreeStyleProject, 'TestJob') job.setDescription("Some description") job.displayName = 'SomeTestJob(TESTING groovy)' job.scm = new hudson.plugins.git.GitSCM(repository) job.scm.branches = [new BranchSpec('*/master')] job.save() Thanks a lot Michael Durrant for ...


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