The Official DevClouds Blogs

logo-devclouds

Trigger Jenkins Build automatically – Jenkins Pipeline 4/4

January 16, 2023

In this article we will learn how trigger Jenkins Build automatically – Jenkins Pipeline. If you want to see our previous lesson

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.

Build

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

2 ways to trigger build

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.

gitlab

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

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.

GitHub Server 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

setting and integration

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.

github url

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.

gitlab integrations url

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.

Gitlab to jenkins webhook url

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.

jenkins job configuration

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

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.

1min interval

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.

Gitlab Dev Branch

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

Jenkinsfile changes

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.

auto trigger 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.

logs

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

Intervals

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.

Share This Article

Join our newsletter & get update, It’s That Simple!

Join, Blogs, Discount & Promo code !!!

THERE’S MORE TO READ.

What tools you need to learn to become a devops engineer and what are the tasks and responsibilities of a devops engineer.

| January 17, 2023

kubernetes is an open source container orchestration framework. Which was originally developed by google.

| January 17, 2023

Terraform allows you to automate and manage, your infrastructure, your platform and services that run on that infrastructure.

| January 16, 2023

what is git exactly it's a version control system but really what that is a system for managing your files.

| January 16, 2023

Git with visual studio code. Source control, version control, backing up whatever you choose to call it.

| January 16, 2023

In order to build the application in the package you need some kind of build tools.

| January 15, 2023

Leave a Reply

Your email address will not be published. Required fields are marked *

Cyber​​Power Cloud, fast, protected virtual servers, instant setup.

Spin up your choice of virtual machine in just 55 seconds.

OVH

Cloud VPS & Dedicated Servers, advanced hosting solutions

Amazon Lightsail provides easy-to-use cloud resources for web applications

Managed Google Cloud Virtual Machines

Easily deploy cloud servers and storage worldwide

Cloud VPS & Dedicated Servers, advanced hosting solutions

Cloud optimized for performance equipped with fast NVMe drives.

AWS

Amazon EC2, the broadest and deepest compute platform

Managing complex cloud infrastructure made easy

High-Performance Cloud Storage Solutions

10 Clouds 1 Panel

Fully managed by our team

All your clouds and dedi's

All your control panels (CP,DA)

All your wordpress sites&plugins

All of that in one CyberPower

Mega control panel

Reliable Co-Location in ISRAEL Service

Buy and add server hardware components to your co-located servers.

Attacked? We Can Defend You!

Advanced DevOps Services

All about us and what we do in cyberpower

Contact us to activate your custom affiliate plan

Terms & Conditions, Privecy Policy