【问题标题】:Active choices parameter主动选择参数
【发布时间】:2017-11-07 00:21:57
【问题描述】:

我的 Jenkins 中安装了活动选择参数。在这里,我必须从本地存储的文本文件中选择一个值,然后将其作为多选选项显示给用户。 例如,我的文本文件可以有一个条目 1 - abc,2 - def。用户必须看到这两个条目,并且可以选择其中一个或两个。此文本文件是手动维护的。我可以有第三个条目 3 - ghi,当我触发 Jenkins 作业时,我应该会看到所有 3 个条目。

谁能帮帮我?

提前谢谢你, 罗希特

【问题讨论】:

    标签: jenkins jenkins-plugins


    【解决方案1】:

    试试这个:

    // create blank array/list to hold choice options for this parameter and also define any other variables.
    def list = []
    def file = "/opt/data/jenkins/jobs/dummy_dummy/workspace/somefile.txt"
    
    // create a file handle named as textfile 
    File textfile= new File(file) 
    
    // now read each line from the file (using the file handle we created above)
    textfile.eachLine { line -> 
            //add the entry to a list variable which we'll return at the end. 
            //The following will take care of any values which will have 
            //multiple '-' characters in the VALUE part 
        list.add(line.split('-')[1..-1].join(',').replaceAll(',','-'))
    }
    
    //Just fyi - return will work here, print/println will not work inside active choice groovy script / scriptler script for giving mychoice parameter the available options.
    return list
    

    【讨论】:

    【解决方案2】:

    您可以使用 groovy 脚本来解析文件并返回选项,例如:

    def list = []
    File textfile= new File("path-to-file") 
    textfile.eachline { line -> 
        //assuming you want only text values without a number
        list.add(line.split('-')[1]) }
    return list
    

    同时为多个值选择“选择类型”。

    【讨论】:

    • 谢谢伊戈尔。我已经添加了您提供的代码。但是当我选择 Build with parameters 时,理想情况下我应该看到 abc、def、ghi 等,但我什么也没看到...
    • @RohitSavanurkar 你检查你的脚本了吗?您可以在 http:// 中运行它,看看它是否从文件中返回这些值。
    • @RohitSavanurkar 我要说的是,您应该首先检查脚本是否正确。请在 Jenkins 脚本控制台中运行它并粘贴输出
    • 你好@Igor。不确定如何在 Jenkins 脚本中运行。你能告诉我我该怎么做吗?我的意思是运行 Jenkins 脚本。
    • @RohitSavanurkar 正如我提到的,转到your_jenkins_url/script,将你的脚本粘贴到那里,看看输出是什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多