Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing groovy script in jenkins Declarative pipeline file

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.

like image 294
user3279954 Avatar asked Apr 03 '19 02:04

user3279954


People also ask

How do I load Groovy script in Jenkins pipeline?

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.

Can I mix declarative and scripted pipeline?

Yes you can only if you want to have external function inside step block.

Which is better scripted or declarative pipeline?

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.

Is a Jenkins file a Groovy file?

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.


1 Answers

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.

like image 140
user_9090 Avatar answered Nov 15 '22 10:11

user_9090