Reputation: 1149
I am trying to build a jenkins job(trigger builds remotely) on docker image build, build all I am getting on docker hub is following:
HISTORY
ID Status Date & Time
7345... ! ERROR 10/12/17 10:03
Reason (I assume): Docker is not authenticated to post to the jenkins url.
Question: How can I trigger the job automatically when an image gets pushed to docker hub?
Upvotes: 1
Views: 4596
Reputation: 1
Go to Jenkins and click on your profile at the top right corner. Select "Configure" to access your Jenkins configuration. Create an API token by installing the necessary plugin if it's not already installed. Save the API token you generated (e.g., 5599665asc58866544dsc64121). Now, let's configure the build trigger section in your pipeline:
Go to your pipeline configuration. Check the "Trigger builds remotely (e.g., from scripts)" checkbox. Enter a random number (e.g., 123456789) and save the configuration. Next, let's create a webhook in Docker Hub:
Go to Docker Hub's webhook section. Create a new webhook.
Enter the following URL format: http://jenkinsusername:5599665asc58866544dsc64121@jenkinsurl/job/job-name/build?token=123456789
Here's an example URL:http://admin:[email protected]:8080/job/front-demo/build?token=123456789
Upvotes: 0
Reputation: 6831
Run a cron
job with a periodic docker search
to list all tags in the docker image of interest (here's the script). Note that this script requires the substitution of the jannis/jq
image with an existing image (e.g. docker run --rm -i imega/jq
).
Save resulting tags list to a file, and monitor it for changes (e.g. with inotifywait).
Fire a POST request using curl
to your Jenkins server's endpoint using Generic Webhook Trigger plugin.
Cautions:
for efficiency reasons this tags listing script should be limited to a few (say, 3) top pages or simple repos with a few tags,
image tag monitoring relies on tags being updated correctly (automatically) after each image change, rather than being stuck in the past, like say Ubuntu tags (e.g. trusty-20190515
was updated a few days ago - late November, without the change in its mid-May tag).
Upvotes: 0
Reputation: 6831
Pull and run Watchtower docker image to poll any third-party public Docker image on Docker Hub or Quay that you need (typically as a base image of your own containers). Here's how. "Polling" here does not imply crudely pulling the whole image every 5 minutes or so - we are monitoring periodically for changes in the image, downloading only the checksum (SHA digest) most of the time (when there are no changes in the locally cached image).
Install the Build Token Root Plugin in your Jenkins server and set it up to receive Slack-formatted notifications secured with a token to trigger builds remotely or - safer - locally (those triggers will be coming from Watchtower container, not Slack). Here's how.
Set up Watchtower to post Slack messages to your Jenkins endpoint upon every change in the image(s) (tags) that you want. Here's how.
Optionally, if your scale is so large that you could end up overloading and bringing down the entire Docker Hub with a flood HTTP GET requests (should the time triggers go wrong and turn into a tight loop) make sure to build in some safety checks on top of Watchtower to "watch the watchman".
Upvotes: 1
Reputation: 6831
Docker Hub webhooks targeting your Jenkings server endpoint require making periodic copies of the image to another repo that you own [see my other answer with Docker Hub -> Watchman -> Jenkins integration through Slack notifications].
You need to set up a cron job with periodic polling (docker pull
) of the source repo to [docker] pull its `latest' tag, and if a change is detected, re-tag it as your own and [docker] push to a repo you own (e.g. a "clone" of the source Docker Hub repo) where you have set up a webhook targeting your Jenkings build endpoint.
Then and only then (in a repo you own) will Jenkins plugins such as Docker Hub Notification Trigger work for you.
As a substitute of polling the registry for image changes (which need not generate much network traffic thanks to the local cache of docker images) you can also poll the source Dockerfile on Github using wget
. For instance Dockerfiles of the official Docker Hub images are here. In case when the Github repo makes releases, you can get push notifications of them using Github Watch > Releases Only feature and if they have CI docker builds. Docker images will usually be available with a delay after code releases, even with complete automation, so image polling is more reliable.
There was also a proposal for a 2019 Google Summer of Code project called Polling Docker Registries for Image Changes that tried to solve this problem for Jenkins users (incl. apparently Google), but sadly it was not taken up by participants.
Upvotes: 0
Reputation: 594
You can configure a WebHook in DockerHub wich will trigger the Jenkins-Build.
Upvotes: 0
Reputation: 114
You can try the following plugin: https://wiki.jenkins.io/display/JENKINS/CloudBees+Docker+Hub+Notification
Which claims to do what you're looking for.
Upvotes: 0