我有一个标准化的声明性Jenkinsfile,用于许多项目。我已经将整个流水线移入了库中,因此我的Jenkinsfile看起来像这样:
@Library('default_jenkins_libs') _
default_pipeline();
图书馆(var/default_pipeline.groovy):
def call() {
pipeline {
node { <snip> }
stages {
stage('Lint and style') {
steps {
//stuff
}
}
//etc...
stage('Post-build') {
steps {
PostBuildStep();
}
}
}
}
def PostBuildStep() {
echo 'Running default post-build step';
}
我希望能够在Jenkinsfile的实际流水线代码中添加定义,例如:
。
@Library('default_jenkins_libs') _
def PostBuildStep() {
echo 'Running customized post-build step'
//do custom stuff
}
default_pipeline();
我还没有弄清楚怎样做。我怀疑这可以通过调用Jenkinsfile所代表的对象并调用其"PostBuildStep()"来完成,可能像'parent.PostBuildStep()"一样,但我没有该类结构/命名参考。
有什么建议吗?关键是,我想要一个标准化的流水线,可以通过库更改进行大规模更改,但仍然给使用它的作业一些控制权。
TIA
@Library('default_jenkins_libs') _ default_pipeline({ CustomDefinedStep() }) def CustomDefinedStep() { echo 'Running part one' echo 'Running part two' echo 'Running part three' }
对你的代码只有一个更改,“def call(body = null) {”需要改为“def call(def body = null) {”,否则我会收到“No matching signature.”的错误消息。谢谢!! - Mike Rysanek