我有一个场景,我必须阅读不同组件的 maven pom 版本并将版本分配给 docker image(TAG)。但是在我阅读 pom 后,将它分配给一些全局变量,它将在 groovy jenkins 脚本中重置为原始值。下面是示例。HMAP_VERSION 值将是 1.2.1 但当它在行中使用时:sh "docker login -u ${ART_USERNAME} -p ${ART_PASSWORD} test.com"
该值将是UNINITIALISED
.
有人可以告诉我可能出了什么问题吗?这将适用于在 env 块中读取的单个 maven 文件,如下所示:
environment {
CLOADER_VERSION = readMavenPom().getVersion()
}
下面是我想做什么的示例。
#! groovy
environment {
HMAP_VERSION = "UNINITIALISED"
CLOADER_VERSION = "UNINITIALISED"
}
stages {
stage('Build Cloader') {
steps {
checkout([$class: 'GitSCM' "rest is removed")
dir('isa-casloader') {
script {
CLOADER_VERSION = readMavenPom().getVersion()
}
container('build') {
sh '/opt/apache-maven/bin/mvn -s settings.xml -B clean install -DskipTests=true'
}
}
}
}
stage ('Build Casloader Docker Image') {
steps {
dir('isa-casloader') {
container('tools') {
echo("CLOADER_VERSION=${CLOADER_VERSION}")
withCredentials() {
sh "docker login -u ${ART_USERNAME} -p ${ART_PASSWORD} testing.com"
sh 'docker build -t testing.com:${CLOADER_VERSION} .'
sh 'docker push testing.com:${CLOADER_VERSION}'
}
}
}
}
}
stage ('Build Heat Map Docker Image') {
steps {
checkout([$class: 'GitSCM', "rest is commented"])
dir('apps') {
container('tools') {
script {
def pom = readMavenPom file: 'pom-docker.xml'
HMAP_VERSION = pom.version
}
echo("HMAP_VERSION=${HMAP_VERSION}")
withCredentials() {
sh "docker login -u ${ART_USERNAME} -p ${ART_PASSWORD} test.com"
sh 'docker build -t test.com:${HMAP_VERSION} .'
sh 'docker push test.com:${HMAP_VERSION}'
}}}}}}}