Hot answers tagged

42

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 you're running on which is why running it outside of a stage is important, while setting the currentBuild.result prevents it ...


8

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 = '...


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

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


3

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 ...


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

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....


2

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. ...


2

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

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 ...


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

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 ...


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

Honestly you shouldn't need to use exit command specifically, but there is a Conditional BuildStep Plugin which may achieve the same end result (code that doesn't run). I haven't come up against this yet, so haven't used the plugin. There is also conditionals as found in this prior Stack Overflow post on Jenkins: Jenkins Pipeline Conditional Step/Stage


1

This might work. I used an example X of two hours prior. lastBuild = currentBuild.getPreviousBuild() if Date(lastBuild.timestamp) < new Date().minusHours(2) { <do your thing here> } You may have to whitelist Date(). Cobbled together from this Cloudbees answer.


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