Automating Jenkins Pipeline Triggers with Webhooks on Google Cloud Platform

Ashutosh Singh
6 min readApr 21, 2023

--

In a world where continuous integration and delivery are the norm, automated testing and deployment have become a necessity. Jenkins, an open-source automation server, is a popular choice for implementing continuous integration and delivery pipelines. However, manually triggering pipelines can be tedious and time-consuming, especially in a large project with frequent code updates. That’s where webhooks come into play!

This blog is for Google Cloud Source Repository for GitHub check this link :- https://ashutoshsingh7.medium.com/automating-ci-cd-with-jenkins-triggering-pipelines-with-webhooks-for-github-repository-49b1d50c73e8

What is a webhook?

A webhook is a URL that triggers a specific action in response to a particular event. In the context of code repositories, webhooks are used to trigger automated builds, tests, and deployments whenever new code is pushed to the repository.

How to Set Up a Webhook in Google Cloud Source Repository(single pipeline)

Google Cloud Source Repository is a Git version control system that provides a private Git repository for your project’s source code. Here’s how to set up a webhook to trigger a Jenkins pipeline whenever code is pushed to your repository.

1. Create a Jenkins Pod on GKE:-

To set up a Jenkins pipeline on GKE, we need to create a Kubernetes pod running the Jenkins container. We can create a Jenkins pod using the following command:

kubectl run jenkins --image=jenkins/jenkins:lts --port=8080

2. Set Up a Jenkins Pipeline:-

Next, we need to set up a Jenkins pipeline that will be triggered by the webhook. Here’s an example Jenkinsfile that prints a message whenever it’s triggered:

pipeline {
agent any
stages {
stage('Print Message') {
steps {
echo 'Hello, World!'
}
}
}
}

3. Create a Webhook in Cloud Source Repository

Now that we have our Jenkins pipeline set up, we can create a webhook in Cloud Source Repository. Follow these steps:

  • Go to your repository’s Settings page.
  • Click on the Webhooks tab.
  • Click on the Add webhook button.
  • Enter the following details:
  1. Payload URL: The URL of the Jenkins pod running on GKE.
  2. Content type: application/json
  3. Secret: Optional. Can be used to validate the webhook sender.
  4. Which events would you like to trigger this webhook?: Push events

4. Test the Webhook

Once you’ve set up the webhook, you can test it by pushing some code to your repository. You should see the message “Hello, World!” printed in your Jenkins pipeline console output.

How to Trigger Multiple Pipelines for Different Folders in the Same Repository

In a project with multiple pipelines, each pipeline can be triggered by a separate webhook. In a Google Cloud Source Repository, each pipeline can be triggered by a webhook associated with a specific folder. Here’s how to set up multiple webhooks for triggering pipelines associated with different folders:

1. Create a Jenkins pipeline for each folder

Create a Jenkins pipeline for each folder you want to trigger a pipeline for. The pipeline should be saved in a Jenkinsfile in the root of the folder.

2. Create a Webhook for Each Pipeline

Create a webhook for each pipeline that you want to trigger. For each webhook, specify the folder path and the URL of the Jenkins pod running on GKE.

Here’s an example of a webhook JSON payload for triggering a pipeline associated with the folder “folder1" in a Google Cloud Source Repository:

{
"ref": "refs/heads/main", // Refers to the branch that was pushed to
"before": "1234567890", // The commit hash before the push
"after": "0987654321", // The commit hash after the push
"repository": {
"full_name": "my-project/my-repo", // The full name of the repository
"name": "my-repo", // The name of the repository
"clone_url": "https://source.developers.google.com/p/my-project/r/my-repo", // The clone URL of the repository
"project_id": "my-project", // The ID of the Google Cloud project
"project_number": "1234567890" // The project number of the Google Cloud project
},
"pusher": {
"name": "Ashutosh Singh", // The name of the user who pushed the code
"email": "ashutosh.singh@gmail.com" // The email of the user who pushed the code
},
"changes": [
{
"ref": "refs/heads/main", // Refers to the branch that was pushed to
"project": "my-project/my-repo", // The full name of the repository
"before": "1234567890", // The commit hash before the push
"after": "0987654321", // The commit hash after the push
"type": "ADDED", // The type of change that occurred
"file_path": "folder1/file.txt" // The path of the file that was changed
}
]
}

Here’s example of webhook JSON payload for triggering a pipeline associated with the multiple different folders in a Google Cloud Source Repository:

{
"ref": "refs/heads/main", // Refers to the branch that was pushed to
"before": "1234567890", // The commit hash before the push
"after": "0987654321", // The commit hash after the push
"repository": {
"full_name": "my-project/my-repo", // The full name of the repository
"name": "my-repo", // The name of the repository
"clone_url": "https://source.developers.google.com/p/my-project/r/my-repo", // The clone URL of the repository
"project_id": "my-project", // The ID of the Google Cloud project
"project_number": "1234567890" // The project number of the Google Cloud project
},
"pusher": {
"name": "Ashutosh Singh ", // The name of the user who pushed the code
"email": "ashutosh.singh@gmail.com" // The email of the user who pushed the code
},
"changes": [
{
"ref": "refs/heads/main", // Refers to the branch that was pushed to
"project": "my-project/my-repo", // The full name of the repository
"before": "1234567890", // The commit hash before the push
"after": "0987654321", // The commit hash after the push
"type": "ADDED", // The type of change that occurred
"file_path": "folder1/file.txt" // The path of the file that was changed
},
{
"ref": "refs/heads/main", // Refers to the branch that was pushed to
"project": "my-project/my-repo", // The full name of the repository
"before": "1234567890", // The commit hash before the push
"after": "0987654321", // The commit hash after the push
"type": "ADDED", // The type of change that occurred
"file_path": "folder2/file.txt" // The path of the file that was changed
},
{
"ref": "refs/heads/main", // Refers to the branch that was pushed to
"project": "my-project/my-repo", // The full name of the repository
"before": "1234567890", // The commit hash before the push
"after": "0987654321", // The commit hash after the push
"type": "ADDED", // The type of change that occurred
"file_path": "folder3/file.txt" // The path of the file that was changed
}
]
}

Here are comments explaining each section and line of code in the JSON payload:

  • ref: Refers to the branch that was pushed to.
  • before: The commit hash before the push.
  • after: The commit hash after the push.
  • repository: Contains information about the repository, such as its full name, name, clone URL, project ID, and project number.
  • pusher: Contains information about the user who pushed the code, such as their name and email.
  • changes: An array that contains information about each change that occurred in the push. Each change contains information about the branch, project, commit hashes, change type, and file path. In this example, we have included three changes, each associated with a different folder in the repository

In the above JSON payload, “my-project/my-repo” refers to the name of your Google Cloud Source Repository, “folder1/file.txt” refers to the file path of the code that was pushed to the repository, and “https://source.developers.google.com/p/my-project/r/my-repo” is the URL to clone the repository.

To create a webhook, go to your repository on Google Cloud Source Repositories, click on the “Settings” tab, and then click on the “Webhooks” tab. Click on the “Add webhook” button and enter the URL of the Jenkins pod running on GKE. You can also specify the folder path associated with the pipeline that you want to trigger.

Once the webhook is set up, every time someone pushes code to the specified folder path, the webhook will trigger the associated pipeline in Jenkins. This allows for automated builds and deployments without having to manually trigger pipelines.

With the use of webhooks and Jenkins, you can easily automate your build and deployment process, saving time and effort while increasing efficiency.

Happy coding!

--

--

Ashutosh Singh
Ashutosh Singh

Written by Ashutosh Singh

I am consummate professional who is committed to staying up-to-date with the latest industry trends and best practices.

No responses yet