R. Barrett
R. Barrett

Reputation: 859

How To Auto-Tag A Docker Image with Git Commit Hash through a Jenkinsfile Pipeline

So I have a Jenkins instance that I need to automatically tag a Docker Image with using a Jenkinsfile Pipeline that automatically tags the image with the commit hash and then pushes it to the Docker Repository. Jenkins is configured correctly, but my pipeline is still failing. At first, I tried using the following command, which returns the current commit hash of my repository.

git rev-parse --short=10 HEAD

Then I noticed this was returning more than one line, so I started using this:

git rev-parse --short=10 HEAD | tail -n +2

Which returns a commit hash similar to this:

338fcaa318b17c40dacf81dcf7a5826e3e3f0160

My goal is to tag my docker images with this hash using a Jenkinsfile:

pipeline {
    agent any
    environment {
        tag = sh(returnStdout: true, script: "git rev-parse -short=10 HEAD | tail -n +2")
    }

    stages {
        stage('Build core lodestone image') {
            steps {
                // TODO: proper tagging
                sh "docker build -f Dockerfile.lodestone -t mirantiseng/lodestone:${var.tag} ."
                withCredentials([usernamePassword(credentialsId: 'common-dockerhub-up', usernameVariable: 'HUB_USER', passwordVariable: 'HUB_PASS')]) {
                    sh "docker login -u ${HUB_USER} -p ${HUB_PASS} && docker push mirantiseng/lodestone:${var.tag}"
                }
            }
        }
        stage('Build core lodestone-comment image') {
            steps {
                // TODO: proper tagging
                sh "docker build -f Dockerfile -t mirantiseng/lodestone-comment:${var.tag} ."
                withCredentials([usernamePassword(credentialsId: 'common-dockerhub-up', usernameVariable: 'HUB_USER', passwordVariable: 'HUB_PASS')]) {
                    sh "docker login -u ${HUB_USER} -p ${HUB_PASS} && docker push mirantiseng/lodestone-comment:${var.tag}"
                }
            }
        }
        stage('Build core lodestone-mover image') {
            steps {
                // TODO: proper tagging
                sh "docker build -f Dockerfile.mover -t mirantiseng/lodestone-mover:${var.tag} ."
                withCredentials([usernamePassword(credentialsId: 'common-dockerhub-up', usernameVariable: 'HUB_USER', passwordVariable: 'HUB_PASS')]) {
                    sh "docker login -u ${HUB_USER} -p ${HUB_PASS} && docker push mirantiseng/lodestone-mover:${var.tag}"
                }
            }
        }
    }
}

The build works if I take out the :${var.tag} and the following block, but it just pushes latest. This leaves the working file looking like this:

pipeline {
    agent any

    stages {
        stage('Build core lodestone image') {
            steps {
                // TODO: proper tagging
                sh "docker build -f Dockerfile.lodestone -t mirantiseng/lodestone ."
                withCredentials([usernamePassword(credentialsId: 'common-dockerhub-up', usernameVariable: 'HUB_USER', passwordVariable: 'HUB_PASS')]) {
                    sh "docker login -u ${HUB_USER} -p ${HUB_PASS} && docker push mirantiseng/lodestone"
                }
            }
        }
        stage('Build core lodestone-comment image') {
            steps {
                // TODO: proper tagging
                sh "docker build -f Dockerfile -t mirantiseng/lodestone-comment ."
                withCredentials([usernamePassword(credentialsId: 'common-dockerhub-up', usernameVariable: 'HUB_USER', passwordVariable: 'HUB_PASS')]) {
                    sh "docker login -u ${HUB_USER} -p ${HUB_PASS} && docker push mirantiseng/lodestone-comment"
                }
            }
        }
        stage('Build core lodestone-mover image') {
            steps {
                // TODO: proper tagging
                sh "docker build -f Dockerfile.mover -t mirantiseng/lodestone-mover ."
                withCredentials([usernamePassword(credentialsId: 'common-dockerhub-up', usernameVariable: 'HUB_USER', passwordVariable: 'HUB_PASS')]) {
                    sh "docker login -u ${HUB_USER} -p ${HUB_PASS} && docker push mirantiseng/lodestone-mover"
                }
            }
        }
    }
}

But I need the docker images to be tagged. I read up on the Jenkinsfile and it said I could make an environmental variable using environment {<something>} to set global variables. I have a variable called tag that I would like to implement that tags the docker images with the commit hash. How can I accomplish this?

Upvotes: 2

Views: 9238

Answers (2)

Dashrath Mundkar
Dashrath Mundkar

Reputation: 9184

You could have used is just $GIT_COMMIT like this

sh "docker build -f Dockerfile.mover -t mirantiseng/lodestone-mover:${GIT_COMMIT} ."

Upvotes: 2

R. Barrett
R. Barrett

Reputation: 859

So all I had to do was change the way tag was defined:

pipeline {
    agent any
    environment {
        tag = sh(returnStdout: true, script: "git rev-parse --short=10 HEAD").trim()
    }

    stages {
        stage('Build core lodestone image') {
            steps {
                // TODO: proper tagging
                sh "docker build -f Dockerfile.lodestone -t mirantiseng/lodestone:${tag} ."
                withCredentials([usernamePassword(credentialsId: 'common-dockerhub-up', usernameVariable: 'HUB_USER', passwordVariable: 'HUB_PASS')]) {
                    sh "docker login -u ${HUB_USER} -p ${HUB_PASS} && docker push mirantiseng/lodestone:${tag}"
                }
            }
        }
        stage('Build core lodestone-comment image') {
            steps {
                // TODO: proper tagging
                sh "docker build -f Dockerfile -t mirantiseng/lodestone-comment:${tag} ."
                withCredentials([usernamePassword(credentialsId: 'common-dockerhub-up', usernameVariable: 'HUB_USER', passwordVariable: 'HUB_PASS')]) {
                    sh "docker login -u ${HUB_USER} -p ${HUB_PASS} && docker push mirantiseng/lodestone-comment:${tag}"
                }
            }
        }
        stage('Build core lodestone-mover image') {
            steps {
                // TODO: proper tagging
                sh "docker build -f Dockerfile.mover -t mirantiseng/lodestone-mover:${tag} ."
                withCredentials([usernamePassword(credentialsId: 'common-dockerhub-up', usernameVariable: 'HUB_USER', passwordVariable: 'HUB_PASS')]) {
                    sh "docker login -u ${HUB_USER} -p ${HUB_PASS} && docker push mirantiseng/lodestone-mover:${tag}"
                }
            }
        }
    }
}

The main change was this

tag = sh(returnStdout: true, script: "git rev-parse --short=10 HEAD").trim()`

Upvotes: 1

Related Questions