Hello,
Im newer to writing pipelines with a Jenkinsfile… I have a question regarding setting an environment variable to something different based on a condition… From what I understand global variables can not be changed within a stage do to scoping… Im basically trying to tag docker images based on the branch they are built off of…
For feature/* Im setting the image to be tagged with ..<BUILD_NUMBER>. Which works great… The next leg of the journey is to tag develop branches the same, but appending .latest to the tag, e.g ..<BUILD_NUMBER>.latest
Currently my Jenkinsfile looks like this:
pipeline {
agent any
environment {
COMMIT_HASH = sh(returnStdout: true, script: 'git rev-parse --short=4 HEAD').trim()
BRANCH = "${env.GIT_BRANCH}"
TAG = "${env.BRANCH}.${env.COMMIT_HASH}.${env.BUILD_NUMBER}".drop(15)
DEV_TAG = "${env.BRANCH}.${env.COMMIT_HASH}.${env.BUILD_NUMBER}".drop(7)
VERSION = "${env.TAG}"
}
stages {
stage("checkout") {
when {
branch "origin/develop"
}
steps {
sh "printenv | sort"
echo "Dev Tag is -- ${env.DEV_TAG}"
echo "Version is -- ${env.$VERSION}"
VERSION = "${env.DEV_TAG}"
}
}
}
}
I’m hoping I can get some direction or help on how to achieve this… I super appreciate any direction…
Gavin,
Ill take a look at your shared library… I really appreciate it dude.
At this point, Im getting back all the values that I want… The last thing I need to do is just name the image when on develop branch… I’m going to checkout your lib and see if I can get it figured out… My code is listed below at the moment… but, Im hoping I can figure out how you may have tagged your images.
that’ll work, you only need script around the assignment script { VERSION = "${env.DEV_TAG}" } not the full block, but i don’t thnk it’ll really make a difference though. You may need do script { env.VERSION = "${env.DEV_TAG}" }
For my script, I build every commit, and publish a hash for it, then when the repo is tagged, it triggers a new job via the multibranch pipeline with tags enabled. see Pipeline: Multibranch