-1

I have created a pipeline in Jenkins whose steps are:

  1. git pull(retrieve the app from GitHub)
  2. build (maven package the app)
  3. building an image
  4. running an image

code for running the image is docker run -d --name **application_name** -p 8081:8081 **application_name**

I need an if-else condition to check if the image has been created or not. for eg.

if (image not created)
     create the image
else
     different step

I need this because every time I change something in code, Jenkins throws an error stating that the image is already running.

Mayur Tripathi
  • 69
  • 2
  • 12
  • Can you edit the question and add your current pipeline script? You should be able to rebuild the image even if the container is already running. – David Maze Sep 28 '20 at 12:44

1 Answers1

0

The error might be related to this question.

You could just build the image anyway, since it wouldn't matter much if it was build again.

It you want to check if the image already exists you could use the docker images command and check if the image exists like described here.

It could look something like this:

script {
    def imageExists = sh(script: "docker images -q <image name>", returnStdout: true) == 0

    if(!imageExists){
        // build the image
    }
}
smelm
  • 1,253
  • 7
  • 14