println in “call” method of “vars/foo.groovy” works, but not in method in class

前端 未结 1 1443
南方客
南方客 2020-12-06 17:44

I\'m iterating through building a Jenkins pipeline shared library, so my Jenkinsfile is a little cleaner.

I\'m using the following page for guidance: https://jenkins

相关标签:
1条回答
  • 2020-12-06 18:08

    I did some digging and found this issue, https://issues.jenkins-ci.org/browse/JENKINS-41953, basically in normal pipeline script println is aliased to echo step. But when you're in a class, e.g. outside of the pipeline CPS, then the echo step isn't available and the println is ignored (since, as I understand it, there is no logger available).

    What you can do is to propagate the script environment into your class methods using a variable and call echo through the variable (found solution in this thread). Like this:

    class A {
        Script script;
        public void a() {
            script.echo("Hello")
        }
    }
    def a = new A(script:this)
    echo "Calling A.a()"
    a.a()
    

    outputs:

    Started by user jon
    [Pipeline] echo
    Calling A.a()
    [Pipeline] echo
    Hello
    [Pipeline] End of Pipeline
    Finished: SUCCESS
    

    Which is what we want. For comparison, here is without propagation:

    class A {
        public void a() {
            println "Hello"
        }
    }
    def a = new A()
    echo "Calling A.a()"
    a.a()
    

    Gives:

    Started by user jon
    [Pipeline] echo
    Calling A.a()
    [Pipeline] End of Pipeline
    Finished: SUCCESS
    
    0 讨论(0)
提交回复
热议问题