问题
I have a groovy script which works fine for all jenkins job but fails for one jenkin jobs. It works fine in Jenkins scriptler but does not work when I create the job dsl in groovy.
parameters {
activeChoiceParam('BRANCH') {
description('Select the branch to build')
choiceType('SINGLE_SELECT')
groovyScript {
script("""
import jenkins.*
import jenkins.model.*
import hudson.*
credentialsId = 'bbfe8cd19'
def github_token = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class, Jenkins.instance, null, null ).find{
it.id == credentialsId}
def inputFile = new File("/tmp/android_chat_new_branch_list")
inputFile.write("curl --silent -X GET -H 'Authorization: token '${github_token.password}'' 'https://api.github.com/repos/org/repo/branches' | grep name | cut -c 14- | rev | cut -c 3- | rev")
""")
}
}
fails with error
Caused by: groovy.lang.MissingPropertyException: No such property: github_token for class: javaposse.jobdsl.dsl.helpers.parameter.ActiveChoiceGroovyScriptContext
回答1:
When using triple quotes, the GString replacement still works. So writing ${...}
in such a string will replace the variable at once. But you want to delay this until the script is run. So you have to quote the dollar sign. e.g.
...
inputFile.write("curl ... -H 'Authorization: token \${github_token.password}'...")
...
Or use triple single quotes (if you don't want any replacements for that string at all)
来源:https://stackoverflow.com/questions/59718803/groovy-script-in-jenkins-fails