如何在声明性管道的特定阶段使用特定的 groovy 版本?
How to use a specific groovy version in a particular stage within a declarative pipeline?
在我的 windows 机器上,
groovy -version
Groovy Version: 3.0.8 JVM: 16.0.1 Vendor: Oracle Corporation OS: Windows 10
我还有两个 groovy 通过 全局工具配置 安装,名称为 -
Groovy 3.0.5
Groovy 3.0.6
我打算在声明性管道中执行 groovy 代码。
我安装的 Jenkins 随 groovy-all-2.4.12
一起提供,而 groovy.xml.XmlSlurper
不提供
下面是一段代码
pipeline {
agent any
tools {
nodejs 'NodeJS'
}
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
stage('Initial-Checks') {
steps {
bat "npm -v"
bat "groovy -v" // gives the one installed on windows(i.e. 3.0.8)
}}
stage('Policy-Code Analysis') {
steps {
script {
def text = '''
<list>
<technology>
<name>Jenkins, Groovy</name>
</technology>
</list>
'''
def list = new XmlSlurper().parseText(text)
assert list instanceof groovy.xml.slurpersupport.GPathResult
println list.technology.name
}
}
}
}
}
运行 这导致
unable to resolve class groovy.xml.XmlSlurper
def list = new groovy.xml.XmlSlurper().parseText(text)
如何将 groovy version 3.0.6
用于包含在 script { ... }
中的代码而不是 Jenkins 使用的版本?
此外,以下不适用于工具
pipeline {
agent any
stages {
stage ("first") {
tools {
groovy "Groovy 3.0.5"
}
steps {
bat 'groovy -version' // gives 3.0.8 and not 3.0.5
}
}
stage("second"){
tools {
groovy "Groovy 3.0.6"
}
steps{
bat 'groovy -version' // gives 3.0.8 and not 3.0.6
}
}
}
}
第一个选项是在不同的阶段使用不同的代理,安装了不同的 groovy 版本(例如 docker 图像)。
pipeline {
agent none
stages {
stage {
agent { label 'some_label' }
steps {
bat 'groovy -version'
}
stage("second"){
agent { label 'another_label' }
steps{
bat 'groovy -version'
}
}
}
}
使用 groovy 工具有一个解决方法。
pipeline {
agent any
environment {
GROOVY_HOME = tool name: '3.0.4', type: 'hudson.plugins.groovy.GroovyInstallation'
}
stages {
stage ('run helloworld.groovy') {
steps {
sh '''
echo 'println "Hello World!"' > helloworld.groovy
${GROOVY_HOME}/bin/groovy helloworld.groovy
'''
}
}
}
}
在我的 windows 机器上,
groovy -version
Groovy Version: 3.0.8 JVM: 16.0.1 Vendor: Oracle Corporation OS: Windows 10
我还有两个 groovy 通过 全局工具配置 安装,名称为 -
Groovy 3.0.5
Groovy 3.0.6
我打算在声明性管道中执行 groovy 代码。
我安装的 Jenkins 随 groovy-all-2.4.12
一起提供,而 groovy.xml.XmlSlurper
下面是一段代码
pipeline {
agent any
tools {
nodejs 'NodeJS'
}
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
stage('Initial-Checks') {
steps {
bat "npm -v"
bat "groovy -v" // gives the one installed on windows(i.e. 3.0.8)
}}
stage('Policy-Code Analysis') {
steps {
script {
def text = '''
<list>
<technology>
<name>Jenkins, Groovy</name>
</technology>
</list>
'''
def list = new XmlSlurper().parseText(text)
assert list instanceof groovy.xml.slurpersupport.GPathResult
println list.technology.name
}
}
}
}
}
运行 这导致
unable to resolve class groovy.xml.XmlSlurper
def list = new groovy.xml.XmlSlurper().parseText(text)
如何将 groovy version 3.0.6
用于包含在 script { ... }
中的代码而不是 Jenkins 使用的版本?
此外,以下不适用于工具
pipeline {
agent any
stages {
stage ("first") {
tools {
groovy "Groovy 3.0.5"
}
steps {
bat 'groovy -version' // gives 3.0.8 and not 3.0.5
}
}
stage("second"){
tools {
groovy "Groovy 3.0.6"
}
steps{
bat 'groovy -version' // gives 3.0.8 and not 3.0.6
}
}
}
}
第一个选项是在不同的阶段使用不同的代理,安装了不同的 groovy 版本(例如 docker 图像)。
pipeline {
agent none
stages {
stage {
agent { label 'some_label' }
steps {
bat 'groovy -version'
}
stage("second"){
agent { label 'another_label' }
steps{
bat 'groovy -version'
}
}
}
}
使用 groovy 工具有一个解决方法。
pipeline {
agent any
environment {
GROOVY_HOME = tool name: '3.0.4', type: 'hudson.plugins.groovy.GroovyInstallation'
}
stages {
stage ('run helloworld.groovy') {
steps {
sh '''
echo 'println "Hello World!"' > helloworld.groovy
${GROOVY_HOME}/bin/groovy helloworld.groovy
'''
}
}
}
}