Ron*_*Ron 8 jenkins jenkins-plugins jenkins-pipeline
我正在学习jenkins管道,我试着遵循这个管道代码.但我的詹金斯总是抱怨这def
不合法.我想知道我是否错过任何插件?我已经安装了groovy
,job-dsl
但是没有用.
Ron*_*Ron 12
正如@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'
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
pipeline {
agent any
stages {
stage('Example Build') {
steps {
echo 'Hello World'
}
}
stage('Example Deploy') {
when {
branch 'production'
}
steps {
echo 'Deploying'
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
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"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
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"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
要阅读更多声明性管道语法,请参阅此处的官方文档
您可以将 def 与声明性管道一起使用,而不是在其中使用,例如
def agentLabel
if (BRANCH_NAME =~ /^(staging|master)$/) {
agentLabel = "prod"
} else {
agentLabel = "master"
}
pipeline {
agent { node { label agentLabel } }
..
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11095 次 |
最近记录: |