Using Git Hooks or Webhooks to Trigger Jenkins Builds

Automating the software development process is crucial for modern software development teams. Continuous Integration/Continuous Deployment (CI/CD) pipelines have become a popular way to streamline the process and ensure efficient delivery of new features and bug fixes. Jenkins, an open-source automation server, is often used as a central component in CI/CD workflows. One important aspect of CI/CD pipelines is the ability to trigger Jenkins builds automatically whenever there are changes in a code repository. This can be achieved using Git hooks or webhooks.

Git Hooks

Git hooks are scripts that Git executes automatically before or after certain events, such as committing changes, pushing to a remote repository, or merging branches. By leveraging Git hooks, developers can trigger Jenkins builds whenever there is a relevant event in their local Git repository.

To use Git hooks to trigger Jenkins builds, follow these steps:

  1. Locate the .git/hooks directory in your local Git repository. This directory contains the scripts that Git uses for hooks.

  2. Create a new file inside the .git/hooks directory named post-commit. This file will be executed after a commit is made.

  3. Add the following code to the post-commit file:

#!/bin/sh
curl -X POST "http://jenkins-server/job/job-name/build" --user "username:api-token"

Replace jenkins-server with the URL or IP address of your Jenkins server. Modify job-name with the name of your Jenkins job, and username:api-token with valid Jenkins credentials that have permission to trigger builds.

  1. Make the post-commit file executable by running the following command in your terminal:
chmod +x .git/hooks/post-commit

Now, whenever you make a commit to your local Git repository, the post-commit script will be triggered, and it will send a POST request to the Jenkins server to trigger a build.

Webhooks

Webhooks are HTTP callbacks that allow external services to be notified when specific events occur in a code repository. To trigger Jenkins builds using webhooks, you need to configure your version control system (e.g., GitHub, Bitbucket) to send HTTP requests to the Jenkins server whenever a relevant event occurs, such as pushing changes or merging pull requests.

To set up webhook-triggered builds, follow these steps:

  1. Access your version control system's repository settings (e.g. GitHub repository settings).

  2. Locate and configure the webhook settings for your repository. Typically, you'll find an option to add a new webhook URL.

  3. Set the webhook URL to the Jenkins server's URL appended with the path /github-webhook/. For example: http://jenkins-server/github-webhook/

  4. Save the webhook settings. Your version control system will now send HTTP requests to the Jenkins server whenever relevant events occur.

Next, you need to configure a Jenkins job to respond to these webhook events:

  1. Open your Jenkins dashboard and navigate to the configuration page of the relevant job.

  2. Scroll down to the "Build Triggers" section and check the option "GitHub hook trigger for GITScm polling."

  3. Save the job configuration. Jenkins is now set to trigger builds whenever it receives webhook events from your version control system.

Now, whenever a relevant event occurs in your code repository, your version control system will send an HTTP request to the specified Jenkins server URL, and the Jenkins job associated with the webhook will be triggered, initiating the build process.

Using Git hooks or webhooks to trigger Jenkins builds enables developers to automate the continuous integration process seamlessly. By reducing manual intervention, teams can achieve faster feedback loops and accelerate the delivery of high-quality software products.


noob to master © copyleft