How do you load a groovy file and execute it

后端 未结 5 625
梦谈多话
梦谈多话 2020-11-27 03:05

I have a jenkinsfile dropped into the root of my project and would like to pull in a groovy file for my pipeline and execute it. The only way that I\'ve been able to get th

5条回答
  •  再見小時候
    2020-11-27 03:44

    If you have pipeline which loads more than one groovy file and those groovy files also share things among themselves:

    JenkinsFile.groovy

    def modules = [:]
    pipeline {
        agent any
        stages {
            stage('test') {
                steps {
                    script{
                        modules.first = load "first.groovy"
                        modules.second = load "second.groovy"
                        modules.second.init(modules.first)
                        modules.first.test1()
                        modules.second.test2()
                    }
                }
            }
        }
    }
    

    first.groovy

    def test1(){
        //add code for this method
    }
    def test2(){
        //add code for this method
    }
    return this
    

    second.groovy

    import groovy.transform.Field
    @Field private First = null
    
    def init(first) {
        First = first
    }
    def test1(){
        //add code for this method
    }
    def test2(){
        First.test2()
    }
    return this
    

提交回复
热议问题