Jenkinsfile 你如何加載一個groovy文件並執行它


I have a jenkinsfile dropped into the root of my project and would like to pull in a groovy file for my pipeline and execute it. The only way that I've been able to get this to work is to create a separate project and use the fileLoader.fromGit command. I would like to do

def pipeline = load 'groovy-file-name.groovy'
pipeline.pipeline()
解決方案

If your JenkinsFile and groovy file in one repository and JenkinsFile is loaded from SCM you have to do:

Example.Groovy

def exampleMethod() {
    //do something
}

def otherExampleMethod() {
    //do something else
}
return this

JenkinsFile

node {
    def rootDir = pwd()
    def example = load "${rootDir}@script/Example.Groovy "
    example.exampleMethod()
    example.otherExampleMethod()
}
 

我有一個jenkinsfile放到我的項目的根目錄下,並且希望為我的管道提供一個groovy文件並執行它。我能夠得到這個工作的唯一方法是創建一個單獨的項目並使用fileLoader.fromGit命令。我想要做的是

def pipeline = load'groovy-file-name.groovy'
pipeline.pipeline()

解決方案

如果您的JenkinsFile和groovy文件在一個存儲庫中,並且JenkinsFile從SCM加載,您必須這樣做:

Example.Groovy


$ b

 def exampleMethod(){
//做某事
}

def otherExampleMethod(){
//做別的事
}
返回這個

$ b

JenkinsFile $ b

 節點{
def rootDir = pwd()
def example = load"${rootDir}@script/Example.Groovy"
example.exampleMethod()
example.otherExampleMethod()
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM