In this article we will learn how trigger Jenkins Build automatically – Jenkins Pipeline. If you want to see our previous lesson
- https://devclouds.io/blog/run-jenkins-in-docker-container-jenkins-pipeline-1-4/
- https://devclouds.io/blog/create-multibranch-pipeline-with-git-jenkins-pipeline-2-4/
- https://devclouds.io/blog/jenkinsfile-jenkins-pipeline-3-4/
So till now you saw that every time I made changes in the repository. I had to manually scan the pipeline so that the changes could be built. But obviously that’s not very practical because usually you would want this whole process to work automatically.

So whenever there is a change or a new commit into the git repository. You want Jenkins to be notified in the whole process to be triggered and changes builds. So how do you configure all of this.
Push Notification for Trigger Jenkins Build automatically

Now there are two ways that the changes in a git repository can trigger Jenkins built job pipeline etc. So one way is through “Push Notifications” meaning that a source code management system detects the changes. Whenever a new commit comes in it notifies Jenkins about those changes.
Polling for Trigger Jenkins Build automatically
Second way is where Jenkins itself pulls this information in regular intervals so it basically checks itself is their. New change is their new change from the repository itself and in Jenkins you can configure both ways.
Push notifications is actually more efficient. Because the communication happens only when there is a relevant change that needs to be built. But also there’s a common practice to configure a built-in jenkins to pull the changes.
So how do you configure each one of those to configure the push notifications from a git repository. You would need to configure both jenkins in your source code management. So that they can communicate with each other and depending on which source code management’s you use.

I use gitlab here depending on that you would have to install a respective plugin in Jenkins. You just have to write the repository server hostname. And the access token or credentials to access the source code management host. So not the repository itself not the project that you want to build.
Jenkins System Configuration

But the actual host and you should give Jenkins access token of a gitlab user or your source code management user. And that configuration is done here in system configuration.

For example I have getlab one here and I have github here so I’m not going to do it. Because it will depend on which ever source code management you have.
But here you can figure how to connect to that repository host in the same way in your source code management.
GitLab Integrations

If you go to settings and integration or is it here you see have webhooks project hooks right. So yo u basically say that this is gonna be Jenkins URL but not just the host. But a web hook URL of Jenkins so I can show you on let’s here.

For example this is for github and here you can see the Jenkins URL for github webhook. Basically this is a URL where Jenkins will listen to code changes in github repository. And this will be obviously different “forked lab” or “bitbucket or whatever.

This is the URL you’re gonna put here. but since I have my Jenkins running on “localhost” it won’t work. Because I can’t refer to localhost. Because it doesn’t accept localhost it needs a proper domain but this is how you configure it.

You just say by default that you want to listen to all the push events and whenever gitlab.

In this case has new changes like a new commit is made to a project. It’s gonna push that change notification to webhook URL of Jenkins so Jenkins will now know.

Okay there has been a change made in the repository. Let me trigger that built so this is how the whole process works. And a polling request it can be configured inside of that Jenkins job.
Scan Multibranch Pipeline Triggers

So if I go here in my pipeline there is a configuration option. Which is called “scan multibranch pipeline triggers”. And if I click here I can configure how often Jenkins should pull the changes.

So this is an alternative to what I show previously. So if I configure one minute interval this will basically mean that Jenkins will check itself. If there has been any changes in a git repository of the project every one minutes so if I save this.

If I go to dev branch let’s say I make a change to “Jenkinsfile”.

and commit the changes. Now I don’t have to manually scan the build again. I just wait for maximum one minute and it should automatically trigger the scan.

So let’s see how this can work so here say I didn’t do anything. It scan the project itself and it was triggered automatically.

The change I made is here so if I see the logs I can see “Application built”. But of course having it at one minute interval is too much so it’s it’s too much load on Jenkins.
Scan Multibranch Pipeline Interval

So usually you would have you would set it at 15 minutes or maybe half an hour or an hour.
Also what what’s a common practice is that sometimes those webhooks. Or those push notifications can be unreliable when does this happen. For example when Jenkins survey is not available when a source code management system pushes a notification. That notification can gets blocked by a Jenkins firewall.
Like in case something happens in that notification doesn’t go through basically Jenkins doesn’t know that. It should trigger a build so as a sort of a back-up plan for that it’s a common practice to use both and set the interval.
It’s a couple of hours maybe one hour so that Jenkins would itself scan the project again. And this is what I personally would use I would actually configure both and set the interval at one or two hours.