Why is this condition in Jenkins pipeline not working? - jenkins

I am getting mad about the following pipeline code, as it does not do what I expect. Does anyone have an idea what I am doing wrong?
Here is my pipeline code to define the parameter
pipeline {
agent any
parameters { booleanParam(name: 'bForceCheckout', defaultValue: false, description: '') }
...
And here the stage itself
stage('SVN Checkout') {
// Get code from SVN repository
steps {
script {
// If project is not yet checked out, setup checkout structure, i.e. which
// folders will be checked out and which will not be checked out
retry (5) {
try {
def svnInfoError = bat (returnStatus: true, script: "svn info ${projectName}")
// bForceCheckout has to be set as parameter in the job
println "---> " + ((svnInfoError != 0) || bForceCheckout)
if ((svnInfoError != 0) || bForceCheckout) {
println "svnInfoError: " + svnInfoError
println "bForceCheckout: " + bForceCheckout
timeout(activity: true, time: 90, unit: 'MINUTES') {
// Clean up
deleteDir ()
... some SVN related stuff ...
}
}
} catch (Exception ex) {
// Clean up
deleteDir ()
println(ex.toString());
error ("SVN checkout: directory structure could not be set up")
}
}
}
}
}
And here is the console output. As you can see, svnInfoError and bForceCheckout are 0 / false, but the part in the if condition still get executed...
12:51:22
[Pipeline] echo
12:51:22 --->false
[Pipeline] echo
12:51:23 svnInfoError: 0
[Pipeline] echo
12:51:23 bForceCheckout: false
[Pipeline] timeout
12:51:23 Timeout set to expire after 1 hr 30 min without activity
[Pipeline] {
[Pipeline] deleteDir
[Pipeline] bat
12:55:21

Thank you guys, it did turn out to be an issue of the data type.
I also now found the following question leading to the same answer: booleanParam in jenkins dsl
Defining a parameter as follows will still give you a variable of type string.
parameters { booleanParam(name: 'bForceCheckout', defaultValue: false, description: '') }
Checking the data type:
println bForceCheckout.getClass()
...will give you
09:06:03 class java.lang.String
In my opinion this is a bug, so I created a Jenkins issue: https://issues.jenkins-ci.org/browse/JENKINS-57499
For now I ended up changing the code above to the following (yes, I will do some renaming as the variable does not hold a boolean value), and now it works:
if ((svnInfoError != 0) || bForceCheckout.toBoolean()) {
EDIT: I found that actually you should use the following syntax to access parameters and this is working as expected:
if ((svnInfoError != 0) || params.bForceCheckout) {

Related

Jenkins Pipeline failure reason is at the end of console instead at the step where it occurred

I'm creating a multi-branch pipeline with a Jenkinsfile and if one step fails I'd like to show the reason in the email that is sent in the post step.
I can fetch the log just fine but my problem is that the failure reason is printed at the end of the console log instead of at the point where it occurred.
If I create a freestyle project then the error messages will always be in the console log where they occurred and the email works as desired.
My Jenkinsfile looks something like this:
pipeline {
agent any
parameters {
string(name: 'VERSION', defaultValue: '', description: 'version number to build')
}
stages {
stage("Check parameters") {
steps {
script {
if (VERSION == null || VERSION == '') {
error("Build failed because required parameter VERSION not set")
}
}
}
}
stage("Do something") {
...
}
stage("Rename successful build") {
steps {
script {
currentBuild.displayName = "v${VERSION}"
currentBuild.description = "Successful build for v${VERSION}"
}
}
}
}
post {
failure {
withFolderProperties {
echo "Recipients: ${env.MAIL_RECIPIENTS}"
script {
if (env.MAIL_RECIPIENTS) {
emailext (
to: env.MAIL_RECIPIENTS,
replyTo: '$DEFAULT_REPLYTO',
subject: '$DEFAULT_SUBJECT',
body: '$DEFAULT_CONTENT',
mimeType: 'text/html'
);
}
}
}
}
}
}
This will generate the following console log if I start the build without a version parameter.
This is just an example and the same persists if another stage fails at some point (I cleaned some of the [Pipeline] lines):
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Check parameters)
[Pipeline] script
[Pipeline] {
[Pipeline] error
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (build)
Stage "Do Something" skipped due to earlier failure(s)
...
Stage "Rename successful build" skipped due to earlier failure(s)
...
Sending email to: xxx
...
ERROR: Build failed because required parameter VERSION not set
Finished: FAILURE
The last two lines are missing in the email because it is sent before they are printed; however, why is the ERROR printed at the end of the log instead of at the step where it occurred? Is there a way where i can configure this?
I'm expecting that the error is printed before the next step is shown in the log.
Your expectation can be easily achieved with Jenkins scripted pipeline but a little difficult with Jenkins declarative pipeline.
but In the Declarative pipeline, If you are raising an Error based on some condition you know what could be the error at the end of the build, so while you raising the error you could grab the error string in a variable and make use of it in the failure stage.
In Scripted Pipeline, it would be something like this
def isItFail = false
def failureReason = ""
node {
try {
stage('check parameter') {
if (VERSION == null || VERSION == '') {
throw new RuntimeException("Version number should not be empty")
}
}
stage('some other stage') {
.....
}
}catch(Exception e ) {
isItFail = true
failureReason = e.getMessage()
}finally {
if(isItFail) {
echo "Send An Email with this Text with Reason: ${failureReason}"
}
}
}
In Declarative Pipeline, it would be
def failReason = ""
pipeline {
agent any;
stages {
stage('check version') {
steps {
script {
script {
if (VERSION == null || VERSION == '') {
failReason = "Build failed because required parameter VERSION not set"
error(failReason)
}
}
}
}
}
stage('other stage') {
....
}
}
post {
failure {
echo "Send An Email with Reason : ${failReason}"
}
}
}
In the declarative pipeline, there is no any catch{} DSL and you can't use try..catch..finally outside of script{} DSL, hence it little hard to bring the failure reason text to the end of the pipeline

Expression depeding on Jenkins build boolean parameter doesn't work in pipeline

I've inherited some Jenkins pipeline and try to improve it. Jenkins and groovy is quite fresh topic for me, so most probably I'm doing something wrong.
I'm using Jenkins ver. 2.121.3
Main aim was to add build parameter to do some extra cleaning during build. So I've added parameter CLEAN_FIRST with Boolean type and default value false to a job configuration and did something like this in pipeline:
// CLEAN_FIRST = false
// def prefix = CLEAN_FIRST ? "" : "REM"
pipeline {
agent none
stages {
stage('Some step') {
steps {
script {
node('master') {
cleanWs()
try {
def prefix = CLEAN_FIRST ? "" : "REM"
echo "CLEAN_FIRST=$CLEAN_FIRST prefix=$prefix"
bat (label: 'build third party',
script: """
$prefix call cleanSomthing.bat
call doOtherStuff.bat
"""
} finally {
echo "some stuff"
}
} // node
} // script
} // steps
} // stage
} // stages
} // pipeline
Now this doesn't work as expected. "REM" prefix is not added.
Echo prints:
CLEAN_FIRST=false prefix=
And bat invokes cleanSomthing.bat which I wish to avoid (to save on build times).
I've tried to make prefix global, but with same result.
Most probably this is caused by some evaluation order or scoping issue, but I can't put finger on it.
Can someone give me a clue why it doesn't work? How to fix it?
Answered own question. Is this problem fixed on some version of Jenkins?
replace
def prefix = CLEAN_FIRST ? "" : "REM"
with
def prefix = params.CLEAN_FIRST ? "" : "REM"
Ok I've found source of problems. It is a bit funny.
When running this pipeline (tested on Mac machine since it had empty job queue):
pipeline {
agent none
stages {
stage('Some step') {
steps {
script {
node('Mac') {
cleanWs()
try {
def logic = true
def prefix = CLEAN_FIRST ? "Ole" : "REM"
def typeLogic = logic.getClass()
def typeParam = CLEAN_FIRST.getClass()
echo "typeLogic=$typeLogic typeParam=$typeParam"
echo "CLEAN_FIRST=$CLEAN_FIRST prefix=$prefix"
sh (script: """
echo prefix=$prefix
""")
} finally {
echo "some stuff"
}
} // node
} // script
} // steps
} // stage
} // stages
} // pipeline
I've got this outcome:
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] stage
[Pipeline] { (Some step)
[Pipeline] script
[Pipeline] {
[Pipeline] node
Running on master in /Users/builder/jenkins/workspace/EIbuild_MacOS
[Pipeline] {
[Pipeline] cleanWs
[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done
[Pipeline] echo
typeLogic=class java.lang.Boolean typeParam=class java.lang.String
[Pipeline] echo
CLEAN_FIRST=false prefix=Ole
[Pipeline] sh
[EIbuild_MacOS] Running shell script
+ echo prefix=Ole
prefix=Ole
[Pipeline] echo
some stuff
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
Finished: SUCCESS
So now source the problem is obvious.
Jenkins in configuration promises variable of type Boolean, but in fact provides type String with values are "true" or "false" which are always evaluated as true when used as condition since both values are not empty strings :).

Jenkins: For each loop issue

I am using Jenkins to trigger an Ansible playbook. I would like to pass in an array of computer names in order for the Ansible playbook to trigger the playbook and image the machines one by one.
In order to do this I believe I need a foreach loop.
I have little skill in groovy/Jenkins and have ran into an issue.
error = "Expected a symbol # line..." The line that this is referring to is HOSTS.each {item ->
Can someone please assist me? My script is below (I have edited out some private data)
pipeline {
agent any
// every day
triggers {
cron('H 7 * * *')
}
environment {
HOSTS = ['node1','node2']
}
stages {
stage('MachineDB Scheduler') {
steps {
HOSTS.each { item -> // review
HOSTNAME = ${item} // review
ansibuildPlaybookperf(
sshUser: env.USER,
vaultUser: env.USER,
server: "$SERVER",
dir: "$HOMEDIR/$BUILD_TAG",
playbook: "$PLAYBOOK",
extras: "--vault-password-file passmgr.sh",
extraVars: "$VARS_JENKINS"
)
}
}
}
}
}
I don't really know a lot about ansible but maybe this helps.
This pipeline shows a list of PC's. In the declarative pipeline I call a groovy function which is defined after the pipeline. In this function I go through the list and past every PC-name.
def list = [
'PCNAME1',
'PCNAME2',
'PCNAME3'
]
pipeline {
agent any
stages {
stage('Loop through PCs') {
steps {
loopPC(list)
}
}
}
}
def loopPC(list){
list.each {
println "Computer ${it}"
}
}
OUTPUT:
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Loop through PCs)
[Pipeline] echo
Computer PCNAME1
[Pipeline] echo
Computer PCNAME2
[Pipeline] echo
Computer PCNAME3
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
You can also use a script block in your declarative pipeline to execute the script immediately. It's less clean but maybe easier in the beginning and to make it work (and closer to your attempt):
def list = [
'PCNAME1',
'PCNAME2',
'PCNAME3'
]
pipeline {
agent any
stages {
stage('Loop through PCs') {
steps {
script {
list.each {
println "Computer ${it}"
}
}
}
}
}
}

Abort current build from pipeline in Jenkins

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) {
currentBuild.result = 'ABORTED'
error('Stopping early…')
}
In the Stage View, this will show that the build stopped at this stage, but the build overall will be marked as aborted, rather than failed (see the grey icon for build #9):
After some testing I came up with the following solution:
def autoCancelled = false
try {
stage('checkout') {
...
if (your condition) {
autoCancelled = true
error('Aborting the build to prevent a loop.')
}
}
} catch (e) {
if (autoCancelled) {
currentBuild.result = 'ABORTED'
echo('Skipping mail notification')
// return here instead of throwing error to keep the build "green"
return
}
// normal error handling
throw e
}
This will result into following stage view:
failed stage
If you don't like the failed stage, you have to use return. But be aware you have to skip each stage or wrapper.
def autoCancelled = false
try {
stage('checkout') {
...
if (your condition) {
autoCancelled = true
return
}
}
if (autoCancelled) {
error('Aborting the build to prevent a loop.')
// return would be also possible but you have to be sure to quit all stages and wrapper properly
// return
}
} catch (e) {
if (autoCancelled) {
currentBuild.result = 'ABORTED'
echo('Skipping mail notification')
// return here instead of throwing error to keep the build "green"
return
}
// normal error handling
throw e
}
The result:
custom error as indicator
You can also use a custom message instead of a local variable:
final autoCancelledError = 'autoCancelled'
try {
stage('checkout') {
...
if (your condition) {
echo('Aborting the build to prevent a loop.')
error(autoCancelledError)
}
}
} catch (e) {
if (e.message == autoCancelledError) {
currentBuild.result = 'ABORTED'
echo('Skipping mail notification')
// return here instead of throwing error to keep the build "green"
return
}
// normal error handling
throw e
}
Following this documentation from Jenkins, you should be able to generate an error to stop the build and set the build result like this:
currentBuild.result = 'ABORTED'
Hope that helps.
The thing that we use is:
try {
input 'Do you want to abort?'
} catch (Exception err) {
currentBuild.result = 'ABORTED';
return;
}
The "return" at the end makes sure that no further code is executed.
I handled in a declarative way as shown below:
Based on catchError block it will execute post block.
If post result falls under failure category, the error block will be executed to stop upcoming stages like Production, PreProd etc.
pipeline {
agent any
stages {
stage('Build') {
steps {
catchError {
sh '/bin/bash path/To/Filename.sh'
}
}
post {
success {
echo 'Build stage successful'
}
failure {
echo 'Compile stage failed'
error('Build is aborted due to failure of build stage')
}
}
}
stage('Production') {
steps {
sh '/bin/bash path/To/Filename.sh'
}
}
}
}
Inspired by all the answers I have put all the stuff together into one Scripted Pipeline. Keep in mind this is not a Declarative Pipeline.
To get this example working you will need:
QuickFIX form this answer Jenkins CI Pipeline Scripts not permitted to use method groovy.lang.GroovyObject
discord notifier plugin - https://plugins.jenkins.io/discord-notifier/
Discord channel webhook url filled in the code
The idea I had was to abort the pipeline if it is "replayed" vs started by "run button"(in branches tab of Jenskins BlueOcean):
def isBuildAReplay() {
// https://stackoverflow.com/questions/51555910/how-to-know-inside-jenkinsfile-script-that-current-build-is-an-replay/52302879#52302879
def replyClassName = "org.jenkinsci.plugins.workflow.cps.replay.ReplayCause"
currentBuild.rawBuild.getCauses().any{ cause -> cause.toString().contains(replyClassName) }
}
node {
try {
stage('check replay') {
if (isBuildAReplay()) {
currentBuild.result = 'ABORTED'
error 'Biuld REPLAYED going to EXIT (please use RUN button)'
} else {
echo 'NOT replay'
}
}
stage('simple stage') {
echo 'hello from simple stage'
}
stage('error stage') {
//error 'hello from simple error'
}
stage('unstable stage') {
unstable 'hello from simple unstable'
}
stage('Notify sucess') {
//Handle SUCCESS|UNSTABLE
discordSend(description: "${currentBuild.currentResult}: Job ${env.JOB_NAME} \nBuild: ${env.BUILD_NUMBER} \nMore info at: \n${env.BUILD_URL}", footer: 'No-Code', unstable: true, link: env.BUILD_URL, result: "${currentBuild.currentResult}", title: "${JOB_NAME} << CLICK", webhookURL: 'https://discordapp.com/api/webhooks/')
}
} catch (e) {
echo 'This will run only if failed'
if(currentBuild.result == 'ABORTED'){
//Handle ABORTED
discordSend(description: "${currentBuild.currentResult}: Job ${env.JOB_NAME} \nBuild: ${env.BUILD_NUMBER} \nMore info at: \n${env.BUILD_URL}\n\nERROR.toString():\n"+e.toString()+"\nERROR.printStackTrace():\n"+e.printStackTrace()+" ", footer: 'No-Code', unstable: true, link: env.BUILD_URL, result: "ABORTED", title: "${JOB_NAME} << CLICK", webhookURL: 'https://discordapp.com/api/webhooks/')
throw e
}else{
//Handle FAILURE
discordSend(description: "${currentBuild.currentResult}: Job ${env.JOB_NAME} \nBuild: ${env.BUILD_NUMBER} \nMore info at: \n${env.BUILD_URL}\n\nERROR.toString():\n"+e.toString()+"\nERROR.printStackTrace():\n"+e.printStackTrace()+" ", footer: 'No-Code', link: env.BUILD_URL, result: "FAILURE", title: "${JOB_NAME} << CLICK", webhookURL: 'https://discordapp.com/api/webhooks/')
throw e
}
} finally {
echo 'I will always say Hello again!'
}
}
Main trick was the order of lines to achive abort state:
currentBuild.result = 'ABORTED'
error 'Biuld REPLAYED going to EXIT (please use RUN button)'
First set the state then throw an exception.
In the catch block both work:
currentBuild.result
currentBuild.currentResult
If you're able to approve the constructor for FlowInterruptedException, then you can do the following:
throw new FlowInterruptedException(Result.ABORTED, new UserInterruption(getCurrentUserId()))
You can add to your shared library repo a file var/abortError.groovy:
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
import jenkins.model.CauseOfInterruption.UserInterruption
def call(message)
{
currentBuild.displayName = "#${env.BUILD_NUMBER} $message"
echo message
currentBuild.result = 'ABORTED'
throw new FlowInterruptedException(Result.ABORTED, new UserInterruption(env.BUILD_USER_ID))
}
Then you can use it this way (after importing library):
abortError("some message")
Note that if you se following error in console logs:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new org.jenkinsci.plugins.workflow.steps.FlowInterruptedException hudson.model.Result jenkins.model.CauseOfInterruption[]
You need follow the link form log and approve security exception.
You can go to the script console of Jenkins and run the following to abort a hung / any Jenkins job build/run:
Jenkins .instance.getItemByFullName("JobName")
.getBuildByNumber(JobNumber)
.finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build"));
This is the way to abort the currently running build pipeline in Jenkins UI(in Build History there is a cancel button), for capture:
The Executor.interrupt(Result) method is the cleanest, most direct way I could find to stop a build prematurely and choose the result.
script {
currentBuild.getRawBuild().getExecutor().interrupt(Result.ABORTED)
sleep(1) // Interrupt is not blocking and does not take effect immediately.
}
Pros:
Works in a declarative pipeline just as well as a scripted one.
No try/catch or exceptions to handle.
Marks the calling stage and any successive stages as green/passing in the UI.
Cons:
Requires a number of in-process script approvals, including one that is considered insecure. Approve and use with caution.
Taken from my answer on devops.stackexchange.com.
As for currentBuild, have a look at the docs for the RunWrapper class.

Jenkinsfile Pipeline errors: "expected a symbol" and "undefined section"

Can anyone explain why I get the following errors, and what can be a possible solution for them?
org.codehaus.groovy.control.MultipleCompilationErrorsException:
startup failed: WorkflowScript: 20: Expected a symbol # line 20,
column 4.
environment {
WorkflowScript: 17: Undefined section "error" # line 17, column 1.
pipeline {
The code in the Jenkinsfile is as follows:
#!groovy
def application, manifest, git, environment, artifactory, sonar
fileLoader.withGit('git#<reducted>', 'v1', 'ssh-key-credential-id-number') {
application = fileLoader.load('<reducted>');
manifest = fileLoader.load('<reducted>');
git = fileLoader.load('<reducted>');
environment = fileLoader.load('<reducted>');
}
pipeline {
agent { label 'cf_slave' }
environment {
def projectName = null
def githubOrg = null
def gitCommit = null
}
options {
skipDefaultCheckout()
}
stages {
stage ("Checkout SCM") {
steps {
checkout scm
script {
projectName = git.getGitRepositoryName()
githubOrg = git.getGitOrgName()
gitCommit = manifest.getGitCommit()
}
}
}
stage ("Unit tests") {
steps {
sh "./node_modules/.bin/mocha --reporter mocha-junit-reporter --reporter-options mochaFile=./testResults/results.xml"
junit allowEmptyResults: true, testResults: 'testResults/results.xml'
}
}
//stage ("SonarQube analysis") {
//...
//}
// stage("Simple deploy") {
// steps {
// // Login
// sh "cf api <reducted>"
// sh "cf login -u <reducted> -p <....>"
//
// // Deploy
// sh "cf push"
// }
// }
}
post {
// always {
// }
success {
sh "echo 'Pipeline reached the finish line!'"
// Notify in Slack
slackSend color: 'yellow', message: "Pipeline operation completed successfully. Check <reducted>"
}
failure {
sh "echo 'Pipeline failed'"
// Notify in Slack
slackSend color: 'red', message: "Pipeline operation failed!"
//Clean the execution workspace
//deleteDir()
}
unstable {
sh "echo 'Pipeline unstable :-('"
}
// changed {
// sh "echo 'Pipeline was previously failing but is now successful.'"
// }
}
}
Your Pipeline is mostly fine — adding Scripted Pipeline elements before the Declarative pipeline block is generally not a problem.
However, at the very start, you're defining an variable called environment (and git), which are basically overriding the elements declared by the various Pipeline plugins.
i.e. When you attempt to do pipeline { environment { … } }, the environment is referring to your variable declaration, which causes things to go wrong.
Rename those two variables, and you'll fix the first error message.
To fix the second error message, remove the attempts to declare variables from the environment block — this block is only intended for exporting environment variables for use during the build steps, e.g.:
environment {
FOO = 'bar'
BAR = 'baz'
}
The script block you have will work fine without these declarations. Alternatively, you can move those variable declarations to the top level of your script.
If you're using declarative pipeline (which you are, e.g. the outer pipeline step), then you may only declare the pipeline on the outer layer, e.g. you can't have variable and function definitions. This is the downside of using declarative pipeline.
More info here
As I see it you can solve this the following ways:
Use scripted pipeline instead
Move the code at the beginning to a global pipeline library (Might be tricky to solve variable scoping if a value is used in several places, but it should be doable.
Move the code at the beginning to an script step inside the pipeline and store the values as described here.

Resources