Home > Software engineering >  Converting a gstringimpl to java.lang.string in a Jenkinsile
Converting a gstringimpl to java.lang.string in a Jenkinsile

Time:11-14

I have a pipeline which takes a persistent string parameter input. The pipeline then checks whether the parameter value is present in a list.

The problem is that the persisted string is of type gstringimpl, and the list items are java.lang.string type. When I use the .contains() method, even though the value is in the list, it won't return true, which I believe is due to the different data types.

I've tried everything online, including the toString() method but I can't get it to work. I'm attaching my code below.

String ver = ""
pipeline {
    agent {
        docker{
            image 'registry/abc/builder:0.1.5'
            args '-t -d -v maven-m2-cache:/home/node/.m2'
        }
    }
    parameters {
        persistentString(name: 'Version', defaultValue: '8.4.7.8', description: 'Version to build', successfulOnly: false)
    }
    stages {
        stage('Analyze Parameter'){
            steps{
                script{
                        ver = "${Version}".toString()
                    }
                }
            }
        stage('Build'){
            steps{
                script{
                    def version_list1 = ['8.4.7.8','8.3.7.9','8.5.4.7']                    
                    if (version_list1.contains("${ver}")){
                            println("build version branch")
                    } else {
                        println("${ver}")
                        println("${ver}".getClass())
                        println(version_list1[0])
                        println(version_list1[0].getClass())
                        println("build master branch")                            
                    }

                }
            }
        }
    }
}

The pipeline always goes into the else block and prints the following:

8.4.7.8

class org.codehaus.groovy.runtime.GStringImpl

8.4.7.8

java.lang.string

build master branch

CodePudding user response:

Don't use String interpolation to resolve Parameters. Instead directly access it like params.PARAM_NAME, example below.

script{
      def version_list1 = ['8.4.7.8','8.3.7.9','8.5.4.7']                    
      if (version_list1.contains(params.Version)){
              println("build version branch")
      } else {
          println("build master branch")                            
      }
}
  • Related