junelane
junelane

Reputation: 151

How to include Jenkins build numbers to docker images build via CI

I am creating a Jenkins pipeline for building multiple microservices and to create docker images and push them to a private Docker registry.

The docker images are created via Gradle build via docker plugin and I am even able to push the images.

I need help with the following questions.

  1. When the images are built via Jenkins how to tag images to a specific Jenkins build? In other words, I would want to maintain the docker images for each build and deliver the docker images belong to a specific build to QA based on build #.
  2. If multiple images are maintained, how do I conditionally prune the obsolete images? Say, I want to clean up the images of last 10 build but would want to keep the images of the build which is tagged with a release or milestone.

Upvotes: 0

Views: 2320

Answers (3)

chetan mahajan
chetan mahajan

Reputation: 813

Use %BUILD_NUMBER% in windows and ${BUILD_NUMBER} in linux

You can do like mentioned below. eg: docker build -t image-name:%BUILD_NUMBER%

Upvotes: 1

junelane
junelane

Reputation: 151

Basically, Jenkins exposes BUILD_NUMBER env variable, we can simply read it in the gradle file like below and use the same to name the docker images


    buildscript {
        ext {
            springBootVersion = "2.1.3.RELEASE"
            dependencyManagementVersion = "1.0.7.RELEASE"
            gradleDockerVersion = "0.13.0"
            spotBugsVersion = "1.7.1"
            guavaVersion = "27.0.1-jre"
            nodeVersion = "1.3.1"
            buildNumber = System.getenv("BUILD_NUMBER")!=null? System.getenv("BUILD_NUMBER") : "latest"
        }
    .....
    .....
    .....
}

Upvotes: 0

fly2matrix
fly2matrix

Reputation: 2477

1- For the tagging purpose you can use jenkin environment variables like BUILD_NUMBER

For detail check following link : https://wiki.jenkins.io/display/JENKINS/Building+a+software+project

2- For maintaining max number of images that you want to hold in the docker registry depends on the provider.

For example : Artifactory from jFrog
https://www.jfrog.com/confluence/display/RTF/Docker+Registry

Set Max Unique Tags. This specifies the maximum number of unique tags, per repository, that should be stored for a Docker image. Once the number of tags for an image exceeds this number, older tags will be removed. Leaving the field blank (default) means all tags will be stored.

Upvotes: 0

Related Questions