Jenkins 如何加载groovy文件并执行它

Jenkins 如何加载groovy文件并执行它,jenkins,groovy,jenkins-pipeline,Jenkins,Groovy,Jenkins Pipeline,我将一个Jenkins文件放到项目的根目录中,并希望为我的管道引入一个groovy文件并执行它。我能够让它工作的唯一方法是创建一个单独的项目并使用fileLoader.fromGit命令。我想做什么 def pipeline=load'groovy file name.groovy' 管道。管道() 在执行load之前,您必须先执行签出scm(或从scm中签出代码的其他方式),如果您的Jenkinsfile和groovy文件位于一个存储库中,并且Jenkinsfile是从scm中加载的,则您必须

我将一个Jenkins文件放到项目的根目录中,并希望为我的管道引入一个groovy文件并执行它。我能够让它工作的唯一方法是创建一个单独的项目并使用
fileLoader.fromGit
命令。我想做什么

def pipeline=load'groovy file name.groovy'
管道。管道()

在执行
load
之前,您必须先执行
签出scm
(或从scm中签出代码的其他方式),如果您的
Jenkinsfile
和groovy文件位于一个存储库中,并且
Jenkinsfile
是从scm中加载的,则您必须执行以下操作:

示例.Groovy

def exampleMethod() {
    println("exampleMethod")
}

def otherExampleMethod() {
    println("otherExampleMethod")
}
return this
def first.groovy(arg1,arg2){
  //whatever others commands

  def caller = load pwd() + '/second.groovy'
  caller.otherMethod(arg1,arg2)
}
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()
                }
            }
        }
    }
}
def test1(){
    //add code for this method
}
def test2(){
    //add code for this method
}
return this
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
def exampleMethod(){
//做点什么
}
def otherExampleMethod(){
//做点别的
}
还这个
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()
}
node {
  checkout scm
  //other commands if you have

  def runner = load pwd() + '/first.groovy'
  runner.whateverMethod(arg1,arg2)
}
节点{
def rootDir=pwd()
def exampleModule=load“${rootDir}@script/Example.Groovy”
exampleModule.exampleMethod()
exampleModule.otherExampleMethod()
}

谢谢@anton和@Krzysztof Krasori,如果我将
签出scm
和精确的源文件结合起来,效果会很好

示例.Groovy

def exampleMethod() {
    println("exampleMethod")
}

def otherExampleMethod() {
    println("otherExampleMethod")
}
return this
def first.groovy(arg1,arg2){
  //whatever others commands

  def caller = load pwd() + '/second.groovy'
  caller.otherMethod(arg1,arg2)
}
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()
                }
            }
        }
    }
}
def test1(){
    //add code for this method
}
def test2(){
    //add code for this method
}
return this
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
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()
}
node {
  checkout scm
  //other commands if you have

  def runner = load pwd() + '/first.groovy'
  runner.whateverMethod(arg1,arg2)
}

非常有用的线程,有相同的问题,随你解决

我的问题是:
Jenkinsfile
->先调用一个
first.groovy
->再调用
second.groovy

以下是我的解决方案:

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()
}
node {
  checkout scm
  //other commands if you have

  def runner = load pwd() + '/first.groovy'
  runner.whateverMethod(arg1,arg2)
}
first.groovy

def exampleMethod() {
    println("exampleMethod")
}

def otherExampleMethod() {
    println("otherExampleMethod")
}
return this
def first.groovy(arg1,arg2){
  //whatever others commands

  def caller = load pwd() + '/second.groovy'
  caller.otherMethod(arg1,arg2)
}
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()
                }
            }
        }
    }
}
def test1(){
    //add code for this method
}
def test2(){
    //add code for this method
}
return this
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
注意:参数是可选的,如果有,请添加它们,或者留空


希望这能进一步帮助您。

如果您有一个管道可以加载多个groovy文件,并且这些groovy文件之间也共享一些东西:

JenkinsFile.groovy

def exampleMethod() {
    println("exampleMethod")
}

def otherExampleMethod() {
    println("otherExampleMethod")
}
return this
def first.groovy(arg1,arg2){
  //whatever others commands

  def caller = load pwd() + '/second.groovy'
  caller.otherMethod(arg1,arg2)
}
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()
                }
            }
        }
    }
}
def test1(){
    //add code for this method
}
def test2(){
    //add code for this method
}
return this
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
first.groovy

def exampleMethod() {
    println("exampleMethod")
}

def otherExampleMethod() {
    println("otherExampleMethod")
}
return this
def first.groovy(arg1,arg2){
  //whatever others commands

  def caller = load pwd() + '/second.groovy'
  caller.otherMethod(arg1,arg2)
}
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()
                }
            }
        }
    }
}
def test1(){
    //add code for this method
}
def test2(){
    //add code for this method
}
return this
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
second.groovy

def exampleMethod() {
    println("exampleMethod")
}

def otherExampleMethod() {
    println("otherExampleMethod")
}
return this
def first.groovy(arg1,arg2){
  //whatever others commands

  def caller = load pwd() + '/second.groovy'
  caller.otherMethod(arg1,arg2)
}
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()
                }
            }
        }
    }
}
def test1(){
    //add code for this method
}
def test2(){
    //add code for this method
}
return this
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

这假设要加载的文件在SCM中。如果文件在SCM中,则在尝试加载之前需要从SCM中获取该文件,这是正确的。但是,如果库文件与主文件位于同一个repo中,那么如果管道配置为自动拉取repo,则不必调用checkout;在job config.Readers中,请注意,在Groovy中,“returnthis”非常重要。@anton与此同时,我找到了
&
操作符(例如:
def exampleMethod=example.&exampleMethod
。效果很好…@michaelaster“this”究竟返回什么?如果这是gradle脚本,它将返回一个项目类的实例作为绑定。但在普通groovy文件中,我无法理解。@AntonShishkin,
${rootDir}是什么
变量,以及它在哪里/如何设置?它是Jenkins固有的还是自定义的?我得到了
groovy.lang.MissingPropertyException:没有这样的属性:rootDir for class:groovy.lang.Binding
。因此,我发现了一个警告——当运行同一个作业的两个并发实例时,Jenkins会将
@2
附加到工作区名称…howev呃,"代码","脚本","代码"目录,"代码","工作空间"目录,就不是这样了@script/Example.Groovy在运行并发构建时将不起作用。这是允许SCM中的Jenkins管道工作所需的,现在我将所有常量和函数集中到一个公共Jenkinsfile.common.Groovy中,该文件在发布管道和集成测试管道之间共享。快速提醒,load()仅在node()内有效。第二次加载()有效,因为在node()内调用了whateverMethod()。