Can';t在Jenkins管道/多管道中使用环境变量
我是詹金斯的新手。今天我尝试创建一个多分支管道。 我想用分支名称标记创建的docker映像 My Jenkins文件锁定如下所示:Can';t在Jenkins管道/多管道中使用环境变量,jenkins,jenkins-pipeline,Jenkins,Jenkins Pipeline,我是詹金斯的新手。今天我尝试创建一个多分支管道。 我想用分支名称标记创建的docker映像 My Jenkins文件锁定如下所示: node { def app stage('Clone repository') { /* Let's make sure we have the repository cloned to our workspace */ checkout scm } stage('Build image') {
node {
def app
stage('Clone repository') {
/* Let's make sure we have the repository cloned to our workspace */
checkout scm
}
stage('Build image') {
/* This builds the actual image; synonymous to
* docker build on the command line */
app = docker.build("brosftw/minecraft")
}
stage('Test image') {
app.inside {
sh 'echo ${BUILD_BRANCHENAME}'
}
}
stage('Push image') {
/* Finally, we'll push the image with two tags:
* First, the incremental build number from Jenkins
* Second, the 'latest' tag.
* Pushing multiple tags is cheap, as all the layers are reused. */
/* Docker credentials from Jenkins ID for BrosFTW Repository */
docker.withRegistry('https://registry.hub.docker.com', 'e2fd9e87-21a4-4ee0-86d4-da0f7949a984') {
/* If Branch is master tag it with the latest tag */
if ("${env.BUILD_BRANCHENAME}" == "master") {
app.push("latest")
} else {
/* If it is a normal branch tag it with the branch name */
app.push("${env.BUILD_BRANCHENAME}")
}
}
}
}
已编辑
来自Jenkins作业日志的docker推送请求:
+ docker tag brosftw/minecraft registry.hub.docker.com/brosftw/minecraft:null
[Pipeline] sh
[Minecraft-Test_master-ATFJUB2KKWARM4FFRXV2PEMHX6QFD24UQ5NGQXBIWT5YQJNXBAIA] Running shell script
+ docker push registry.hub.docker.com/brosftw/minecraft:null
The push refers to a repository [registry.hub.docker.com/brosftw/minecraft]
echo命令的输出如下所示:
[Pipeline] {
[Pipeline] sh
[Minecraft-Test_master-ATFJUB2KKWARM4FFRXV2PEMHX6QFD24UQ5NGQXBIWT5YQJNXBAIA] Running shell script
+
[Pipeline]
有谁能告诉我,我对env变量的错误是什么
我的第二个问题是,app.inside
没有返回分支名称。。。。我不明白为什么
谢谢你的回答 您可以使用
env.branch\u name
访问分支名称。此外,您不需要在字符串中插入变量
因此,最后一部分的工作应如下所示:
docker.withRegistry('https://registry.hub.docker.com','e2fd9e87-21a4-4ee0-86d4-da0f7949a984'){
/*如果分支是主分支,则使用最新标记对其进行标记*/
如果(环境分支机构名称==“主”){
应用程序推送(“最新”)
}否则{
/*如果是普通分支,则用分支名称标记它*/
应用程序推送(环境分支机构名称)
}
}
不确定,为什么您认为该变量名为
BUILD\u BRANCHENAME
。它是分支机构名称。您可以使用管道作业的管道语法链接(然后在“全局变量引用”下)查看此类全局变量列表。您的实际问题是什么?什么不起作用?为我工作。谢谢你的回答!我没有意识到错误的变量。