In this lab, you will learn how to integrate a GitHub repository into a Jenkins Freestyle Project and trigger the build automatically when there are changes to the GitHub repository.
- Jenkins installed and running.
- GitHub repository with proper access.
- Git plugin installed in Jenkins.
- Webhook permission for the GitHub repository.
-
Create a New Freestyle Project
- Log in to Jenkins.
- Click on
New Item
. - Enter a name for your project, select
Freestyle project
, and clickOK
.
-
Configure GitHub Repository
- In the project configuration, scroll down to the Source Code Management section.
- Select
Git
. - In the
Repository URL
field, enter the URL of your GitHub repository (e.g.,https://github.com/<username>/<repo-name>.git
). - If the repository is private, click on
Add
next to Credentials and enter your GitHub username and Personal Access Token (PAT).
-
Branch to Build
- Specify the branch to build, usually
main
ormaster
:- In Branch Specifier, enter
*/main
(replacemain
with the branch name you want to track).
- In Branch Specifier, enter
- Specify the branch to build, usually
- Set Build Triggers
- Scroll down to the Build Triggers section.
- Check the option
GitHub hook trigger for GITScm polling
. - This allows the Jenkins job to be triggered automatically when a change is pushed to the GitHub repository.
-
Go to GitHub Repository
- Navigate to the GitHub repository you want to integrate with Jenkins.
-
Add Jenkins Webhook
- Go to
Settings
>Webhooks
. - Click on
Add webhook
. - In the Payload URL field, enter the URL of your Jenkins server followed by
/github-webhook/
(e.g.,http://<jenkins-server>/github-webhook/
). - Set the Content type to
application/json
. - Choose
Just the push event
or select specific events to trigger the job. - Click on
Add webhook
.
- Go to
- Add a Build Step
- In your Jenkins project configuration, scroll down to the Build section.
- Click
Add build step
, then selectExecute shell
(or any other action based on your project needs). - Enter the shell commands or scripts to be executed during the build. For example:
echo "Building the project..."
-
Save the Project Configuration
- Click on
Save
to save your project settings.
- Click on
-
Trigger a Manual Build (Optional)
- Click
Build Now
on the left side of the project page to verify that the configuration is working.
- Click
-
Automatic Build Trigger
- Push changes to the GitHub repository.
- After pushing changes, Jenkins should trigger the build automatically due to the webhook.
- Check Build Status
- Go to the Jenkins project page and click on
Build History
to see the status of your triggered builds. - You should see a new build triggered by the webhook when you pushed to the GitHub repository.
- Go to the Jenkins project page and click on