当前位置: 首页 > 知识库问答 >
问题:

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

易烨磊
2023-03-14

我正在尝试开发共享库,目录结构如下

  • src/com/mycomapny
    • 我的测试。棒极了
    • 测试。棒极了

    我的Jenkinsfile调用test.groovy中唯一可用的方法,需要输入。它导入MyTest并创建对象,调用构造函数,然后是执行MyTest.groovy文件中可用功能的实际方法

    在这里,构造函数类从未从全局vars/test调用过。棒极了

    我尝试从groovy调用类和方法,它工作正常,但当我从jenkinsfile调用它时,它不工作。我不知道为什么我不能调用构造函数和我错过了什么。

    我已将@NonCPS置于上述测试中。groovy方法,但仍然不起作用。

    //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"
                     }
                 }
             }
        }
    }
    

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

    EDIT1:当我使用@NonCPS上面的方法"def nofity"我的构建是越来越成功,但我得到的除了打印语句在第一行的通知方法。

    如果我不使用@NonCPS,我会得到以下异常

    Error when executing always post condition:
    com.cloudbees.groovy.cps.impl.CpsCallableInvocation
    
    hudson.remoting.ProxyException:com.cloudbees.groovy.cps.impl.CpsCallableInvocation
    Finished: FAILURE
    

共有1个答案

司知
2023-03-14

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

考虑以下对共享库代码的更改:

src/com/mycompany/MyTest。棒极了

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()
  }
}

变量/测试。棒极了

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)
}

詹金斯档案

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

请记住,这只是重构共享管线库代码的一种方式。您的主要目标应该是从类构造函数内部移动闭包调用。这取决于你把它放在哪里,所以它最适合你。

 类似资料:
  • 问题内容: 从共享库/ dll调用函数的最简单,最安全的方法是什么?我对在Linux上执行此操作最感兴趣,但是如果有一种与平台无关的方法会更好。 有人可以提供示例代码来显示如何在用户将自己的版本编译到共享库的情况下执行以下工作吗? 顺便说一句,我知道如何编译共享库(),我只需要知道一种在运行时加载它的简单方法。 问题答案: 注意: 您正在库调用周围传递C 对象(在这种情况下为STL字符串)。有 *

  • 为什么: 如果一个类不提供任何然后在编译时由编译器给出,但是如果一个类包含,那么默认构造函数不是由编译器提供。 我正在编译下面的代码。它给出了编译错误。 代码: 控制台错误: 当我只调用。工作正常。但是当您想使用参数化构造函数调用

  • 问题内容: 我有一个.so文件,我想在我的Go代码中调用其功能。 我该怎么做呢?我已经阅读了cgo和syscall软件包。它们接近我想要的位置,但是我看不到可以在.so文件中调用函数的任何地方。 我想确切地实现ctypes包在Python中的功能。 有人可以帮忙吗? 问题答案: 如果要使用在编译时静态已知的共享库,则可以简单地使用cgo。阅读有关如何准确执行此操作的文档,但是通常您会指定一些链接器

  • 我通过jenkins管道(共享库)运行此代码。 它在本地工作,但在Jenkins管道中,我得到以下错误: 请帮帮我

  • 我有一个Jenkins文件,我正在尝试从共享库实例化groovy类。我得到“无法解决类测试” 我有一个src/com/org/foo。共享库中的groovy文件: 我正试图在我的Jenkins文件中实例化它 如果我将文件作为一个类引用(我没有对其构造函数的访问权限),那么它就会起作用。即: 这很好,让我使用foo函数。但是,我失去了赋予类常量并用参数构造它的能力。 知道如何定义和使用共享库中的类吗

  • 问题内容: 两个共享库liba.so和libb.so。liba.so使用libb.so。所有c文件都使用-fPIC编译。链接使用- shared。当我们在liba.so上调用dlopen时,它无法在libb.so中找到符号…我们得到“未定义符号”错误。我们可以dlopen libb.so没有错误。我们知道liba正在找到libb,因为我们没有得到文件未找到错误。删除libb.so时,出现文件未找到