Is there a way to reuse groovy script loaded once in Jenkinsfile.
Right now this is what I am doing
steps {
script {
def util = load("${env.WORKSPACE}/scripts/build_util.groovy")
util.runStep1()
}
}
steps {
script {
def util = load("${env.WORKSPACE}/scripts/build_util.groovy")
util.runStep2()
}
}
steps {
script {
def util = load("${env.WORKSPACE}/scripts/build_util.groovy")
util.runStep3()
}
}
I am doing the same again in post build with multiple script blocks step to send mails.
Is there a better way to do this? I cannot use shared libraries.
In Jenkinsfile, simply use load step to load the Groovy script. After the Groovy script is loaded, the functions insides can be used where it can be referenced, as shown above.
Yes you can only if you want to have external function inside step block.
Basically, declarative and scripted pipelines differ in terms of the programmatic approach. One uses a declarative programming model and the second uses an imperative programming mode. Declarative pipelines break down stages into multiple steps, while in scripted pipelines there is no need for this.
The Jenkinsfile is written using the Groovy Domain-Specific Language and can be generated using a text editor or the Jenkins instance configuration tab. The Declarative Pipelines is a relatively new feature that supports the concept of code pipeline. It enables the reading and writing of the pipeline code.
Yes, you just need to load the script only once.
def util = load("${env.WORKSPACE}/scripts/build_util.groovy")
You can create a stage and load the script there and store in a variable and then do something like this:-
stage('Environment') {
agent { node { label 'master' } }
steps {
script {
def util = load("${env.WORKSPACE}/scripts/build_util.groovy")
}
}
}
post {
// Things that we want done regardless of pipeline's outcome
//
always {
// Push the overall statistics from all the stages to InfluxDB
//
node (LINUX_BUILD_NODE){
script{
//Mail sending function call
//
util.runStep1()
util.runStep2()
util.runStep3()
}
}
}
}
You can use "util" in any stage to call the different functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With