jenkins - 我的 jenkins 管道可以有一个可重用的 "post" block 吗?

标签 jenkins jenkins-plugins jenkins-pipeline

我有许多用于多个不同平台的 jenkins 管道,但我对所有这些管道的“post{}” block 非常相同。它在这一点上相当大,因为我在其中包括了成功、不稳定、失败和中止。

有没有办法参数化我可以在所有管道中导入的可重用 post{} block ?我希望能够导入它并向其传递参数(因为虽然它几乎相同,但对于不同的管道却略有不同)。

当前复制并粘贴到我的所有管道中的示例帖子 block {}

post {
    success{
        script {
            // I'd like to be able to pass in values for param1 and param2
            someGroovyScript {
                param1 = 'blah1'
                param2 = 'blah2'
            }
            // maybe id want a conditional here that does something with a passed in param
            if (param3 == 'blah3') {
                echo 'doing something'
            }
        }
    }
    unstable{
        ... you get the idea
    }
    aborted{
        ... you get the idea
    }
    failure{
        ... you get the idea
    }
}

以下不起作用:

//在 mypipeline.groovy 中

...
post {
    script {
        myPost{}
    }
}

//在 vars/myPost.groovy 中

def call(body) {

    def config = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = config
    body()

    return always {
        echo 'test'
    }
}

无效条件“myPost”- 有效条件是 [always, changed, fixed, regression, aborted, success, unstable, failure, notBuilt, cleanup]

我能以某种方式覆盖 post{} 吗?

最佳答案

共享库是实现此目的的一种方法,您已经非常接近了。

@Library('my-shared-library')_
pipeline {
...
    post {
        always {
            script {
                myPost()
            }
        }
    }
}

关于jenkins - 我的 jenkins 管道可以有一个可重用的 "post" block 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51508234/

相关文章:

docker - 如何使用 jenkins 管道步骤在 docker 镜像中安装 pip ?

jenkins - Jenkins管道脚本失败,并显示“类生成期间出现一般错误:方法代码太大!”

php - 为什么 Behat PHP 命令在终端中有效,但在 Jenkins 中无效?

macos - Mac 上的 jenkins,路径设置不正确,没有/usr/local/bin

Jenkins "Choice Parameter"- 有没有办法使用分隔符定义文本和值?

Jenkins 动态声明式管道参数

在 jenkinsfile 中找不到 mysqldump

Jenkins Active Choice Reactive Reference 参数多分支管道作业中的格式化 HTML 获取脚本中的当前分支名称

jenkins - jtl 文件未在 jenkins 中为 jmeter 解析

jenkins - 我如何确保始终执行 Jenkins 管道阶段,即使前一个阶段失败?