Reputation: 13
I'm new to Jenkins. Today I tried to create a Multibranche Pipeline. I would like to tag the created docker image with the branch name.
My Jenkins file locks like follows:
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}")
}
}
}
}
Edited
docker push request from Jenkins job log:
+ 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]
And the output of the echo command is the following:
[Pipeline] {
[Pipeline] sh
[Minecraft-Test_master-ATFJUB2KKWARM4FFRXV2PEMHX6QFD24UQ5NGQXBIWT5YQJNXBAIA] Running shell script
+
[Pipeline]
Can anyone tell me what I'm doing wrong with the env variables?
My second Issue is, that the app.inside
doesn't return the branch name.... and I don't understand why.
Thanks for every answer.
Upvotes: 0
Views: 1348
Reputation: 37650
You can access the branch name using env.BRANCH_NAME
. Further, you don't need interpolate variables inside strings.
So the last part should work as follows:
docker.withRegistry('https://registry.hub.docker.com', 'e2fd9e87-21a4-4ee0-86d4-da0f7949a984') {
/* If Branch is master tag it with the latest tag */
if (env.BRANCH_NAME == "master") {
app.push("latest")
} else {
/* If it is a normal branch tag it with the branch name */
app.push(env.BRANCH_NAME)
}
}
Not sure, why you thought the variable is called BUILD_BRANCHENAME
. It's BRANCH_NAME
. You can see such list of global variables using the Pipeline Syntax link of a pipeline job (and then under Global Variables Reference).
Upvotes: 1