Jenkins 管道多配置项目 [英] Jenkins Pipeline Multiconfiguration Project

查看:15
本文介绍了Jenkins 管道多配置项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Jenkins 有一份运行 ant 脚本的工作.我使用多配置项目"轻松地在多个软件版本上测试了这个 ant 脚本.

I have a job in Jenkins that is running an ant script. I easily managed to test this ant script on more then one software version using a "Multi-configuration project".

这种类型的项目真的很酷,因为它允许我指定我需要的两个软件的所有版本(在我的例子中是 Java 和 Matlab),它会使用我的所有参数组合运行我的 ant 脚本.

This type of project is really cool because it allows me to specify all the versions of the two software that I need (in my case Java and Matlab) an it will run my ant script with all the combinations of my parameters.

然后将这些参数用作字符串,在我的 ant 使用的可执行文件的位置定义中连接起来.

Those parameters are then used as string to be concatenated in the definition of the location of the executable to be used by my ant.

示例:env.MATLAB_EXE=/usr/local/MATLAB/${MATLAB_VERSION}/bin/matlab

example: env.MATLAB_EXE=/usr/local/MATLAB/${MATLAB_VERSION}/bin/matlab

这是完美的工作,但现在我正在将此脚本迁移到它的 pipline 版本.

我设法使用 参数化管道 pluin.有了这个,我可以手动选择如果我手动触发构建,我可以手动选择要使用的软件版本,并且我还找到了一种定期执行此操作的方法,在每次运行时选择我想要的参数.

I managed to implement the same script in a pipeline fashion using the Parametrized pipelines pluin. With this I achieve the point in which I can manually select which version of my software is going to be used if I trigger the build manually and I also found a way to execute this periodically selecting the parameter I want at each run.

这个解决方案看起来还不错,但并不真正令人满意.

This solution seems fairly working however is not really satisfying.

我的多配置项目有一些它没有的功能:

My multi-config project had some feature that this does not:

  1. 我可以设置多个参数来插入它们并执行每个组合
  2. 执行明显分开,在构建历史/构建细节中很容易识别使用了哪些设置
  3. 只需向参数添加一个新的可能"值即可产生所需的执行

请求

所以我想知道是否有更好的解决方案来解决我的问题,也可以满足上述要点.

Request

So I wonder if there is a better solution to my problem that can satisfy also the point above.

长话短说:有没有办法在 jenkins 中实现多配置项目但使用管道技术?

推荐答案

我最近看到很多类似的问题被问到,所以解决这个问题似乎是一个有趣的练习......

I've seen this and similar questions asked a lot lately, so it seemed that it would be a fun exercise to work this out...

在代码中可视化的矩阵/多配置作业实际上只是几个嵌套的 for 循环,每个参数轴一个.

A matrix/multi-config job, visualized in code, would really just be a few nested for loops, one for each axis of parameters.

你可以用一些硬编码的 for 循环来构建一些相当简单的东西来循环几个列表.或者您可以变得更复杂并进行一些递归循环,这样您就不必对特定循环进行硬编码.

You could build something fairly simple with some hard coded for loops to loop over a few lists. Or you can get more complicated and do some recursive looping so you don't have to hard code the specific loops.

免责声明:我做的操作远比写代码的多.我对 groovy 也很陌生,所以这可能可以更干净地完成,并且可能有很多更 groovy 可以完成的事情,但无论如何这都能完成工作.

DISCLAIMER: I do ops much more than I write code. I am also very new to groovy, so this can probably be done more cleanly, and there are probably a lot of groovier things that could be done, but this gets the job done, anyway.

只需做一些工作,这个 matrixBuilder 就可以封装在一个类中,这样您就可以传入一个任务闭包和轴列表并取回任务映射.将其粘贴在共享库中并在任何地方使用.从多重配置作业中添加一些其他功能应该很容易,例如过滤器.

With a little work, this matrixBuilder could be wrapped up in a class so you could pass in a task closure and the axis list and get the task map back. Stick it in a shared library and use it anywhere. It should be pretty easy to add some of the other features from the multiconfiguration jobs, such as filters.

此尝试使用递归 matrixBuilder 函数来处理任意数量的参数轴并构建所有组合.然后它并行执行它们(显然取决于节点可用性).

This attempt uses a recursive matrixBuilder function to work through any number of parameter axes and build all the combinations. Then it executes them in parallel (obviously depending on node availability).

/*
    All the config axes are defined here
    Add as many lists of axes in the axisList as you need.
    All combinations will be built
*/
def axisList = [
    ["ubuntu","rhel","windows","osx"],           //agents
    ["jdk6","jdk7","jdk8"],                      //tools
    ["banana","apple","orange","pineapple"]      //fruit
]



def tasks = [:]
def comboBuilder
def comboEntry = []


def task = {
    // builds and returns the task for each combination

    /* Map the entries back to a more readable format
       the index will correspond to the position of this axis in axisList[] */
    def myAgent = it[0]
    def myJdk   = it[1]
    def myFruit = it[2]

    return {
        // This is where the important work happens for each combination
        node(myAgent) {
            println "Executing combination ${it.join('-')}"
            def javaHome = tool myJdk
            println "Node=${env.NODE_NAME}"
            println "Java=${javaHome}"
        }

        //We won't declare a specific agent this part
        node {
            println "fruit=${myFruit}"
        }
    }
}


/*
    This is where the magic happens
    recursively work through the axisList and build all combinations
*/
comboBuilder = { def axes, int level ->
    for ( entry in axes[0] ) {
        comboEntry[level] = entry
        if (axes.size() > 1 ) {
            comboBuilder(axes[1..-1], level + 1)
        }
        else {
            tasks[comboEntry.join("-")] = task(comboEntry.collect())
        }
    }
}

stage ("Setup") {
    node {
        println "Initial Setup"
    }
}

stage ("Setup Combinations") {
    node {
        comboBuilder(axisList, 0)
    }
}

stage ("Multiconfiguration Parallel Tasks") {
    //Run the tasks in parallel
    parallel tasks
}

stage("The End") {
    node {
        echo "That's all folks"
    }
}

您可以在 http:///localhost:8080/job/multi-configPipeline/[build]/flowGraphTable/(在构建页面的 Pipeline Steps 链接下可用.

You can see a more detailed flow of the job at http://localhost:8080/job/multi-configPipeline/[build]/flowGraphTable/ (available under the Pipeline Steps link on the build page.

您可以将阶段向下移动到任务"创建中,然后更清楚地查看每个阶段的详细信息,但不能像多配置作业那样在一个整洁的矩阵中.

You can move the stage down into the "task" creation and then see the details of each stage more clearly, but not in a neat matrix like the multi-config job.

...
return {
    // This is where the important work happens for each combination
    stage ("${it.join('-')}--build") {
        node(myAgent) {
            println "Executing combination ${it.join('-')}"
            def javaHome = tool myJdk
            println "Node=${env.NODE_NAME}"
            println "Java=${javaHome}"
        }
        //Node irrelevant for this part
        node {
            println "fruit=${myFruit}"
        }
    }
}
...

或者您可以用自己的 stage 包装每个 node 以获得更多详细信息.

Or you could wrap each node with their own stage for even more detail.

当我这样做时,我注意到我之前的代码中有一个错误(现在已在上面修复).我将 comboEntry 引用传递给任务.我应该发送一份副本,因为虽然阶段的名称是正确的,但当它实际执行它们时,这些值当然是最后遇到的所有条目.所以我把它改成了tasks[comboEntry.join("-")] = task(comboEntry.collect()).

As I did this, I noticed a bug in my previous code (fixed above now). I was passing the comboEntry reference to the task. I should have sent a copy, because, while the names of the stages were correct, when it actually executed them, the values were, of course, all the last entry encountered. So I changed it to tasks[comboEntry.join("-")] = task(comboEntry.collect()).

我注意到您可以将原始 stage ("Multiconfiguration Parallel Tasks") {} 留在并行任务的执行附近.从技术上讲,现在您已经嵌套了阶段.我不确定詹金斯应该如何处理这个问题,但它没有抱怨.但是,父"级时序不包括并行级时序.

I noticed that you can leave the original stage ("Multiconfiguration Parallel Tasks") {} around the execution of the parallel tasks. Technically now you have nested stages. I'm not sure how Jenkins is supposed to handle that, but it doesn't complain. However, the 'parent' stage timing is not inclusive of the parallel stages timing.

我还注意到,当一个新的构建开始运行时,在作业的阶段视图"上,所有以前的构建都消失了,大概是因为阶段名称并不完全匹配.但是在构建完成运行后,它们都再次匹配并且旧的构建再次出现.

I also noticed is that when a new build starts to run, on the "Stage View" of the job, all the previous builds disappear, presumably because the stage names don't all match up. But after the build finishes running, they all match again and the old builds show up again.

最后,Blue Ocean 似乎并没有以同样的方式将其可视化.它不识别并行进程中的阶段",仅识别封闭阶段(如果存在),或者如果不存在则并行".然后只显示各个并行过程,而不是其中的阶段.

And finally, Blue Ocean doesn't seem to vizualize this the same way. It doesn't recognize the "stages" in the parallel processes, only the enclosing stage (if it is present), or "Parallel" if it isn't. And then only shows the individual parallel processes, not the stages within.

这篇关于Jenkins 管道多配置项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆