0

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 ?

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it works when I don't pass-in any argument, but when I try to call the function as gitCheckout('some-sha'), it fails with error No signature of method: gitCheckout.call() is applicable for argument types: (java.lang.String) values, I tried passing gitCheckout('*/master') for testing, still same error.
Check if you name the file correctly. I just noticed that you mentioned vars/gitCheckout.goovy in the question. Maybe that was just a typo in the text. The error suggests that gitCheckout custom vars cannot be found - it can be caused by an incorrect file name. The signature of the call function you shown looks fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.