πŸš€πŸ“… Day 24 DevOps Challenge - Unleashing Jenkins Power: Seamlessly Build, Deploy, and Automate with WebHooks πŸš€

πŸš€πŸ“… Day 24 DevOps Challenge - Unleashing Jenkins Power: Seamlessly Build, Deploy, and Automate with WebHooks πŸš€

Β·

4 min read

βš“Task-01: πŸš€ Getting Started with Jenkins and GitHub for CI/CD using GitHub Webhooks 🌐

Step 1: Fork the Repository

Step 2: Create a Freestyle Project in Jenkins

  • Log in to your Jenkins dashboard.

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

  • Enter a name for your project and select "Freestyle project," then click "OK."

Step 3: Configure Repository URL

  • In the project configuration, scroll down to the "Source Code Management" section.
  • Select "Git" and paste the URL of your forked repository.

Step 4: Set Up Source Code Management

  • In the "Build Triggers" section, select "GitHub hook trigger for GITScm polling." This will enable Jenkins to listen for GitHub webhook events.

Step 5: Connect Jenkins to the GitHub Repository

  • Go back to your Jenkins project's dashboard.

  • In the left-hand menu, click on "Configure" to edit the project settings.

  • Scroll down to the "Build" section and click on "Add build step."

  • Choose "Execute shell" and enter the command that you want Jenkins to execute after the job is built. This could be your build and deployment script.

Step 6: Configure Webhook in GitHub

  • Open your forked GitHub repository.

  • Go to the "Settings" tab.

  • In the left-hand menu, select "Webhooks" and click on the "Add Webhook" button.

  • In the "Payload URL" field, enter the Jenkins URL followed by /github-webhook/. This is where Jenkins will listen for webhook events.

  • Under "Which events would you like to trigger this webhook?" select the events that should trigger the webhook. For CI/CD purposes, you might choose "Push" events.

  • Click "Add webhook" to save the configuration.

Step 7: Test the Webhook

  • To test the webhook, make a small code change in your forked GitHub repository (e.g., edit a file, add a new line).

  • Commit and push the change to the repository.

Step 8: Observe Automatic Triggering

  • When you push the code change, GitHub will send a webhook payload to the Jenkins webhook URL.

  • Jenkins will detect this payload and trigger the job you configured.

  • Go to the Jenkins dashboard and observe the job running automatically based on the webhook trigger.

With these steps, you've successfully set up Jenkins with GitHub for CI/CD using GitHub webhooks. Now, whenever you push code changes to your GitHub repository, Jenkins will automatically run your specified build and deployment steps.

πŸ› οΈ Task 2: Seamless Application Deployment using Webhooks and Docker Compose πŸš€

Step 1: Setting Up Jenkins with GitHub Webhook

  1. Follow the steps mentioned in your previous Task 1 to set up Jenkins with GitHub webhook integration. This includes forking the repository, configuring the Jenkins job, and setting up the webhook in your GitHub repository settings.

Step 2: Configure Docker Compose and Deployment Script

  1. In your Jenkins job configuration, locate the "Execute shell" build step.

  2. Modify the execute shell command to include Docker Compose commands for deploying your application. For instance:

     # Stop and remove the existing containers
     docker-compose down
    
     # Pull the latest changes from the repository
     git pull origin master
    
     # Build the Docker images
     docker-compose build
    
     # Start the application using Docker Compose
     docker-compose up -d
    

    Adjust the commands according to your application's needs and your Docker Compose setup.

  3. Save the Jenkins job configuration.

Step 3: Test the Webhook and Deployment

  1. Make a small code change in your GitHub repository (like modifying a file or adding a new line).

  2. Commit the change to your repository.

Step 4: Automated Deployment

  1. With the webhook in place, GitHub will send a payload to Jenkins upon receiving your commit.

  2. Jenkins will trigger the configured job automatically.

  3. The job's execute shell commands will run, which includes stopping any existing containers, pulling the latest code changes, rebuilding images, and restarting the application using Docker Compose.

By following these steps, you've set up a continuous deployment process using webhooks, Jenkins, and Docker Compose. Your application will be automatically deployed whenever you make changes to the GitHub repository and trigger the webhook event. Always make sure to adapt the commands and scripts to match your specific project's requirements.

πŸŽ‰ Final Thoughts

Webhooks act as messengers, letting GitHub talk to external services (such as Jenkins) about what's happening in a repository. These happenings could be anything from pushing code to creating pull requests or updating issues.

When you set up a webhook in your GitHub repository settings and share your Jenkins server's URL, you're essentially creating a bridge that allows GitHub to send important updates to Jenkins whenever interesting events occur. πŸ’Œ

Did you find this article valuable?

Support Adarsh Jha by becoming a sponsor. Any amount is appreciated!

Β