Day 25 Task: Complete Jenkins CI/CD Project - Continued with Documentation

Gopal GautamGopal Gautam
3 min read

🔹Task-01

  • Document the process from cloning the repository to adding webhooks, and Deployment, etc. as a README , go through this example

  • A well written readme file will help others to understand your project and you will understand how to use the project again without any problems.

    Integrating Jenkins Freestyle Project with GitHub Webhooks

    This guide will walk you through the process of integrating your Jenkins Freestyle project with GitHub webhooks. This integration will allow Jenkins to automatically build and deploy your project whenever changes are pushed to your GitHub repository.

    Prerequisites:

    Before you begin, ensure that you have the following:

    • A Jenkins server set up and running.

    • Install Docker and Docker Compose

    • A GitHub repository where your project's source code is hosted.

Step1: Create a Jenkins Freestyle Project:

  1. Log in to your Jenkins dashboard.

  2. Click on "New Item" to create a new project.

  3. Enter a name for your project and select "Freestyle project." Click "OK."

Step2: Configure Source Code Management

for private repositories:

  1. In the project configuration, scroll down to the "Source Code Management" section.

  2. Select your version control system (e.g., Git).

  3. Enter the URL of your GitHub repository in the "Repository URL" field.

  4. If your repository is private, you'll need to set up authentication using SSH keys or personal access tokens. Configure this in the "Credentials" section.

  5. Save the project configuration.

for public repositories:

  1. In the project configuration, scroll down to the "Source Code Management" section.

  2. Select your version control system (e.g., Git).

  3. Enter the URL of your GitHub repository in the "Repository URL" field.

  4. No additional authentication is required for public repositories.

  5. Save the project configuration.

Step3: Configure Github Webhooks

  1. Go to your GitHub repository.

  2. Click on "Settings" in the repository menu.

  3. Select "Webhooks" from the left sidebar.

  4. Click on "Add webhook."

  5. In the "Payload URL" field, enter the URL of your Jenkins server followed by /github-webhook/. For example: https://your-jenkins-server/github-webhook/.

  6. Set the "Content type" to "application/json."

  7. In the "Which events would you like to trigger this webhook?" section, choose the events that should trigger the webhook. Typically, you'll select "Just the push event."

  8. Click "Add webhook" to save your webhook configuration.

Step4: Add Current User to Docker Group

  1. Open a terminal and run the following command to add the current user to the Docker group (replace <username> with your username):

     sudo usermod -aG docker <username>
    

    This step allows you to use Docker without sudo privileges.

Step5: Add Jenkins to Docker Group

  1. To allow Jenkins to use Docker, you need to add the Jenkins user to the Docker group. Run the following command:

     sudo usermod -aG docker jenkins
    

    This step enables Jenkins to execute Docker commands.

Step6: Configure Jenkins Build Triggers

  1. In your Jenkins project configuration, scroll down to the "Build Triggers" section.

  2. Check the box that says "Build when a change is pushed to GitHub."

  3. Save the project configuration.

Step7: Test the Intergration

  1. Push a change to your GitHub repository (e.g., make a code commit).

  2. Visit your Jenkins dashboard.

  3. You should see your Jenkins project automatically triggered by the GitHub webhook.

  4. Monitor the build progress and view build logs in the Jenkins dashboard.

  5. To update your project, execute the following commands in your Jenkins project's "Execute Shell" build step:

     docker-compose down
     docker-compose up -d --build
    

Congratulations! You've successfully integrated your Jenkins Freestyle project with GitHub webhooks. Now, every time you push changes to your GitHub repository, Jenkins will automatically build and deploy your project.

For any issues or further customization, refer to the Jenkins documentation or GitHub webhooks documentation.

0
Subscribe to my newsletter

Read articles from Gopal Gautam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gopal Gautam
Gopal Gautam

Hii I am a backend/DevOps engineer.I have a experience with development and automation.I mostly work with Python, django, Cloud based technologies.