Jenkins 3 : Some about triggers

Vishugoyal
5 min readMar 27, 2021

DAY 3 Some about triggers

what is triggers in jenkins:
Triggers helps to build the jobs automatically that helps the process to be automate

if you set the trigger then the job will run according to the schedule or periodically

  • Trigger build remotely (eg: from script)
  • build after other project are build
  • build periodically
  • github hooks trigger from gitscm polling
  • Poll Scm
You can see the build triggers option in job's -> Configure setting.

These are the most common Jenkins build triggers:

  • Trigger builds remotely
  • Build after other projects are built
  • Build periodically
  • GitHub hook trigger for GITScm polling
  • Poll SCM

1. Trigger builds remotely :

If you want to trigger your project built from anywhere anytime then you should select Trigger builds remotely option from the build triggers.

You’ll need to provide an authorization token in the form of a string so that only those who know it would be able to remotely trigger this project’s builds. This provides the predefined URL to invoke this trigger remotely.

predefined URL to trigger build remotely: JENKINS_URL/job/JobName/build?token=TOKEN_NAMEJENKINS_URL: the IP and PORT which the Jenkins server is running
TOKEN_NAME: You have provided while selecting this build trigger.//Example:
http://e330c73d.ngrok.io/job/test/build?token=12345

Whenever you will hit this URL from anywhere you project build will start.

2. Build after other projects are built

If your project depends on another project build then you should select Build after other projects are built option from the build triggers.

In this, you must specify the project(Job) names in the Projects to watch field section and select one of the following options:

1. Trigger only if the build is stable
Note: A build is stable if it was built successfully and no publisher reports it as unstable2. Trigger even if the build is unstable
Note: A build is unstable if it was built successfully and one or more publishers report it unstable3. Trigger even if the build fails

After that, It starts watching the specified projects in the Projects to watch section.

Whenever the build of the specified project completes (either is stable, unstable or failed according to your selected option) then this project build invokes.

Build periodically:

If you want to schedule your project build periodically then you should select the Build periodically option from the build triggers.

You must specify the periodical duration of the project build in the scheduler field section

This field follows the syntax of cron (with minor differences). Specifically, each line consists of 5 fields separated by TAB or whitespace:

MINUTE HOUR DOM MONTH DOW

MINUTEMinutes within the hour (0–59)HOURThe hour of the day (0–23)DOMThe day of the month (1–31)MONTHThe month (1–12)DOWThe day of the week (0–7) where 0 and 7 are Sunday.

To specify multiple values for one field, the following operators are available. In the order of precedence,

  • * specifies all valid values
  • M-N specifies a range of values
  • M-N/X or */X steps by intervals of X through the specified range or whole valid range
  • A,B,...,Z enumerates multiple values

Examples:

# every fifteen minutes (perhaps at :07, :22, :37, :52)
H/15 * * * *
# every ten minutes in the first half of every hour (three times, perhaps at :04, :14, :24)
H(0-29)/10 * * * *
# once every two hours at 45 minutes past the hour starting at 9:45 AM and finishing at 3:45 PM every weekday.
45 9-16/2 * * 1-5
# once in every two hours slot between 9 AM and 5 PM every weekday (perhaps at 10:38 AM, 12:38 PM, 2:38 PM, 4:38 PM)
H H(9-16)/2 * * 1-5
# once a day on the 1st and 15th of every month except December
H H 1,15 1-11 *

After successfully scheduled the project build then the scheduler will invoke the build periodically according to your specified duration.

GitHub webhook trigger for GITScm polling:

A webhook is an HTTP callback, an HTTP POST that occurs when something happens through a simple event-notification via HTTP POST.

GitHub webhooks in Jenkins are used to trigger the build whenever a developer commits something to the branch.

Let’s see how to add build a webhook in GitHub and then add this webhook in Jenkins.

  1. Go to your project repository.
  2. Go to “settings” in the right corner.
  3. Click on “webhooks.”
  4. Click “Add webhooks.”
  5. Write the Payload URL as
http://e330c73d.ngrok.io/github-webhook
//This URL is a public URL where the Jenkins server is running

Here https://e330c73d.ngrok.io/ is the IP and port where my Jenkins is running.

If you are running Jenkins on localhost then writing https://localhost:8080/github-webhook/ will not work because Webhooks can only work with the public IP.

So if you want to make your localhost:8080 expose public then we can use some tools.

In this example, we used ngrok tool to expose my local address to the public.

To know more on how to add webhook in Jenkins pipeline, visit: https://blog.knoldus.com/opsinit-adding-a-github-webhook-in-jenkins-pipeline/

Poll SCM:

Poll SCM periodically polls the SCM to check whether changes were made (i.e. new commits) and builds the project if new commits were pushed since the last build.

You must schedule the polling duration in the scheduler field. Like we explained above in the Build periodically section. You can see the Build periodically section to know how to schedule.

After successfully scheduled, the scheduler polls the SCM according to your specified duration in scheduler field and builds the project if new commits were pushed since the last build.

--

--

Vishugoyal

I am pursiing B-tech. and also very fond of learning new technology under the guidance of Vimal Daga sir (World Record Holder).