Tagging Docker Images based on branch

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…

Thanks all,

I’m mobile so can’t dig into this much ATM, but hopefully my shared library can help a bit.

If this is all you want to do, you can wrap it in a script {} block, that’ll let you assign variables.

1 Like

Hey Halkeye,
Thanks for responding once again. I super appreciate the input. Is this what you were thinking???

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 {
            script{
            sh "printenv | sort"
            echo "Dev Tag is -- ${env.DEV_TAG}"
            echo "Version is -- ${env.$VERSION}"
            VERSION = "${env.DEV_TAG}"
            }
        }
    }
}

}

Thanks again.

Johnny m

Gavin,
Ill take a look at your shared library… I really appreciate it dude. :slight_smile:

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.

Thanks again, man!

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("list environment variables") {
        steps {
            sh "printenv | sort"
            echo "${DEV_TAG}.latest"
            script{
                if (BRANCH.contains ('origin/develop')) {
                    echo 'branch is develop'
                    $VERSION = "${env.DEV_TAG}.latest"]
                    echo "${env.VERSION}"
                }
            }
        }
    }
}

}

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 https://plugins.jenkins.io/workflow-multibranch/

Hey Gavin,
I tried using the script block and that worked great!!! Thanks man, I super appreciate your guidance on that. :slight_smile:

Johnny M