Report this

What is the reason for this report?

about githooks with jenkins

Posted on April 25, 2020

how to do i run on slave when changes are made to master and run on slave when changes are made on anyother branch.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

Using Git hooks with Jenkins for CI/CD is a common practice and can be accomplished using a variety of methods. However, Jenkins typically uses webhooks, not Git hooks, to trigger builds. Here’s how you can set it up:

  1. Set up a Jenkins job for each branch:

You’ll want to have a separate Jenkins job for the master branch and for the other branches. You can use Jenkins’ Multibranch Pipeline to automatically create a pipeline for each branch in your repository.

  1. Configure webhooks in your Git repository:

This process might vary slightly depending on where your Git repository is hosted (like GitHub, GitLab, Bitbucket, etc.). Generally, you’ll go to the settings for your repository, find the webhooks section, and add a new webhook. The Payload URL will be your Jenkins server URL followed by /github-webhook/ or /gitlab-webhook/ etc., depending on your Git host.

For GitHub, for example, it would be http://your-jenkins-server/github-webhook/.

  1. Set up triggers in your Jenkins jobs:

In the configuration for each Jenkins job, you will need to set up the job to be triggered by the corresponding webhook.

In a Pipeline job, you’d add this to your Jenkinsfile:

triggers {
  githubPush()
}

Or, in a freestyle job or similar, you’d check the box for GitHub hook trigger (or similar, depending on your Git host) in the job configuration.

  1. Configure your Jenkins slave nodes:

In your Jenkins master node, you will need to go to “Manage Nodes” and set up your slave nodes. This involves giving each one a unique name and setting up how the master will connect to the slave.

  1. Run job on the appropriate slave:

In the Jenkins job configuration, you’ll have an option to restrict where the job can be run. You can use the “Restrict where this project can be run” checkbox and then specify the name of the slave where the job should run.

  1. Differentiate between branches in your Jenkins job:

You can differentiate between branches in your Jenkins job using the env.BRANCH_NAME variable. In a Pipeline job, you could do this in your Jenkinsfile:

if (env.BRANCH_NAME == 'master') {
  // run steps for when changes are made to master
} else {
  // run steps for when changes are made to any other branch
}

In this way, when changes are pushed to the Git repository, the corresponding webhook will trigger the appropriate Jenkins job. The Jenkins job will then run on the specified slave node.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.