Jenkins共享库中未调用构造函数

时间:2019-07-01 05:56:34

标签: jenkins groovy jenkins-pipeline jenkins-groovy jenkins-shared-libraries

我正在尝试开发共享库,并且具有以下目录结构

  • src / com / mycomapny
    • MyTest.groovy
  • vars
    • test.groovy
  • Jenkinsfile

我的 Jenkinsfile 仅调用 test.groovy 中可用的方法,并提供必需的输入。导入MyTest并创建对象并调用构造函数,然后执行执行 MyTest.groovy 文件

中可用功能的实际方法

这里从未从全局vars / test.groovy调用的构造函数类

我尝试从groovy调用类和方法,它工作正常,但是当我从jenkinsfile调用它时,它无法工作。我不知道为什么我不能调用构造函数以及我缺少的东西。

我已将 @NonCPS 放在上述test.groovy方法上,但仍然无法正常工作。

function getSenate() {
  json = dataSenate,
    json = json.results[0],
    jlen = json.num_results,
    members = json.members;
}

function getHouse() {
  json = dataHouse,
    json = json.results[0],
    jlen = json.num_results,
    members = json.members;
}

//GET ALL DATA:
function getData(counter) {
  memberParty = members[counter].party;
  memberState = members[counter].state;
  memberSeniority = members[counter].seniority;
  firstName = members[counter].first_name;
  middleName = members[counter].middle_name;
  lastName = members[counter].last_name;
  memberVotes = members[counter].votes_with_party_pct;
  missedVotes = members[counter].missed_votes;
  missedVotesPerc = members[counter].missed_votes_pct;
  spacer = " ";
  fullName = firstName.concat(spacer, middleName, spacer, lastName);
  if (fullName.includes("null") == true) {
    fullName = firstName.concat(spacer, lastName)

  } else {
    fullName = firstName.concat(spacer, middleName, spacer, lastName)
  }
}

在这里,我只想从jenkins文件中调用构造函数,并在我的vars / test.groovy中打印输入值的值。

EDIT1: 当我在“ def nofity”方法上方使用@NonCPS时,我的构建成功了,但是除了notify方法第一行中的print语句外,我什么都没得到。

如果我不使用@NonCPS,则会收到异常

//MyTest.groovy
package com.mycompany

class MyTest implements Serializable {
    public _notification

    MyTest(Closure content) {
        notification = [:]
        content.resolveStrategy = Closure.DELEGATE_FIRST
        content.delegate = notification
        content()
        println("Entered Constructor")
        this._notification = notification
    }

}

//test.groovy
import com.mycopany.MyTest
def notify(Closure content) {
    println("This is line i am getting in the output but nothing after that")
    MyTest tester = new MyTest(content)
    println(tester._notification.colorcode)
} 

//Jenkinsfile
@library("MysharedLibrary@master") _
pipeline{
    stages {
         stage {
             steps {
                 test.notify {
                      buildno = 12
                      jobname = "Mytest"
                      colorcode = "Blue"
                 }
             }
         }
    }
}

1 个答案:

答案 0 :(得分:2)

您看到此错误,因为正在构造函数内部调用闭包。相反,您应该将其提取为单独的方法。构造函数应用于使用初始化期间传递的值来初始化对象。例如,通常的做法是将引用传递给WorkflowScript对象,因此您可以使用echo之类的管道步骤。

请考虑对共享库代码进行以下更改:

src / com / mycompany / MyTest.groovy

package com.mycompany

class MyTest {
  final Map notification = [:]
  final Script script

  MyTest(Script script) {
    this.script = script
  }

  def notify(Closure content) {
    processContent(content)
    script.echo "Notification processed = ${notification}"
  }

  def processContent(Closure content) {
    content.resolveStrategy = Closure.DELEGATE_FIRST
    content.delegate = notification
    content()
  }
}

vars / test.groovy

import com.mycompany.MyTest
import groovy.transform.Field

@Field
final MyTest tester = new MyTest(this)

def notify(Closure content) {
    println("This is line i am getting in the output but nothing after that")
    tester.notify(content)
    println(tester.notification.colorcode)
}

Jenkinsfile

pipeline {
    agent any

    stages {
         stage("Test") {
             steps {
                 script {
                     test.notify {
                          buildno = 12
                          jobname = "Mytest"
                          colorcode = "Blue"
                     }
                 }
             }
         }
    }
}

输出:

[Pipeline] Start of Pipeline
[Pipeline] node
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
This is line i am getting in the output but nothing after that
[Pipeline] echo
Notification processed = [buildno:12, jobname:Mytest, colorcode:Blue]
[Pipeline] echo
Blue
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline

请记住,这只是重构共享管道库代码的一种方法。您的主要目标应该是从类构造函数内部移动闭包调用。由您决定放置它的位置,以便它最适合您。