Jenkins中的多配置/矩阵构建管道

时间:2019-05-08 17:55:52

标签: jenkins continuous-integration jenkins-pipeline

使用Jenkins,多配置构建的现代最佳实践是什么?

我想支持多个分支和多个配置。

例如,对于我要构建定位的软件的每个版本V1,V2 平台P1和P2。

我们设法建立了多分支的声明式管道。每个构建都有自己的docker,因此可以轻松支持多个平台。

pipeline { 
    agent none 
    stages {
        stage('Build, test and deploy for P1) {
            agent {
                dockerfile {
                   filename 'src/main/docker/Jenkins-P1.Dockerfile'
                }
            }
            steps {
               sh buildit...
            }
        }
        stage('Build, test and deploy for P2) {
            agent {
                dockerfile {
                   filename 'src/main/docker/Jenkins-P2.Dockerfile'
                }
            }
            steps {
               sh buildit...
            }
        }
    }
}

这提供了一个涵盖多个平台的工作,但每个平台没有单独的红色/蓝色状态。 有一个很好的论点,那就是,这无关紧要,因为除非构建适用于所有平台,否则您不应该发布该版本。

但是,我希望每种配置都有单独的状态指示器。这表明我应该使用一个多配置构建,该构建为每个配置触发一个参数化的构建,如下所示(以及linked question):

pipeline { 
    parameters {
      choice(name: 'Platform',choices: ['P1', 'P2'], description: 'Target OS platform', )
    }
    agent {
       filename someMagicToGetDockerfilePathFromPlatform()
    }
    stages {
        stage('Build, test and deploy for P1) {
            steps {
               sh buildit...
            }
        }
    }
}

这有几个问题:

  • 声明性管道对其脚本的编写方式有更多限制
  • 多配置构建无法触发声明式管道(即使使用参数化的触发器插件,我也会得到“项目不可构建”的信息。)

这也引出了一个问题:声明性管道中的参数有什么用?

有没有一种可以兼顾两方面的策略,即:

  • 管道作为代码
  • 单独的状态指示器
  • 重复次数有限?

2 个答案:

答案 0 :(得分:2)

这是部分答案。我认为其他具有更好经验的人将可以对此进行改进。

目前未经测试。我可能正在吠错树。 请发表评论或添加更好的答案。

类似以下内容:

    def build(string platform) {
       switch(platform) {
         case P1:
            dockerFile = 'foo'
            indicator = 'build for foo'
            break
         case P2:
            dockerFile = 'bar'
            indicator = 'build for bar'
            break
       }
       pipeline {
         agent {
            dockerfile {
               filename "$dockerFile"
            }
            node { 
               label "$indicator"
            }
         }
         stages {
           steps {
             echo "build it"
           }
         }
       }
    }
  • 相关代码可以移至共享库(即使您实际上不需要共享)。

答案 1 :(得分:0)

我认为最干净的方法是将所有内容都与您介绍的第一个类似,并在管道中进行,我在这里看到的唯一修改是将它们并行化,因此您实际上将尝试针对两个平台进行构建/测试。 / p>

要重用上一阶段的工作空间,您可以执行以下操作:reuseNode true

与该流程类似的东西,将具有针对平台的并行构建 enter image description here

    App.js:8 GET http://localhost:3001/download/test.pdf    net::ERR_CONNECTION_REFUSED

    Uncaught (in promise) TypeError: Failed to fetch
相关问题