jenkins-pipeline

post equivalent in scripted pipeline?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 20:33:13
问题 What is the syntax of 'post' in scripted pipeline comparing to declarative pipeline? https://jenkins.io/doc/book/pipeline/syntax/#post 回答1: For scripted pipeline, everything must be written programmatically and most of the work is done in the finally block: Jenkinsfile (Scripted Pipeline): node { try { stage('Test') { sh 'echo "Fail!"; exit 1' } echo 'This will run only if successful' } catch (e) { echo 'This will run only if failed' // Since we're catching the exception in order to report on

How to add a timeout step to Jenkins Pipeline

二次信任 提交于 2019-11-26 20:04:24
问题 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? 回答1: 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),

Use Jenkins 'Mailer' inside pipeline workflow

人走茶凉 提交于 2019-11-26 19:17:55
问题 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

Jenkins: Cannot define variable in pipeline stage

≯℡__Kan透↙ 提交于 2019-11-26 19:13:49
问题 I'm trying to create a declarative Jenkins pipeline script but having issues with simple variable declaration. Here is my script: pipeline { agent none stages { stage("first") { def foo = "foo" // fails with "WorkflowScript: 5: Expected a step @ line 5, column 13." sh "echo ${foo}" } } } However, I get this error: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 5: Expected a step @ line 5, column 13. def foo = "foo" ^ I'm on Jenkins 2.7.4 and

Jenkins Pipeline Wipe Out Workspace

╄→гoц情女王★ 提交于 2019-11-26 18:57:32
问题 We are running Jenkins 2.x and love the new Pipeline plugin. However, with so many branches in a repository, disk space fills up quickly. Is there any plugin that's compatible with Pipeline that I can wipe out the workspace on a successful build? 回答1: You can use deleteDir() as the last step of the pipeline Jenkinsfile (assuming you didn't change the working directory). 回答2: Like @gotgenes pointed out with Jenkins Version. 2.74 , the below works, not sure since when, maybe if some one can

Is it possible to capture the stdout from the sh DSL command in the pipeline

↘锁芯ラ 提交于 2019-11-26 18:48:40
For example: var output=sh "echo foo"; echo "output=$output"; I will get: output=0 So, apparently I get the exit code rather than the stdout. Is it possible to capture the stdout into a pipeline variable, such that I could get: output=foo as my result? Andrey Prokopiev Note: The linked Jenkins issue has since been solved. As mention in JENKINS-26133 it was not possible to get shell output as a variable. As a workaround suggested using of writ-read from temporary file. So, your example would have looked like: sh "echo foo > result"; def output=readFile('result').trim() echo "output=$output";

Get absolute path to workspace directory in Jenkins Pipeline plugin

前提是你 提交于 2019-11-26 17:09:17
问题 I'm currently doing some evaluation on the Jenkins Pipeline plugin (formerly know as Workflow plugin). Reading the documentation I found out that I currently cannot retriev the workspace path using env.WORKSPACE : The following variables are currently unavailable inside a workflow script: NODE_LABELS WORKSPACE SCM-specific variables such as SVN_REVISION Is there any other way how to get the absolute path to the current workspace? I need this running some test which in turn gets some parameter

How do I pass variables between stages in a declarative Jenkins pipeline?

落爺英雄遲暮 提交于 2019-11-26 15:47:35
问题 How do I pass variables between stages in a declarative pipeline? In a scripted pipeline, I gather the procedure is to write to a temporary file, then read the file into a variable. How do I do this in a declarative pipeline? E.g. I want to trigger a build of a different job, based on a variable created by a shell action. stage("stage 1") { steps { sh "do_something > var.txt" // I want to get var.txt into VAR } } stage("stage 2") { steps { build job: "job2", parameters[string(name: "var",

Show a Jenkins pipeline stage as failed without failing the whole job

时光总嘲笑我的痴心妄想 提交于 2019-11-26 15:44:43
问题 Here's the code I'm playing with node { stage 'build' echo 'build' stage 'tests' echo 'tests' stage 'end-to-end-tests' def e2e = build job:'end-to-end-tests', propagate: false result = e2e.result if (result.equals("SUCCESS")) { stage 'deploy' build 'deploy' } else { ?????? I want to just fail this stage } } Is there any way for me to mark the 'end-to-end-tests' stage as failed without failing the whole job? Propagate false just always marks the stage as true, which is not what I want, but

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

不想你离开。 提交于 2019-11-26 15:44:27
问题 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? 回答1: 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/syntax/#options 回答2: You got at the disableConcurrentBuilds property: