从外部Groovy文件加载jenkins参数

时间:2019-03-29 15:48:24

标签: jenkins groovy jenkins-pipeline

我有一个很大的Jenkinsfile,我想在其他项目中重复使用,但是每个项目有不同的参数,因此我尝试每个项目只有一个文件,其中只包含这样的参数:

Jenkinsfile

node {

    checkout scm
    def options = []
    def optionsBuilder = load pwd() + '/global-scripts/optionsBuilder.groovy'
    options.addAll(optionsBuilder.buildOptions(env.JOB_BASE_NAME))


    properties { options }
}

global-scripts / optionsBuilder.groovy

def buildOptions(jobName) {
    echo "load " + pwd() + "/project-scripts/" + jobName + ".groovy"
    def jobOptionsBuilder = load pwd() + "/project-scripts/" + jobName + ".groovy"
    return jobOptionsBuilder.buildOptions()
}
return this

project-scripts / job.groovy

def buildOptions() {
    def options = [buildDiscarder(logRotator(numToKeepStr: '5')),
                   parameters([string(name: 'releaseVersion', defaultValue: env.releaseVersion, description: 'Version that needs to be released'),
                               string(name: 'nextVersion', defaultValue: env.nextVersion, description: 'Next snapshot version' ),
                               string(name: 'branch', defaultValue: env.branch, description: 'Branch that needs to be released'),
                               booleanParam(name: 'sendRocketChatNotification', defaultValue: true, description: 'Send notification to Rocket_Chat'),
                               booleanParam(name: 'sendEmail', defaultValue: true, description: 'Send an email with changelog'),
                               booleanParam(name: 'dryRun', defaultValue: false, description: 'No git push and no mvn deploy')])]


    return options
}
return this

但是似乎我找不到正确的语法..詹金斯向我抛出此错误:

java.lang.ClassCastException: org.jenkinsci.plugins.workflow.multibranch.JobPropertyStep.properties expects java.util.List<hudson.model.JobProperty> but received class org.jenkinsci.plugins.workflow.cps.CpsClosure2
    at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:394)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:318)

1 个答案:

答案 0 :(得分:2)

我的印象是,在您的Jenkinsfile中,您应该只写

    properties(options)