Spinning up the Jenkins server on your local machine
GitHub, Inc. is an Internet hosting service for software development and version control using Git. It is primarily used for storing, tracking, and collaborating on software projects. It lets you and others collaborate on projects from any part of the world.
Go to https://github.com/<YourUserId>
to access your GitHub home page.
To create a repository, move to the Repositories
section.
Click on the New
button. It will take you to a new page where you will fill out your repository details.
Provide a Repository name
and a suitable Description
. Select the Public
option to make the repository visible to everyone.
Keep the rest of the settings as default and click on the Create repository
button, located at the bottom of the screen.
Click on creating a new file
option to start creating your own python file.
We will be creating a trivial Python code file. Provide the name of the file with a .py
extension and type the following lines of code:
print('This repo will be integrated to Jenkins job in order to automate the building phase')
Scroll down and click on Commit new file
button.
Great Job! You have added your Python code file to the newly created repository.
We are going to create a Jenkins Freestyle job and integrate it into our GitHub repository. A Freestyle job is a stand-alone collection of steps that are not part of a pipeline. Freestyle jobs help us execute the task without running an entire pipeline.
Access the hosted Jenkins server. The default Jenkins Port is 8080
.
Click on Create a job
button to start creating a job.
Provide the name of your job, Ex. github_integration. Choose Freestyle project
and click on OK
button, located at the bottom of the screen.
Provide a Description
of the job. For self-learning purposes, click on the ?
symbol corresponding to each setting option to understand the setting.
Scroll down to Source Code Management
section and select Git
. Provide the URL of the repository created previously. In order to make Jenkins successfully authenticate with your GitHub repository, you need to provide your GitHub credentials. Select the Add
option.
For the Kind
option, select Username with password
. Enter your Username
and Password
.
Click on the Add
button to successfully add the credentials in Jenkins. Click on the Credentials
dropdown and select the credentials you just added.
Scroll down to the Branches to build
settings and rename the branch to */main
in the Branch Specifier
option. Keep the rest of the settings as defaults. Click on the Save
button, located at the bottom of the page.
We have successfully created and configured a Jenkins job.
Click on Build Now
to initiate a build.
Observe a green tick to the left of the build number, showing the last build was successfully completed. Click on the build number.
Observe the GitHub repository pulled during the execution of the build. It is the same repository that we created previously and provided in the configuration settings of the job. Click on Console Output
located to the left of the screen.
Understand the flow of instructions in the Console Output
.
Well done! You have successfully created a Freestyle Job in Jenkins.
We were manually starting the build by clicking on Build Now
. We will now be automating the builds such that the process starts right after every commit is made to the repository. We will be creating a webhook on our GitHub repository, which will trigger the build process.
Webhooks are most commonly used to simplify communication between two applications, but they can also be used to automate Infrastructure-as-code (IaC) workflows and enable GitOps practices. To set up a webhook from GitHub, we need to provide Jenkins’ URL. But Jenkins is currently hosted on the local machine. That’s exactly where NGROK helps us. It will expose the Jenkins server to the Internet. Note that this is not recommended in a production environment.
To create a ngrok tunnel, enter the command ngrok http 8080
in the terminal. Note that the Jenkins server is running on 8080. From the command prompt, copy the forwarding URL.
To create a webhook, click Settings
on GitHub. Click Webhooks
, followed by Add webhook
.
Paste the forwarding URL in payload URL
and append /github-webhook/
to the URL. Disable
SSL verification and click on Add webhook
button.
We will now be integrating the webhook to our job. In Jenkins, open the job and click Configure
.
Scroll down to Build Triggers
option and check Github hook trigger for GITScm polling
. Click on Save
.
We will now make changes in python code file and trigger a build automatically. In GitHub, click on the .py
file. Click on pencil icon to edit the file.
Add the following print statement:
print('We are now automating the Jenkins build')
Give a commit message and click on Commit Changes
button.
Jump to your Jenkins screen and observe that a build is automatically triggered.
Open the build and click on Console Output
. Observe the first line of the logs and compare it with the previous build that was run in previous task.
Great job! You integrated a webhook into Jenkins Job and automated builds.
Congratulations! You’ve successfully completed this tutorial!
You learned: