PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, December 13, 2022

[FIXED] How to use parameter to skip a stage of Jenkins declarative pipeline?

 December 13, 2022     jenkins-pipeline, syntax     No comments   

Issue

I'm trying to create a Jenkinsfile that runs a release build step only if a boolean parameter isRelease is true, otherwise skip this step.

My Jenkinsfile looks like this (extract):

pipeline {
    agent { label 'build' }
    tools {
        jdk 'OpenJDK 11'
    }
    parameters {
        booleanParam( name: 'isRelease', description: 'Run a release build?', defaultValue: false)
    }
    
    stages {
        stage('Build') {
            steps {
                sh 'mvn clean compile'
           }
        }
        stage('Test') {
           steps {
                sh 'mvn test'
           }
        }
        stage('Release') {
            when {
                beforeAgent true
                expression { return isRelease }
            }
            steps {
                sh 'echo "######### Seems to a release!"'
            }
        }
    }       
}

However, I don't seem to understand how to use the parameters variable properly. What happens is that the release step is always executed.

I changed expression { return isRelease } to expression { return "${params.isRelease}" } which did not make a difference. Changing it to expression { return ${params.isRelease} } causes the step to fail with java.lang.NoSuchMethodError: No such DSL method '$' found among steps.

What's the right way to use a parameter to skip a step?


Solution

You were closest on your first attempt. The latter two failed because:

  • Converting a Boolean to a String would always return truthiness because a non-empty String is truthy.
  • That is invalid Jenkins Pipeline or Groovy syntax.

The issue here with the first attempt is that you need to access isRelease from within the params object.

when {
  beforeAgent true
  expression { params.isRelease }
}

The when directive documentation actually has a very similar example in the expression subsection for reference.



Answered By - Matt Schuchard
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing