How to release docker image with Jenkins pipeline How to release docker image with Jenkins pipeline docker docker

How to release docker image with Jenkins pipeline


First thing is to have a specific repo for each project that you have. This helps not to mess up the docker images while pushing it to the repository.

You can use the shell scripts/commands in the pipeline script to execute all the commands which are mentioned here.

There are multiple ways in which you push the docker images to the repository.

Tagging based on Time and Build Number:

Take an example of auth-api, where we create an env variable called $DOCKER_TAG in the jenkins and set it to the time the build is created:

DOCKER_TAG: $(date +%Y%m%d).$BUILD_NUMBER 

$BUILD_NUMBER is provided as part of Jenkins job. Now tag the docker image as follows:

docker tag DOCKER_IMAGE auth-api/<DOCKER_IMAGE>:<DOCKER_TAG>

(Assuming you have built the docker image and push it to the repository after tagging- above step.)

NOTE : Docker allows to push the image only if it matches the REPO-NAME)

It helps to find out the docker image creation date and time along with the build number. This image can be deployed to different environments like dev, test, prod.

Tagging based on the environment

Tag the docker image based on the deployment environment, i.e dev, test, prod.Configure an environment variable in the pipeline script, using withenv, which may look something like this:

docker tag DOCKER_IMAGE auth-api/<DOCKER_IMAGE>:$ENV

This will help to have a deployment specific docker images.

You can even tag the docker image based on git commit.[not tested personally]