How to override a Jenkinsfile library function locally?
You cannot override a function defined inside library script. But you can consider defining custom post build step as a closure passed to default_pipeline()
. Consider following example:
vars/default_pipeline.groovy
def call(body = null) {
pipeline {
agent any
stages {
stage('Build') {
steps {
script {
body != null ? body() : PostBuildStep()
}
}
}
}
}
}
def PostBuildStep() {
echo 'Running default post-build step';
}
Jenkinsfile
@Library('default_jenkins_libs') _
default_pipeline({
echo 'Running customized post-build step'
})
In this case default_pipeline
has a single optional parameter that is a closure that defines your custom post build step. Running following example will produce following output:
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Running customized post-build step
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Hope it helps.