Jenkins: Something about triggers and how to create a simple job with GitHub

Vishugoyal
10 min readJan 17, 2022

Something about the triggers option in Jenkins

In a very easy language, we can say that job which is automatically do anything for ourself with some set of operations.
we will run a job manually in Jenkins there is challenges facing without trigger i.e. human beings build the job manually. So challenges were resolved by the help of the trigger. It has the option to set the periodically run or build the job.

To create a new job or items

Click the new item or create a new job.

Fill the item name and then click freestyle project and the click to the OK button.. and we use Github plugin to integrate our code with Jenkins

Some about the Github :

Create an account on Github and also download the git bash and git
open the git bash

open the git bash

create a new repo in Github and then clone this repo with the help of cmd in git bash.

click new add the new repo.

fill the repository name , remember for starting the mode of the repository is public and click on the readme file and then click on the create repository...

Click the top middle right green box code.

Now you copy the link of the repository in //HTTP mode only .
Now switch to the git bash program.
Type the cmd
#git clone link-of-repository-you-copy-from-github

Now move to that folder in that repo..

Now create a new file.py

now you write the code in your file
Press “i” to change the mode of the file in the insert mode then write the code and then press ESC then type “:wq” to save the file

To use the #cat cmd to read the file

#git status
#git add my_code_file.py
#git commit -m “new code is added” my_code_file.py

Before pushing the code on the Github
#git config user.name “your username
#git config user.password “your password

Now the time to push your code on the Github profile

USE the cmd
#git push

Finally your code push on the Github profile and added to the online open-source tool

let's try to build a small and basic job in Jenkins Without Trigger

Triggers :

Triggers help a lot to run the job automatically means build the job
Types of triggers

  • Trigger build remotely (eg: from a script)
  • build after other projects 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 the 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 your 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 the scheduler field and builds the project if new commits were pushed since the last build.

Build periodically:
Under Build TriggersBuild periodicallySchedule you can create a schedule (or multiple schedules) for Jenkins to build periodically or on a specific date/time. It might be tests that should be run periodically (every morning for example) or a DB clean-up Jenkins job or any other Jenkins job. In this mode we are facing some challenges like it always download the code when the schedule was run .

Now we created a project with trigger build periodically

Same steps as above but when we configure than in the trigger option should be clicked on the build periodically

Syntax of the schedule of the build periodically
min hours date month day
* * * * * means every one min
* 10 * * * means every 10 min

Poll SCM :
It is similar to build periodically but 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, whereas build periodically builds the project periodically even if nothing has changed.But in this trigger, we face some challenges i.e this trigger run always but downloads the code only when some changes take place. This is a save of time and source of CPU.

Now we created a job with the use of the plugin git
Steps already done do again to create a new job
* click on the new item in Dashboard
*Enter an item name
*click on the Freestyle project
*click ok

When you move on the configure page some steps
* Click the Source Code Management tab
* if you already download the git plugin then the option was to come under the source code management like NONE and GIT click on the git option...
* And further you fill your repository link in the URL bar of the SCM under
Preinstall git plugin and also install git in your base os
by using yum install git

When you build now the job then the new history was created and in the workshop space have download all the file which was on your git repo.

Github hooks trigger from gitscm polling :
In Build Triggers select GitHub hook trigger for GITScm polling. When Jenkins will receive the PUSH GitHub hook, it will trigger Git SCM polling logic which will start a new Jenkins build, with the updated code. We use ngrok to expose local Jenkins to the internet so that Github can send the webhooks
It only downloads the code when some changes take place and build the job.

HOW TO RUN PYTHON JOB IN JENKINS

First of all, in your base, OS have installed the pip in which your Jenkins run like your rhel8 and also install pyhton3 in your base OS

First, install python 3 then install the pip command in your base os

now you run a single command in your base os
pip install Appium-Python-Client

then in execute shell you write the cmd like
python3 -m unittest <python_file_name.py>

Now enjoy your trigger with Jenkins first job

The further apart on the next blog continue link given below :

--

--

Vishugoyal

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