我有许多适用于多个不同平台的 Jenkins 管道,但所有这些管道的“post{}”块非常相同。此时它相当大,因为我包括成功、不稳定、失败和中止。
有没有办法参数化我可以在所有管道中导入的可重用的 post{} 块?我希望能够导入它并传递它的参数(因为虽然它几乎相同,但对于不同的管道来说,它的变化非常小)。
当前复制并粘贴到我所有管道中的示例帖子块
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'
}
}
Invalid condition "myPost" - valid conditions are [always, changed, fixed, regression, aborted, success, unstable, failure, notBuilt, cleanup]
我可以以某种方式覆盖 post{} 吗?
共享库是一种方法,你已经非常接近了。
@Library('my-shared-library')_
pipeline {
...
post {
always {
script {
myPost()
}
}
}
}