How To Build Job In Jenkins

WHAT IS A JOB?

A job in jenkins is like a project which contains steps and post-build actions. There are various ways for creating a job, few of which are mentioned here as below.

  1. build a job normally
  2. build a job using poll scm
  3. build a job periodically
  4. build a job using github webhook

For creating your job in jenkins please follow the steps below:-

Build a Jenkins job normally

1. If you go to Jenkins Dashboard then click on New Item.

2. Enter the item name, and select the ‘Freestyle project’ option. And click OK.

3. On the Source Code Management section, select the Git option, and specify the GitHub Repository URL.

4. Add the Repository URL in the Source Code Management section.

buil jenkins job normally

Remember:- if your GitHub repository is private, Jenkins will first validate your login credentials with GitHub and only then access the source code from your GitHub repository, enter the git clone url in scm.

There are various SCM options available like GIT, SVN but in this case for demo purpose we are using GIT as SCM.

5. Now, it is the time to build the code. Click on “Add build step” and select the then “Execute Shell”.

6. Enter the command to perform build (we execute simple linux command over here)

ls
mv index.html/ /var/www/html

add build step

Note: Here we are deploying index.html through apache 2 in ubuntu, if apache is pre-installed on your system then it is fine, else execute the below command to install apache2.

sudo apt update -y && sudo apt install apache2 -y

install apache2

7. Click Apply and then Save button.

8. Once you saved the configuration, then you can click on Build Now option.

Once the build is completed, the status of the build will be visible that the build was successful or not. Build which is failed, are represented with red color. Blue symbol is for success.

configuration build

9. Click on Console Output from the left side of the screen to see the status of the build you run. It should show the success message.

build run

After this go to browser and hit server ip then you will get the result of your deployment, shown
as below :

Build a Jenkins job using poll SCM

1. Go to the configure page of a project.

2. Go to the “Build Triggers” section.

3. Check “Poll SCM”.

4. Enter the schedule that you want in cron-style.

The help that placed right side of input area might be useful

Example for poll at every 15 minutes is here as below:

poll scm

5. Save and Run the job.

When any change is detected at polling, we can see that message in the log of Jenkins.

Build a Jenkins job periodically

1. Go to the configure page of a project.

2. Go to the “Build Triggers” section.

3. Check “Build Periodically”.

4. Enter the cron expression.

  • Example: 20 04 * * *.

run job build trigger

5. Save and Run the job.

Now the job will run at 4:20 am everyday.

Build a Jenkins job using Git webhook

1. Click on the job name, select configure and scroll down to build triggers.

configure build trigger

Click on the checkbox for GitHub hook trigger

2. Open your GitHub repository and navigate to the “Settings” tab.

github repository setting

3. Select the “Webhooks” option on the left menu bar.

webhook

4. Click “Add Webhook”

Add Webhook

5. For “Payload URL”:

6. Select “application/json” as the encoding type.

7. Leave “Secret” blank (unless a secret has been created and configured in the Jenkins “Configure System -> GitHub plugin” section).

8. Select “Let me select individual events”.

  • Enable PUSH event.

9. Make sure “Active” is checked.

application/json

10. Click “Add Webhook”.

Jenkins will now receive push notifications for that repository, and related builds will be automatically triggered.

Subscribe Now