How do you load a groovy file and execute it

后端 未结 5 623
梦谈多话
梦谈多话 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:54

    Thanks @anton and @Krzysztof Krasori, It worked fine if I combined checkout scm and exact source file

    Example.Groovy

    def exampleMethod() {
        println("exampleMethod")
    }
    
    def otherExampleMethod() {
        println("otherExampleMethod")
    }
    return this
    

    JenkinsFile

    node {
        // Git checkout before load source the file
        checkout scm
    
        // To know files are checked out or not
        sh '''
            ls -lhrt
        '''
    
        def rootDir = pwd()
        println("Current Directory: " + rootDir)
    
        // point to exact source file
        def example = load "${rootDir}/Example.Groovy"
    
        example.exampleMethod()
        example.otherExampleMethod()
    }
    

提交回复
热议问题