I'm having a strange issue which I can't seem to quite understand. I have written a custom step
which accepts parameters used to clone github/bitbucket repositories more easily. The step
works just fine - it calls the appropriate checkout()
for branches
and pr
s, but for some reason this only works if you call it from a script { gitUtils.cloneRepo(...) }
. It doesn't work in a declarative pipeline if you don't wrap it around with a script { }
with a super strange exception:
WorkflowScript: 25: Expected a symbol @ line 25, column 17.
gitUtils().getCredentials(repo)
^
WorkflowScript: 26: Expected a symbol @ line 26, column 17.
gitUtils().cloneRepo(url: repo)
^
WorkflowScript: 27: Expected a symbol @ line 27, column 17.
gitUtils().getRevision()
^
WorkflowScript: 26: Invalid parameter "url", did you mean "message"? @ line 26, column 38.
gitUtils().cloneRepo(url: repo)
^
WorkflowScript: 27: Missing required parameter: "message" @ line 27, column 17.
gitUtils().getRevision()
Any ideas why this is happening?
import java.lang.IllegalArgumentException
def call() {
return this
}
def cloneRepo(Map parameters = [url: null, branch: "master", credentials: null]) {
def url = parameters.getOrDefault("url", null)
def branch = parameters.getOrDefault("branch", "master")
def credentials = parameters.getOrDefault("credentials", null)
script {
if(!url) {
throw new IllegalArgumentException("cloneRepo() expects url argument to be present!")
}
if(credentials == null) {
credentials = getCredentials(url)
}
if (branch.matches("\\d+") || branch.matches("PR-\\d+")) {
if (branch.matches("PR-\\d+")) {
branch = branch.substring(3)
}
checkout changelog: false, poll: false, scm: [
$class: 'GitSCM',
branches: [[name: 'pr/' + branch]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'LocalBranch', localBranch: 'pr/' + branch]],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: credentials,
refspec: 'refs/pull/' + branch + '/head:pr/' + branch,
url: url
]]
]
} else {
checkout changelog: false, poll: false, scm: [
$class: 'GitSCM',
branches: [[name: branch]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: credentials,
url: url
]]
]
}
}
}