jenkins-pipeline

Create JSON strings from Groovy variables in Jenkins Pipeline

跟風遠走 提交于 2019-11-27 20:33:03
问题 I have to create this JSON file in Groovy. I have try many things ( JsonOutput.toJson() / JsonSlurper.parseText() ) unsuccessfully. { "attachments":[ { "fallback":"New open task [Urgent]: <http://url_to_task|Test out Slack message attachments>", "pretext":"New open task [Urgent]: <http://url_to_task|Test out Slack message attachments>", "color":"#D00000", "fields":[ { "title":"Notes", "value":"This is much easier than I thought it would be.", "short":false } ] } ] } This is for posting a

How to add a timeout step to Jenkins Pipeline

£可爱£侵袭症+ 提交于 2019-11-27 20:20:50
When you are using a free style project you can set that after 20 minutes the build is aborted if not concluded. How is this possible with a Jenkins Multi Branch Pipeline Project? StephenKing You can use the timeout step: timeout(20) { node { sh 'foo' } } If you need a different TimeUnit than MINUTES , you can supply the unit argument: timeout(time: 20, unit: 'SECONDS') { EDIT Aug 2018: Nowadays with the more common declarative pipelines (easily recognized by the top-level pipeline construct), timeouts can also be specified using options on different levels (per overall pipeline or per stage):

Can I create dynamically stages in a Jenkins pipeline?

南笙酒味 提交于 2019-11-27 19:43:37
I need to launch a dynamic set of tests in a declarative pipeline. For better visualization purposes, I'd like to create a stage for each test. Is there a way to do so? The only way to create a stage I know is: stage('foo') { ... } I've seen this example , but I it does not use declarative syntax. david.perez Use the scripted syntax that allows more flexibility than the declarative syntax, even though the declarative is more documented and recommended. For example stages can be created in a loop: def tests = params.Tests.split(',') for (int i = 0; i < tests.length; i++) { stage("Test ${tests[i

Abort current build from pipeline in Jenkins

房东的猫 提交于 2019-11-27 19:17:29
I have a Jenkins pipeline which has multiple stages, for example: node("nodename") { stage("Checkout") { git .... } stage("Check Preconditions") { ... if(!continueBuild) { // What do I put here? currentBuild.xxx ? } } stage("Do a lot of work") { .... } } I want to be able to cancel (not fail) the build if certain preconditions are not met and there is no actual work to be done. How can I do this? I know the currentBuild variable is available, but I can't find the documentation for it. You can mark the build as ABORTED, and then use the error step to cause the build to stop: if (!continueBuild)

Extract version ID from POM in a Jenkins pipeline

巧了我就是萌 提交于 2019-11-27 19:03:04
I've created a pipeline and using the embedded groovy pipeline script definition and can't seem to get the version ID of the project from the POM. I tried this which works in a groovy console but on in the Jenkins build pipeline script: def project = new XmlSlurper().parse(new File("pom.xml")) def pomv = project.version.toString() According to the documentation Jenkins has a $POM_VERSION but the value doesn't have anything in it when I assign it to a variable and echo it out. def pomv = "$POM_VERSION" OR def pomv = '$POM_VERSION" Use readMavenPom like this: pom = readMavenPom file: 'pom.xml'

How to I get the url of build triggered with build step on Jenkins?

北慕城南 提交于 2019-11-27 18:53:34
问题 While using Groovy based pipelines in Jenkins you can trigger child jobs using the the build stage. Still the documentation above states nothing regarding what kind of return object you would get and what attributes it has. The only thing I found so far is that I can use build.getResult() to obtain the result of the triggered job. Still, I do want to obtain the URL of this job. 回答1: From the documentation for the build step in /pipeline-syntax ( waitFormCompletion argument, the original has

How do I prevent two pipeline jenkins jobs of the same type to run in parallel on the same node?

耗尽温柔 提交于 2019-11-27 18:27:44
I want not to allow two jobs of the same type (same repository) not to run in parallel on the same node. How can I do this using groovy inside Jenkinsfile? hypery2k You got at the disableConcurrentBuilds property: properties properties: [ ... disableConcurrentBuilds(), ... ] Then the job would wait the older one to finish first The answer provided in https://stackoverflow.com/a/43963315/6839445 is deprecated. The current method to disable concurrent builds is to set options: options { disableConcurrentBuilds() } Detailed description is available here: https://jenkins.io/doc/book/pipeline

Use Jenkins 'Mailer' inside pipeline workflow

社会主义新天地 提交于 2019-11-27 17:59:28
I'd like to leverage the existing Mailer plugin from Jenkins within a Jenkinsfile that defines a pipeline build job. Given the following simple failure script I would expect an email on every build. #!groovy stage 'Test' node { try { sh 'exit 1' } finally { step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'me@me.com', sendToIndividuals: true]) } } The output from the build is: Started by user xxxxx [Pipeline] stage (Test) Entering stage Test Proceeding [Pipeline] node Running on master in /var/lib/jenkins/jobs/rpk-test/workspace [Pipeline] { [Pipeline] sh [workspace] Running

How to fix NotSerializableException error during Jenkins workflow build?

末鹿安然 提交于 2019-11-27 17:44:26
问题 When I run the following code on the Jenkins workflow (Jenkins 1.609.1 ,workflow 1.8) I get error of 'NotSerializableException' (also below). However, if I move the "build job" outside the "for" scope it works fine (the job is activated). Any ideas why this behavior? node('master') { ws('/opt/test) { def file = "/ot.property" def line = readFile (file) def resultList = line.tokenize() for(item in resultList ) { build job: 'testjob_1' } } } Got error: Running: End of Workflow java.io

Check if a file exists in jenkins pipeline

冷暖自知 提交于 2019-11-27 17:04:57
问题 I am trying to run block if a directory exists in my jenkins workspace and the pipeline step "fileExists: Verify file exists" in workspace doesn't seem to work correctly. I'm using Jenkins v 1.642 and Pipeline v 2.1. and trying to have a condition like if ( fileExists 'test1' ) { //Some block } What are the other alternatives I have within pipeline? 回答1: You need to use brackets when using the fileExists step in an if condition or assign the returned value to a variable Using variable: def