gpt4 book ai didi

jenkins - 在库中使用 Jenkins 管道,但添加额外的阶段

转载 作者:行者123 更新时间:2023-12-05 07:22:56 25 4
gpt4 key购买 nike

我们有很多很多项目都有自己的 Jenkinsfile,它只是执行我们共享库中定义的管道。管道确保所有项目都以完全相同的方式构建、打包和安装。

project-a/Jenkinsfile

library 'the-shared-library'

buildProject name: 'project-a', buildApi: true, ...

the-shared-library/vars/buildProject.groovy

def call(Map config) {
pipeline {
// standard stages go here
}
}

我们希望对其进行扩展,以允许在管道期间为某些项目(例如众多项目中的 1 个)执行额外的阶段。如果可能的话,我正在考虑这样做:

  1. 将一个作为阶段的配置参数传递到 buildProject
  2. buildProject.call 中,如果提供了自定义阶段,将其附加到管道的末尾,或者可能在两个(已知)阶段之间,然后运行它

像这样的……

project-a/Jenkinsfile

library 'the-shared-library'

def myCustomStage = ... // not sure how

buildProject name: 'project-a', buildApi: true, ..., customStage: myCustomStage

the-shared-library/vars/buildProject.groovy

def call(Map config) {
def customStage = config.customStage

pipeline {
// standard stages 1 through 3
// if customStage provided, it goes here
// standard stages 5 through 5
}
}

我不确定这里的正确解决方案是什么。

最佳答案

我还没有测试过,但是像这样的东西看起来应该可以工作:

//project-a/Jenkinsfile
library 'the-shared-library'

def myCustomStage = { echo 'Hello' }

buildProject name: 'project-a', buildApi: true, ..., myCustomStage
//the-shared-library/vars/buildProject.groovy
def call(Map config, Closure customStage=null) {
def customStage = config.customStage

pipeline {
// standard stages 1 through 3
// if customStage provided, it goes here
stage('Conditional'){
when{
expression { customStage }
}
steps { script {customStage()} }
// standard stages 5 through 5
}
}

参见 Can I use a Closure to define a stage in a Jenkins Declarative Pipeline?

关于jenkins - 在库中使用 Jenkins 管道,但添加额外的阶段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56264878/

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