groovy script in jenkins fails

房东的猫 提交于 2020-01-15 08:58:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!