[英]Jenkins Job DSL: How to read Pipeline DSL script from file?
我想通過Job DSL生成基於管道插件的作業,Job DSL包含在由Jenkins檢出的git存儲庫中。
但是,我認為將管道腳本作為Job DSL腳本中的引用字符串並不是很好。 所以我想把它們讀成一個字符串並將其傳遞給script()
函數:
definition {
cps {
sandbox()
script( new File('Pipeline.groovy').text )
}
}
}
我在哪里可以使用Pipeline.groovy
來實現這個目的? 我嘗試將它放在我的DSL腳本旁邊,也放在DSL源的resources/
文件夾中。 但詹金斯總是拋出一個“找不到檔案”。
你試過readFileFromWorkspace()
嗎? 它應該能夠找到你從git結賬的文件。
參考Job DSL管道工作: https : //jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob ,並在http://job-dsl.herokuapp.com/上查看生成的配置。
下面的Job DSL創建一個管道作業,它從Jenkinsfile中提取實際作業:
pipelineJob('DSL_Pipeline') {
def repo = 'https://github.com/path/to/your/repo.git'
triggers {
scm('H/5 * * * *')
}
description("Pipeline for $repo")
definition {
cpsScm {
scm {
git {
remote { url(repo) }
branches('master', '**/feature*')
scriptPath('misc/Jenkinsfile.v2')
extensions { } // required as otherwise it may try to tag the repo, which you may not want
}
// the single line below also works, but it
// only covers the 'master' branch and may not give you
// enough control.
// git(repo, 'master', { node -> node / 'extensions' << '' } )
}
}
}
}
您/您的Jenkins管理員可能希望將Jenkins作業創建與實際作業定義分開。 這對我來說似乎是明智的......集中腳本來在單個Jenkins DSL倉庫中創建Jenkins作業並不是一個壞主意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.