我可以在共享库中定义和参数化整个声明性管道吗?

Can I have an entire declarative pipeline defined and parameterized in a shared library?

基本上我希望我的 Jenkinsfile 看起来像这样:

@Library('my-descriptive-pipline') _
myPipeline('arg1','arg2','arg3')

那么我的共享库 (/vars/descriptivePipeline.groovy) 将如下所示:

pipeline {
    agent any
    options {
      skipDefaultCheckout()
    }
    environment {
    }
    stages {
        stage('stageA') {
            steps {
                ... Do something with arg1, arg2 or arg3
            }
        }
        stage('stageB') {
            steps {
                ... Do something with arg1, arg2 or arg3
            }
        }
    ....

我想集中管道配置,这样我就不必担心 Jenkinsfile 的蔓延。声明式管道是否可行?

不,目前无法共享完整的声明性管道。它 可以通过脚本管道实现:

开始报价: “声明式选项很好,但您将失去通过共享库提取可重用代码的能力。例如,您还不能这样做:

MyCompanyStandardPipeline {
    ... // which will plugin standard sections...
}

jenkins users mailing list.

上 Cuong Tran 的最后引用

您可以将共享库与任一 Jenkinsfile 变体一起使用,正如链接线程上的下一个回复所指出的那样,但只有使用脚本才能共享 完整 管道。

从声明式管道 1.2 开始,现在可以在共享库中定义整个管道。

您可以找到更多 here and here

shared libraries plugin documentation 所述:

Defining Declarative Pipelines

Starting with Declarative 1.2, released in late September, 2017, you can define Declarative Pipelines in your shared libraries as well.

这是相当新的,因此可能仍然存在一些错误,但它应该可以工作。不要忘记升级共享库和声明性插件。

不建议将整个管道放在共享库中...因为开发人员永远不会知道幕后发生的事情...声明的条目 objective 将消失..我建议创建可重用 class 或脚本并让开发人员编写管道