如何在Jenkins DSL文件中启用Groovy沙箱选项?
问题解决:Jenkins DSL配置Active Choice参数的Groovy沙箱
你遇到的问题是Job DSL里ActiveChoiceGroovyScriptContext没有sandbox()方法,正确的配置方式是在activeChoiceParam或activeChoiceReactiveParam的上下文里使用useSandbox(true),而非在groovyScript块内调用sandbox(true)。
修正后的DSL代码
pipelineJob("mar/common/mysql-sync") { description("Jenkins Job to sync lower env DBs with prod") logRotator{numToKeep(5)} parameters { activeChoiceParam('environment') { description('Select the Environment for the DB you wish to restore') choiceType('SINGLE_SELECT') useSandbox(true) // 在此启用沙箱 groovyScript { script("return['dev','staging']") fallbackScript('return ["ERROR"]') } } activeChoiceReactiveParam('DB_HOST') { description('Select the DB you wish to restore') choiceType('SINGLE_SELECT') useSandbox(true) // 在此启用沙箱 groovyScript { script("if(environment.equals('dev')){return ['mysql-test-dev.cgdrbdgdfgdgjok.eu-west-1.rds.amazonaws.com']} else {return ['mysql-test-staging.cgdrfgefredhjok.eu-west-1.rds.amazonaws.com']}") fallbackScript('return ["ERROR"]') } referencedParameter('environment') } } definition { cpsScm { scm { git { remote { url('git@github.com:allen/jenkins-jobs.git') credentials('github-jenkins-ssh') } branches('master') extensions { cleanBeforeCheckout() } } } scriptPath("jenkins-pipelines/custom/s3-to-mysql-restore.groovy") } } }
关键说明
useSandbox(true)直接在activeChoiceParam/activeChoiceReactiveParam的闭包中配置,作用于该参数的所有Groovy脚本(主脚本和fallback脚本)。- 原代码中在
groovyScript块内调用的sandbox(true)不属于Job DSL的API规范,因此会抛出方法不存在的异常。
内容的提问来源于stack exchange,提问作者Salman Ahsan
相关产品推荐
相关产品推荐

