Build Docker image only if Code Repository has been updated (Jenkins)

Currently, in a Jenkins job, I build a Docker image with files from a git repository. When this repository is changed/updated I would like to either:

  1. Be able to detect this in Jenkins so I can change my Bash script to rebuild the Docker image from my Dockerfile
  2. Find some other method of building my Docker image that does not require me to build it every time I run the Jenkins job (I run the job hourly, and it takes nearly 10 minutes to build the Docker image)

What is a clean and efficient solution to this? Should I just build the image every time I run the job after all?

  • How do I deploy updated Docker images to Amazon ECS tasks?
  • jax-rs client is not creating jax-rs cookie
  • Can I run Docker-in-Docker without using the --privileged flag
  • Docker run command from git repo?
  • How do I run mysqld as root in the official MySql docker image?
  • serving multiple docker microservices behind nginx proxy
  • No value has been specified for property 'imageId' while creating container
  • What is wrong in my docker-compose.yml file?
  • apache marathon: my docker image keeps failing
  • docker cannot start container after reboot use '--restart-always'
  • Symfony - Unknown MySQL server host when using command line
  • Dockerfile manual install of multiple deb files
  • 2 Solutions collect form web for “Build Docker image only if Code Repository has been updated (Jenkins)”

    This one is simple, first what are you using to host your git? hopefully github / gitlab? if so, “polling” SCM like @amit mentioned is overkill.

    why let jenkins look for changes, when the git server can alert jenkins on changes?

    What you want to do is set up a jenkins job that builds a docker image for you

    then on either github/gitlab, set up a web hook (project settings -> web hooks) to the jenkins project.

    this way, when you push changes to GH/GL, it will send a payload to jenkins, which would then run your build project

    You can configure a “Poll SCM” build trigger – this will cause Jenkins to periodically check your git repository for changes and the build step will be triggered only if changes are detected. (I assume your build step will be doing the docker build).

    You can find more information here –

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.