Continuous Integration With Jenkins & GitHub

Sabina Akter
5 min readDec 4, 2022

Hello Everyone, Welcome to my MediumPage!! In this article I would like to do a demo project on Continuous Integration with Jenkins and GitHub.

Jenkins: Jenkins is an open source automation server. It helps automate the parts of software development related to building, testing, and deploying, facilitating continuous integration and continuous delivery.

Prerequisite:

  • Jenkins install
  • Knowledge on GiHub, Git and basic Python

For Jenkins Installation and Jenkins Setup, please see the attached link Jenkins documentation.

Let’s start to create a Jenkins job. I have created a Git repository and named it Jenkins-CICD-Pipeline. Inside that folder I have created an app.py python file. Inside the app.py file please type the below simple scripts for our Jenkins project:

Simple Python Script

def my_function():

print(“Hello from Jenkins ”)

print(“……………………………”)

my_function()

Attached is the snapshot shows the sample repository.

Github repo

Step 1: Click on ‘New Item’ to create a new Jenkins job.

First, open a browser, navigate to your Jenkins URL, and log in using your Jenkins user name and password.

Then, on the Jenkins Dashboard, click the New Item option on the left-hand side menu.

Jenkins Job Create

Step 2: Enter the name of Jenkins job, Choose ‘Freestyle project’ and Click ‘OK’.

Create Jenkins FreeStyle Project

Step 3: Enter Job Description if necessary(Optional).

Step 4: Please select Source Code Management as ‘Git’ and paste the Git Repository URL on the Repository URL box.

Specifying the GitHub repository URL

Step 5: Scroll down to the Build Triggers section and check the Poll SCM see the attached screenshoot here 👎

Note: schedule is given as ‘* * * * *’ which means to schedule trigger for every minute.

Specify Poll SCM

Now the Job checks Git repository once every minute for changes and triggers Build if any changes made in the Git repository.

Step 6: Now Under Build, Choose ‘Execute Windows batch command’ if you were running Jenkins in windows environment, select ‘Execute shell’ if you were running Jenkins in Linux environment. I am using Executive shell as I am on a mac os.

Specify Execute shell

Step 7: Enter the commands for the build and click save.

Enter #!/bin/sh inside Execute shell

The Jenkins project is created now.

Jenkins project created

Final Step: Continuous Testing Using Jenkins

Now let’s commit into the repository. Here ‘app.py’ file is changed and committed.

Added some lines into the app.py file

Jenkins Build automatically triggered on code commit.

Auto trigged by Jenkins

The output is displayed on the console output like below.

Console Output without print statement

Note: As you see my console output didn’t print the print statement, I did little research on it (url in the bottom on the reference section). Then I figured out a way. I put the below script on the Build stage in Execute shell

#! /bin/sh

python3 -u app.py

Then I see the following output on the console that prints my print statement successfully on the console.

Console Output with print statement

Learning from the error:

  • When I run build the project I see the attached error
Build stage failed in Jenkins
  • After doing little research, my finding was the git repo I had, name was */master but actually the git branch name was */main. Then finally I changed the git branch */name into main instead of */master.
git branch name : */master
Git branch name changed into */main

Then observe the auto trigger build successfully the build stage.

Build status successful

Conclusion

In this tutorial, I showed, how Continuous Integration works in Jenkins, and how to troubleshoots on Jenkins.

Thank you everyone 😃, for reading my blog!!

Stay Tuned for my next project!!

👏👏👏👏 Happy Learning!!

If you think, this article is helpful 😄, please give me a clap 👏 👏👏👏 to inspire me. If you need help with Jenkins CI-CD pipeline please see the attached link here.

Please connect me on linkedin 👎

https://www.linkedin.com/in/aktersabina122/

Please follow me on Medium and Twitter 👎:

https://aktersabina122.medium.com/

https://twitter.com/aktersabina122

--

--