[英]Jenkins Pipeline Jenkinsfile: 'node' and 'pipeline' directives
[英]Jenkins pipeline - how to load a Jenkinsfile without first calling node()?
我有一個有點獨特的設置,在這里我需要能夠動態加載生活在我正在構建的src之外的Jenkinsfiles。 Jenkinsfiles本身通常調用node(),然后執行一些構建步驟。 這將導致不必要地吞噬多個執行程序,因為我需要已經調用node()才能使用加載步驟來運行Jenkinsfile,或者如果我將Jenkinsfile讀取為字符串並執行它,則可以執行常規操作。
我今天在工作界面中擁有什么:
@Library(value='myGlobalLib@head', changelog=fase) _
node{
load "${JENKINSFILES_ROOT}/${PROJECT_NAME}/Jenkinsfile"
}
加載的Jenkinsfile通常也調用node()。 例如:
node('agent-type-foo'){
someBuildFlavor{
buildProperty = "some value unique to this build"
someConfig = ["VALUE1", "VALUE2", "VALUE3"]
runTestTarget = true
}
}
這導致在管道運行期間消耗2個執行程序。 理想情況下,無需先調用node()即可加載Jenkinsfiles,但是每當嘗試嘗試時,我都會收到一條錯誤消息,指出:
"Required context class hudson.FilePath is missing
Perhaps you forgot to surround the code with a step that provides this, such as: node"
有什么方法可以在沒有hudson.FilePath上下文的情況下加載Jenkinsfile或執行Groovy? 我似乎在文檔中找不到任何內容。 我現在要預處理Jenkinsfile,以刪除它們對node()的初始調用,並使用Jenkinsfile所使用的值調用node(),然后加載文件的其余部分,但這有點令我感到脆弱。
使用load
步驟時,Jenkins評估文件。 您可以將Jenkinsfile的邏輯包裝到一個函數中(在我的示例中為run()
),這樣它將加載但不會自動運行。
def run() {
node('agent-type-foo'){
someBuildFlavor{
buildProperty = "some value unique to this build"
someConfig = ["VALUE1", "VALUE2", "VALUE3"]
runTestTarget = true
}
}
}
// This return statement is important in the end of Jenkinsfile
return this
從您的工作腳本中調用它,如下所示:
def jenkinsfile
node{
jenkinsfile = load "${JENKINSFILES_ROOT}/${PROJECT_NAME}/Jenkinsfile"
}
jenkinsfile.run()
這樣就不再有嵌套的node
塊,因為第一個在調用run()
函數之前就已關閉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.