我正在学习jenkins管道,我试着遵循这个管道代码.但我的詹金斯总是抱怨这def
不合法.我想知道我是否错过任何插件?我已经安装了groovy
,job-dsl
但是没有用.
正如@Rob所说,有两种类型的管道:scripted
和declarative
.它像imperative
VS declarative
.def
只允许在scripted
管道中或包裹script {}
.
从下面开始node
,def
或者if
被允许,如下所示.这是传统的方式.
node {
stage('Example') {
if (env.BRANCH_NAME == 'master') {
echo 'I only execute on the master branch'
} else {
echo 'I execute elsewhere'
}
}
}
开头pipeline
,def
或者if
不允许,除非它被包装script {...}
.声明性管道使得很容易编写和读取.
pipeline { agent any triggers { cron('H 4/* 0 0 1-5') } stages { stage('Example') { steps { echo 'Hello World' } } } }
pipeline { agent any stages { stage('Example Build') { steps { echo 'Hello World' } } stage('Example Deploy') { when { branch 'production' } steps { echo 'Deploying' } } } }
pipeline { agent any stages { stage('Non-Parallel Stage') { steps { echo 'This stage will be executed first.' } } stage('Parallel Stage') { when { branch 'master' } failFast true parallel { stage('Branch A') { agent { label "for-branch-a" } steps { echo "On Branch A" } } stage('Branch B') { agent { label "for-branch-b" } steps { echo "On Branch B" } } } } } }
pipeline { agent any stages { stage('Example') { steps { echo 'Hello World' script { def browsers = ['chrome', 'firefox'] for (int i = 0; i < browsers.size(); ++i) { echo "Testing the ${browsers[i]} browser" } } } } } }
要阅读更多声明性管道语法,请参阅此处的官方文档