Jenkins管道外部脚本返回空值

时间:2017-05-25 21:27:59

标签: jenkins groovy jenkins-pipeline

我的问题类似于this one关于如何加载外部groovy脚本,然后在不同的groovy脚本中调用它的方法。到目前为止,我已经能够获得不返回值的方法,但是我无法将返回值转换为被调用的变量。

例如,以下管道代码可以正常工作,但在运行Jenkins管道时,null的值为$build_user。它实际上没有返回我期望它,我不知道为什么。

node {
    stage('test') {
        def tools = load "/var/lib/jenkins/workflow-libs/vars/tools.groovy"
        build_user = tools.get_user()
        echo "build_user: $build_user"
    }
}

以下是相关tools.groovy的内容。

def exampleMethod() {
    // Do stuff
}

// Try to get a build username
def get_user() {

    try {
        wrap([$class: 'BuildUser']) {
            // Set up our variables
            fallback_user = 'GitHub'
            github_user = BUILD_USER
            commit_author = 'Test1'

            // Try to use Jenkins build user first
            if (github_user) {
                echo "using github_user: $github_user"
                return github_user
            }
            // Otherwise try to use commit author
            else if (commit_author) {
                echo "using commit_author: $commit_author"
                return commit_author
            }
            // Otherwise username is blank so we use the default fallback
            else {
                echo "using fallback: $fallback_user"
                return fallback_user
            }
        }
    }
    catch (err) {
        // Ignore errors
    }

    echo "Done."
}

return this

以上代码的完整Jenkins输出。

Started by user XXX
[Pipeline] node
Running on master in /var/lib/jenkins/workspace/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] load
[Pipeline] { (/var/lib/jenkins/workflow-libs/vars/tools.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] wrap
[Pipeline] {
[Pipeline] echo
using github_user: XXX
[Pipeline] }
[Pipeline] // wrap
[Pipeline] echo
Done.
[Pipeline] echo
build_user: null
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

如果我在结尾删除return this并在Jenkins中抛出以下错误,则上述方法根本不起作用。

  

java.lang.NullPointerException:无法调用get_user()方法   null对象   ...

我做错了什么?我怀疑我错过了一些简单的东西,但我对Groovy并不满意,所以我不确定它是什么。

2 个答案:

答案 0 :(得分:2)

您必须以import random x=0 B=['E','C','D','T'] E=[1] C=[1] D=[] T=[] Inicio=input('Presiona enter para iniciar el juego!!!') #random.shuffle(E) #random.shuffle(C) #random.shuffle(D) #random.shuffle(T) def ocultar(lista): nueva_lista = [] for e in range(len(lista)): nueva_lista.append('*') return nueva_lista while x==0: print('E=',ocultar(E)) print('C=',ocultar(C)) print('D=',ocultar(D)) print('T=',ocultar(T)) Coordenadas1 = input('Ingrese la lista 1: ') Coordenadas2 = input('Ingrese la posicion 1: ') Coordenadas3 = input('Ingrese la lista 2: ') Coordenadas4 = input('Ingrese la posicion 2: ') listas = {'E': E, 'C': C, 'D': D, 'T': T} pos1 = int(Coordenadas2) pos2 = int(Coordenadas4) if listas[Coordenadas1][pos1] == listas[Coordenadas3][pos2]: (listas[Coordenadas1]).remove(listas[Coordenadas1][pos1]) (listas[Coordenadas3]).remove(listas[Coordenadas3][pos2]) if len(E)==0 and len(C)==0 and len(D)==0 and len(T)==0: x=x+1 print("Ganaste!!!") 结束tools.groovy 请参阅此问题的答案How do you load a groovy file and execute it

答案 1 :(得分:1)

你的函数get_user()什么都不返回。

return(s)内的wrap([$class: 'BuildUser']) {...}确实从wrap课程返回,而不是从您的函数返回。

相关问题