gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:06:28 25 4
gpt4 key购买 nike

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

  • src/com/mycomapny
  • MyTest.groovy
  • 变量
  • test.groovy
  • Jenkins 文件

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

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

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

    我已放置 @NonCPS 在上面的 test.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.groovy 中打印输入值的值。

    编辑1:
    当我在方法“def nofity”上方使用@NonCPS 时,我的构建成功了,但除了通知方法第一行中的打印语句外,我什么也没得到。

    如果我不使用 @NonCPS,我将收到以下异常
    Error when executing always post condition:
    com.cloudbees.groovy.cps.impl.CpsCallableInvocation

    hudson.remoting.ProxyException:com.cloudbees.groovy.cps.impl.CpsCallableInvocation
    Finished: FAILURE

    最佳答案

    您看到此错误是因为您在构造函数中调用了闭包。相反,您应该将其提取到一个单独的方法中。构造函数应该用于使用初始化期间传递的值来初始化对象。例如,通常的做法是传递对 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()
    }
    }

    变量/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)
    }

    Jenkins 文件
    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

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

    关于jenkins - Jenkins 共享库中未调用构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56830611/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com