简体   繁体   English

使用 groovy 脚本的 Jenkins 参数

[英]Jenkins parameters using groovy script

UPDATE更新

I have a simple pipeline where I want to receive in parameters multiple choices from a file.我有一个简单的管道,我想从一个文件中接收多个参数的选择。 In my file I have在我的文件中,我有

@Test1,Accounts
@Test2,Services
@Test3,Accesses

and I want to have all of "@Test1", "@Test2" and "@Test3" in checkboxes as parameters so I would run only the tests selected.并且我希望复选框中的所有“@Test1”、“@Test2”和“@Test3”作为参数,所以我只会运行选定的测试。 But I'm not understanding what I'm doing wrong.但我不明白我做错了什么。 Pipeline管道

def code = """tests = getChoices()
return tests
def getChoices() {
    def filecontent = readFile "/var/jenkins_home/test.txt"
    def stringList = []
    for (line in filecontent.readLines()) {stringList.add(line.split(",")[0].toString())}
    List modifiedList = stringList.collect{'"' + it + '"'}
    return modifiedList
}""".stripIndent()

properties([
        parameters([
                [$class              : 'CascadeChoiceParameter',
                 choiceType          : 'PT_CHECKBOX',
                 description         : 'Select a choice',
                 filterLength        : 1,
                 filterable          : false,
                 name                : 'choice1',
                 referencedParameters: 'role',
                 script              : [$class        : 'GroovyScript',
                                        fallbackScript: [
                                                classpath: [],
                                                sandbox  : true,
                                                script   : 'return ["ERROR"]'
                                        ],
                                        script        : [
                                                classpath: [],
                                                sandbox  : true,
                                                script   : code
                                        ]
                 ]
                ]
        ])
])

pipeline {
    agent {
        docker { image 'node:latest' }
    }

    stages {
        stage('Tags') {
            steps {
                getChoices()
            }
        }
    }
}

def getChoices() {
    def filecontent = readFile "/var/jenkins_home/test.txt"
    def stringList = []

    for (line in filecontent.readLines()) {
        stringList.add(line.split(',')[0].toString())
    }
    List modifiedList = stringList.collect { '"' + it + '"' }
    echo "$modifiedList"
    return modifiedList
}

With this approach I know I can use multi-select checkboxes because I tried to substitute通过这种方法,我知道我可以使用多选复选框,因为我试图替换

def code = """ tests = ["Test1", "Test2", "Test3"]
return tests""".stripIndent()

and I get the output that I wanted.我得到了我想要的输出。

But when I run my pipeline I get build SUCCESS but always get fallbackScript in my Build parameters checkbox.但是当我运行我的管道时,我得到了构建成功,但总是在我的构建参数复选框中得到 fallbackScript。 Can anyone help me out understand what is causing fallbackScript to run always?任何人都可以帮助我了解导致 fallbackScript 始终运行的原因吗? Thanks :)谢谢 :)

If you want to auto-populate build parameters you have to return a list of parameters from your function.如果要自动填充构建参数,则必须从函数中返回参数列表。 When you execute the pipeline the build with parameters will be populated.当您执行管道时,将填充带有参数的构建。 Note in this was only from the second execution of the pipeline the new parameters will be available.请注意,这只是从管道的第二次执行开始,新参数才可用。 Refer following.参考以下。

pipeline {
    agent any
    parameters{
            choice(name: 'TESTES', choices: tests() , description: 'example')
    }

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}


def tests() {
    return ["Test01", "Test2", "Test4"]
}

If you want to get user input each time you execute a build you should move your choice parameter into a stage.如果您想在每次执行构建时获取用户输入,您应该将您的选择参数移动到一个阶段。 Please refer to the following.请参考以下内容。

pipeline {
    agent any

    stages {
        stage('Get Parameters') {
            steps {
                script{
                def choice = input message: 'Please select', ok: 'Next',
                             parameters: [choice(name: 'PRODUCT', choices: tests(), description: 'Please select the test')]
                    echo '$choice'
                }
            }
        }
    }
}


def tests() {
    return ["Test01", "Test2", "Test4"]
}

Update 02更新 02

Following is how to read from a file and dynamically create the choice list.以下是如何从文件中读取并动态创建选择列表。

pipeline {
    agent any

    stages {
        stage('Get Parameters') {
            steps {
                script{
                        sh''' 
                        echo "@Test1,Accounts" >> test.txt
                        echo "@Test2,Services" >> test.txt
                        '''
                def choice = input message: 'Please select', ok: 'Next',
                                parameters: [choice(name: 'PRODUCT', choices: getChoices(), description: 'Please select the test')]
                }
            }
        }
    }
}


def getChoices() {
    def filecontent = readFile "test.txt"
    def choices = []
    for(line in filecontent.readLines()) {
        echo "$line"
        choices.add(line.split(',')[0].split('@')[1])
        
    }
    return choices
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM