rodee
rodee

Reputation: 3151

How to refer to the parameters in groovy pipeline script?

I merged this as vars/gitCheckout.goovy add this as library into the jenkins

def call(String branch = '*/master') {
  checkout([$class: 'GitSCM',
      branches: [[name: ${branch}]],
      doGenerateSubmoduleConfigurations: false,
      extensions: [[$class: 'SubmoduleOption',
                    disableSubmodules: false,
                    parentCredentials: false,
                    recursiveSubmodules: true,
                    reference: '',
                    trackingSubmodules: false]],
      submoduleCfg: [],
      userRemoteConfigs: [[url: 'https://my-server.com/some/project.git']]])
}

Calling this method as below from jenkins Pipeline Script:

@Library('jenkins-library@master') _

pipeline {
  agent { label 'my-server' }
  stages {
    stage('Git Checkout') {
        steps {
        gitCheckout()
        }
  }
  }
}

This fails with error java.lang.NoSuchMethodError: No such DSL method '$' found among steps [ArtifactoryGradleBuild, MavenDescriptorStep, ....

I tried $branch, params.branch, but it didn't work, the code otherwise works if I don't use parameter and hardcode the branch name. Also, whenever I make any update to this .groovy script, should I test it by merging and running it as jenkins job? is there any other way to test before merging the groovy script ?

Upvotes: 0

Views: 1500

Answers (1)

Szymon Stepniak
Szymon Stepniak

Reputation: 42174

Replace ${branch} in the 3rd line with just branch. You use $ with a variable name when you e.g. interpolate variables inside Groovy strings:

def value = "current branch is: ${branch}" // produces: current branch is */master

If you forgot to use $ in string interpolation, nothing would happen:

def value = "current branch is: branch" // produces: current branch is branch

Upvotes: 1

Related Questions