我有一个脚本可以将生成的 RPM 发送到 Nexus OSS 管理器。 Jenkins的阶段是:
stage ('nexus deploy') {
env.REPONAME = "snapshots"
sh '''
mvn deploy:deploy-file -Durl="${env.NEXUS_URL}/$env.REPONAME"
'''
}
我已经设置了环境变量
env.NEXUS_URL
但是用两个变量并排调用它,中间有一个斜线,不知何故没有检测到变量,并且构建失败并出现错误。-Durl="${env.NEXUS_URL}/$REPONAME": bad substitution
最佳答案
你在混淆 groovy
语法和 shell
中的语法.
您可以使用 env.VAR
在 groovy
你可以使用 ${VAR}
之间sh '..'
:
pipeline {
agent any
options {
buildDiscarder(logRotator(numToKeepStr: '3'))
}
environment {
NEXUS_URL = 'https://mynexus.com'
REPONAME = 'myrepo'
}
stages {
stage('test') {
steps {
echo "print env vars in groovy"
echo "my nexus is " + env.NEXUS_URL + " any my repo name is " + env.REPONAME
sh 'echo "env vars in sh"'
sh 'echo "nexus is ${NEXUS_URL} and my repo name is ${REPONAME}"'
}
}
}
}
输出:
[Pipeline] echo
print env vars in groovy
[Pipeline] echo
my nexus is https://mynexus.com any my repo name is myrepo
[Pipeline] sh
[test] Running shell script
+ echo 'env vars in sh'
env vars in sh
[Pipeline] sh
[test] Running shell script
+ echo 'nexus is https://mynexus.com and my repo name is myrepo'
nexus is https://mynexus.com and my repo name is myrepo
在您的情况下,您需要:
mvn deploy:deploy-file -Durl=${NEXUS_URL}/${REPONAME}
关于jenkins - 如何在 jenkins 管道中连接两个 env 变量,中间有一个斜线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47594084/