groovy 在我的Jenkins共享库中,为什么我似乎没有执行我的方法?

bfhwhh0e  于 2023-06-21  发布在  Jenkins
关注(0)|答案(1)|浏览(168)

我有以下共享库结构

com/company/test/
                /ServiceBuilder.groovy
                /MonoBuilder.groovy
                /MicroServiceBuilder.groovy

然后在src/com/test/builders中,我有这3个文件

#ServiceBuilder.groovy
package com.company.test

abstract class ServiceBuilder implements Serializable {
  protected String msg = null
  // Used by all sub-classes
  protected def printMsg() {
    println "${this.msg}"
  }
  // Implement in sub-classes
  protected def setMsg(final String msg) {
  }
}

#MicroserviceBuilder.groovy
package com.company.test

class MicroserviceBuilder extends ServiceBuilder {
  MicroserviceBuilder(){
    super()
  }
  def setMsg(final String msg) {
    println this.getClass()
    this.msg = "Mirco: $msg"
  }
}

#MonoServiceBuilder
package com.company.test

class MonoBuilder extends ServiceBuilder {
  MonoBuilder(){
    super()
  }
  def setMsg(final String msg) {
    println this.getClass()
    this.msg = "Mono: $msg"
  }
}

下面是使用上述共享库的Jenkinsfile cat

@Library("my-shared-lib@feature/test-shared-lib") _
import com.company.test.ServiceBuilder
import com.company.test.MonoBuilder
import com.company.test.MicroserviceBuilder

node("linux-ubuntu") {
  stage("Mono"){
    ServiceBuilder builder = new MonoBuilder()
    builder.setMsg("hello")
    builder.printMsg()
  }
  stage("Micro"){
    ServiceBuilder builder = new MicroserviceBuilder()
    builder.setMsg("hola")
    builder.printMsg()
  }
}

代码执行了,但我没有看到任何预期的println输出。我以为

  • 调用object.setMsg(...)时打印的类类型
  • Mono: hello用于MonoService.printMsg()调用
  • Micro: hola用于Microservice.printMsg()调用

但在Jenkins控制台中

Running on linux-ubuntu-1 in /home/jenkins/workspace/Utilities/Playground/test-shared-lib
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Mono)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Micro)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline

我错过了什么?TIA

g6baxovj

g6baxovj1#

我通过查看Why do I love Jenkins shared libraries?来使它工作。下面是我修改过的3个Groovy文件,我在其中添加了一个jfile属性,它是调用Jenkinsfilethis指针。

#ServiceBuilder.groovy
package com.company.test

abstract class ServiceBuilder implements Serializable {
  protected def jfile = null
  protected String msg = null
  protected def printMsg() {
    jfile.println "${this.getClass()}: ${this.msg}"
  }
  protected def setMsg(final String msg) {
  }
}

#MicroserviceBuilder.groovy
package com.company.test

class MicroserviceBuilder extends ServiceBuilder {
  MicroserviceBuilder(final def jfile){
    super()
    this.jfile = jfile
  }
  def setMsg(final String msg) {
    jfile.println this.getClass()
    this.msg = "Mirco: $msg"
  }
}

#MonoBuilder.groovy 
package com.zift.test

class MonoBuilder extends ServiceBuilder {
  MonoBuilder(final def jfile){
    super()
    this.jfile = jfile
  }
  def setMsg(final String msg) {
    jfile.println this.getClass()
    this.msg = "Mono: $msg"
  }
}

这是Jenkinsfile

@Library("zift-infrastructure@feature/test-share-lib") _
import com.company.test.ServiceBuilder
import com.company.test.MonoBuilder
import com.company.test.MicroserviceBuilder

node("linux-ubuntu") {
  stage("Mono"){
    ServiceBuilder builder = new MonoBuilder(this)
    builder.setMsg("hello")
    builder.printMsg()
  }
  stage("Micro"){
    ServiceBuilder builder = new MicroserviceBuilder(this)
    builder.setMsg("hola")
    builder.printMsg()
  }
}

Jenkins控制台输出现在显示

Running on linux-ubuntu-2 in /home/jenkins/workspace/Utilities/Playground/test-shared-lib
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Mono)
[Pipeline] echo
class com.zift.test.MonoBuilder
[Pipeline] echo
class com.zift.test.MonoBuilder: Mono: hello
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Micro)
[Pipeline] echo
class com.zift.test.MicroserviceBuilder
[Pipeline] echo
class com.zift.test.MicroserviceBuilder: Mirco: hola
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline

这是预期的输出。但是为什么我必须这样做,使用调用者的println,而不是能够在类本身中这样做呢?

相关问题