简体   繁体   English

活动选择参数中的通用 Groovy 脚本

[英]Common Groovy script in active choices parameter

I have a groovy script that will be common to many jobs - they will all contain an Active Choices Reactive Parameter.我有一个 groovy 脚本,它对许多工作都是通用的 - 它们都将包含一个 Active Choices Reactive 参数。 Rather than repeat the same script dozens of times I would like to place in a (library | ??) one time, and reference it in each job.而不是重复相同的脚本几十次,我想把它放在(库| ??)一次,并在每个工作中引用它。

The script works beautifully for any job I paste it in. Just need to know if it is possible to plop it into one place and share across all jobs.该脚本适用于我粘贴的任何工作。只需要知道是否可以将它放到一个地方并在所有工作中共享。 Update it once, updates all jobs.更新一次,更新所有作业。

import jenkins.model.Jenkins;

ArrayList<String> res = new ArrayList<String>();
def requiredLabels = [new hudson.model.labels.LabelAtom ("Product")];
requiredLabels.add(new hudson.model.labels.LabelAtom(ClientName));

Jenkins.instance.computers.each {
        if (it.assignedLabels.containsAll(requiredLabels)) {
            res.add(it.displayName);    
        }
}

return res;

Nope, looks like it isn't possible (yet).不,看起来不可能(还)。

https://issues.jenkins-ci.org/browse/JENKINS-46394 https://issues.jenkins-ci.org/browse/JENKINS-46394

CAVEAT: This will work only if you have access to your Jenkins box.警告:这仅在您有权访问 Jenkins 盒子时才有效。 I haven't tried to do it by adding paths to the jenkins home我没有尝试通过添加 jenkins 家的路径来做到这一点

You can use this:你可以使用这个:

  1. Make all your functions into a groovy file.将您的所有函数都放入一个 groovy 文件中。 For example will call it: activeChoiceParams.groovy例如会调用它:activeChoiceParams.groovy
  2. Convert that file into a jar by: jar cvf <jar filename> <groovy file> .通过以下方式将该文件转换为 jar: jar cvf <jar filename> <groovy file> For example: jar cvf activeChoiceParams.jar activeChoiceParams.groovy例如: jar cvf activeChoiceParams.jar activeChoiceParams.groovy
  3. Move your jar file to /packages/lib/ext将您的 jar 文件移动到 /packages/lib/ext
  4. Restart Jenkins重启詹金斯
  5. In your active choices groovy script use (for example>:在您的活动选择 groovy 脚本中使用(例如>:
import activeChoiceParams

return <function name>()

All functions must return a list or a map所有函数必须返回一个列表或地图

The option we decide on was to have a common parameters function .groovy we store in git.我们决定的选项是在 git 中存储一个通用参数函数 .groovy。 There is a service hook that pushes the files out to a known network location on check-in.有一个服务挂钩可在签入时将文件推送到已知的网络位置。 In our Jenkins build step we then have the control dynamically load up the script and invoke the function passing in any parameters.在我们的 Jenkins 构建步骤中,我们让控件动态加载脚本并调用传递任何参数的函数。

ArrayList<String> res = new ArrayList<String>();

try {
new GroovyShell().parse( new File( '\\\\server\\share\\folder\\parameterFunctions.groovy' ) ).with {
    res = getEnvironments(ClientName);
}
} catch (Exception ex) {
  res.add(ex.getMessage());
}

return res;

And our parameterFunctions.groovy will respond how we want:我们的 parameterFunctions.groovy 将响应我们想要的方式:

public ArrayList<String> getEnvironments(String p_clientName) {

    ArrayList<String> res = new ArrayList<String>();

    if (!(p_clientName?.trim())){
        res.add("Select a client");
        return res;
    }

    def possibleEnvironments = yyz.getEnvironmentTypeEnum();

    def requiredLabels = [new hudson.model.labels.LabelAtom ("PRODUCT")];
    requiredLabels.add(new hudson.model.labels.LabelAtom(p_clientName.toUpperCase()));


    Jenkins.instance.computers.each { node ->
        if (node.assignedLabels.containsAll(requiredLabels)) {
                // Yes.  Let's get the environment name out of it.
                node.assignedLabels.any { al ->
                    def e = yyz.getEnvironmentFromString(al.getName(), true);
                    if (e != null) {
                        res.add(al.getName());
                        return; // this is a continue
                    }
                }
            }
        }


    return res;
}

I found interesting solution by using Job DSL plugin.我通过使用 Job DSL 插件找到了有趣的解决方案。

usually job definition for Active Choices is look like: Active Choices 的工作定义通常如下所示:

from https://jenkinsci.github.io/job-dsl-plugin/#method/javaposse.jobdsl.dsl.helpers.BuildParametersContext.activeChoiceParam来自https://jenkinsci.github.io/job-dsl-plugin/#method/javaposse.jobdsl.dsl.helpers.BuildParametersContext.activeChoiceParam

  job('example') {
    parameters {
        activeChoiceParam('CHOICE-1') {
            choiceType('SINGLE_SELECT')
            groovyScript {
                script(readFileFromWorkspace('className.groovy') + "\n" + readFileFromWorkspace('executionPart.groovy'))
            }
        }
     }
  }
  1. in className.groovy you can define class as a common part在 className.groovy 中,您可以将类定义为公共部分
  2. with executionPart.groovy you can create instance and make your particular part使用 executionPart.groovy,您可以创建实例并制作您的特定零件

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

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