我正在学习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"
}
}
}
}
}
}
要阅读更多声明性管道语法,请在此处参考官方文档
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句