我完全是 Groovy 的菜鳥。對于背景關系,我有一個使用 deploy.pipeline.groovy 腳本的 Jenkins 管道。我還有一個用于 git PR 的 test.pipeline.groovy 腳本。
我試圖減少兩個腳本中的重復代碼,所以我創建了一個 Globals.groovy 腳本來存盤我的常量和一個 Functions.groovy 腳本來存盤兩個管道腳本的可重用函式。所有檔案都在同一目錄中,但我不知道如何將我的 Globals 和 Functions 腳本匯入到管道腳本中以供使用。
我的 Globals.groovy 檔案是這樣的:
import groovy.transform.Field
@CompileStatic class Globals {
@Field final String test1 = 'first test'
@Field final String test2 = 'second test'
}
我的 Functions.groovy 檔案是這樣的:
@CompileStatic class Functions {
def TestMessage1() { println globals.test1 }
def TestMessage2() { println globals.test2 }
}
兩個管道腳本都有一個“測驗”階段,如下所示:
def runTests{
stage('Test') {
functions.TestMessage1()
functions.TestMessage1()
}
}
我不知道如何將我的 Globals.groovy 腳本匯入或加載到我的 Functions.groovy 腳本中,然后將我的 Functions.groovy 腳本匯入或加載到我的腳本中。
我試過把它放在我的 Functions.groovy 腳本的頂部:
def globals = load('Globals.groovy')
這在我的管道腳本的頂部
def functions = load('Functions.groovy')
我究竟做錯了什么?
uj5u.com熱心網友回復:
您可以在 groovy 檔案中使用函式,并需要在底部回傳
這是我的 Jenkinsfile:
def pipeline
node('master') {
checkout scm
pipeline = load 'script.groovy'
pipeline.execute()
}
這是我的 script.groovy(與 repo 中的 Jenkinsfile 級別相同)
//import ...
def execute() {
println 'Test'
}
return this
詹金斯作業輸出:
[Pipeline] load
[Pipeline] { (script.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] echo
Test
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/342373.html
