我可以从 Jenkins 管道中的函数调用另一个函数吗?

Can i call an another function from my function in Jenkins pipeline?

我的 Jenkins 管道中有两个独立的函数,我想从第二个函数调用第一个函数。

我尝试了以下代码。

def first(){
    return{
        stages{
            stage("test"){
                steps{
                    echo "ok"
                }
            }
        }
     }
}

 def second(){
     return{
        first().call()
    }
}


pipeline {
    agent any
    stages{
       stage("Run"){
            steps{
                script{
                    second().call()

                }

            }

        }

   }

}

这可能与否。建议我正确的方法。

是的,你可以。您的 Jenkinsfile 将如下所示:

def first(){
    stage("test"){
        println "executing first"
    }  
}

def second(){
    println("calling first from second")
    first()
}


pipeline {
    agent any
    stages{
       stage("Run"){
            steps{
                second()
            }
        }
    }
}