gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:38:49 24 4
gpt4 key购买 nike

多配置构建(使用 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...
}
}
}
}

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

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

    是否有一种策略可以两全其美,即:
  • 管道即代码
  • 单独的状态指示器
  • 限制重复?
  • 最佳答案

    这是部分答案。我认为其他有更好经验的人将能够改进它。

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

  • 除非需要用户输入,否则不要使用管道参数
  • 使用脚本化和声明性管道的混合
    (另见 https://stackoverflow.com/a/46675227/1569204)
  • 有一个基于参数声明管道的函数:
    (另见 https://jenkins.io/doc/book/pipeline/shared-libraries/)
  • 使用节点在管道中创建可见指标(至少在蓝海中)

  • 所以类似于以下内容:
        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"
    }
    }
    }
    }
  • 相关代码可以移动到共享库中(即使您实际上并不需要共享它)。
  • 关于jenkins - Jenkins 中的多配置/矩阵构建管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56046541/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com