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 to run Apache Spark 2.1 Driver Program in docker container with bridge mode
  • Can't access storage account
  • How do I enable “debug” logging of the Docker daemon? (Ubuntu 16.04)
  • Runtime.getRuntime().exec on host from inside a Docker process
  • Connecting to a running docker container - differences between using ssh and running a command with “-t -i” parameters
  • Running package manager inside the docker
  • How to create a public key store for microservices?
  • Files generated through docker-compose run web rails g controller do not have permissions to edit
  • Docker port forwarding not working
  • Getting “FATAL: role ”root“ does not exist” when trying to do “docker up”
  • How to access boot2docker-ip from inside a docker container
  • deploying .war on docker container running tomcat
  • 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 – https://wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-Buildsbysourcechanges

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