Difference between revisions of "GitHub/Jenkins: Setting up Webhooks"

From Gcube Wiki
Jump to: navigation, search
(Webhook on the GitHub repository)
(GitHub Plugin on Jenkins (only for Jenkins admins))
 
(12 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
= What is a webhook? =
 
= What is a webhook? =
A webhook is a mechanism to automatically trigger the build of a [[Build_and_Integration_System:_Jenkins|Jenkins project]] upon a commit pushed in a Git [[Git_Repositories|repository]].
+
A webhook is a mechanism to automatically trigger the build of a [[Build_and_Integration_System:_Jenkins|Jenkins project]] upon a commit pushed in a Git repository.
  
In order for builds to be triggered automatically by PUSH and PULL REQUEST events, a Jenkins Web Hook needs to be added to each GitHub repository or organization that interacts with your build server. You will need admin permissions on that repository.
+
In order for builds to be triggered automatically by PUSH and PULL REQUEST events, a Jenkins Web Hook needs to be added to each GitHub repository. You need admin permissions on that repository.
  
This guide details the steps to have Jenkins automatically create a build if it detects changes to a GitHub repository. This can be a very useful improvement to continuous integration setup with Jenkins because this method is only telling Jenkins to attempt a new build when a change is detected rather than polling on an interval, which can be a very inefficient.
+
This guide details the steps to have Jenkins automatically create a build if it detects changes to a GitHub repository. This can be a very useful improvement to continuous integration setup with Jenkins because this method is only telling Jenkins to attempt a new build when a change is detected rather than polling on an interval, which can be a very inefficient.
  
 
For more information, see https://developer.github.com/webhooks/.
 
For more information, see https://developer.github.com/webhooks/.
Line 11: Line 11:
  
 
The GitHub plugin (https://plugins.jenkins.io/github) must be installed on Jenkins. This will give us the ability to configure Jenkins to use our GitHub repository.
 
The GitHub plugin (https://plugins.jenkins.io/github) must be installed on Jenkins. This will give us the ability to configure Jenkins to use our GitHub repository.
 
  
 
Go in the page: Manage Jenkins -> Configure System -> GitHub:  
 
Go in the page: Manage Jenkins -> Configure System -> GitHub:  
Line 29: Line 28:
 
The next step is to tell Jenkins to build when the Github hook is called. In the "Build Triggers" section:
 
The next step is to tell Jenkins to build when the Github hook is called. In the "Build Triggers" section:
 
* check "GitHub hook trigger for GITScm polling"
 
* check "GitHub hook trigger for GITScm polling"
* check "Poll SCM" option with no schedule defined. This setup basically tells Jenkins to poll your Gitea repo only when requested via the webhook.
+
* check "Poll SCM" option with no schedule defined. This setup basically tells Jenkins to poll your GitHub repo only when requested via the webhook.
  
 
[[File:JenkinsGitHubBuildTriggers.png|1000px]]
 
[[File:JenkinsGitHubBuildTriggers.png|1000px]]
Line 49: Line 48:
 
** Make sure to include the last /!
 
** Make sure to include the last /!
 
* Select “application/json” as the encoding type
 
* Select “application/json” as the encoding type
 +
* Leave “Secret” blank (unless a secret has been created and configured in the Jenkins “Configure System -> GitHub plugin” section)
  
 
[[File:GitHubWebhookConfig2.png|1000px]]
 
[[File:GitHubWebhookConfig2.png|1000px]]
  
* Leave “Secret” blank (unless a secret has been created and configured in the Jenkins “Configure System -> GitHub plugin” section)
 
 
* Select “Let me select individual events”
 
* Select “Let me select individual events”
 
** Enable ''Pushes'' event
 
** Enable ''Pushes'' event
 
** Enable ''Pull Requests'' event
 
** Enable ''Pull Requests'' event
  
[[File:GitHubWebhookConfig3.png|1000px]]
+
[[File:GitHubWebhookConfig3.png|600px]]
  
 
* Make sure “Active” is checked
 
* Make sure “Active” is checked
* Click “Add Webhook”
+
* Click “Add Webhook” or “Update Webhook”
  
 +
[[File:GitHubWebhookConfig4.png|600px]]
  
 
Jenkins will now receive push and pull request notifications for that repository, and related builds will be automatically triggered.  
 
Jenkins will now receive push and pull request notifications for that repository, and related builds will be automatically triggered.  
Line 67: Line 67:
 
See https://help.github.com/en/github/extending-github/about-webhooks.
 
See https://help.github.com/en/github/extending-github/about-webhooks.
  
= Update Jenkins job with GitHub configuration =
+
= Testing the GitHub/Jenkins round trip =
 +
1) We push a new commit with a message "Update README.md" in the GitHib repository:
 +
 
 +
[[File:GitHubCommitBuild.png|1000px]]
 +
 
 +
2) If the webhook has been properly set up, on the Jenkins interface we should see that the build of the linked project has started:
 +
 
 +
[[File:JenkinsGitHubBuild2.png|400px]]
  
In the ‘General’ section of the job configuration check the Github project tick box and enter the URL to the repository that you configured in step 2.
+
3) Clicking on the ongoing build, we can appreciate that it has been triggered by the commit:
  
Next update the Source Code Management section, first set the repository URL (note the format git@github.com:{YOUR_REPO}. You can also specify the branch you would like to use.
+
[[File:JenkinsGitHubBuild3.png|400px]]
  
The last step is to tell Jenkins to build when the Github hook is called, select the highlighted option below in the Build Triggers section.
+
4) Projects configured [[Build_and_Integration_System:_Jenkins|downstream]] will be also built if the build completes successfully.
  
That’s it! Your Jenkins build should now be triggered whenever a push is made to your repository.
 
  
 
''Back to the [[Continuous_Integration_procedure_(2019) | CI guide]].''
 
''Back to the [[Continuous_Integration_procedure_(2019) | CI guide]].''
  
 
[[Category:Continuous_Integration]]
 
[[Category:Continuous_Integration]]

Latest revision as of 05:17, 1 December 2019

What is a webhook?

A webhook is a mechanism to automatically trigger the build of a Jenkins project upon a commit pushed in a Git repository.

In order for builds to be triggered automatically by PUSH and PULL REQUEST events, a Jenkins Web Hook needs to be added to each GitHub repository. You need admin permissions on that repository.

This guide details the steps to have Jenkins automatically create a build if it detects changes to a GitHub repository. This can be a very useful improvement to continuous integration setup with Jenkins because this method is only telling Jenkins to attempt a new build when a change is detected rather than polling on an interval, which can be a very inefficient.

For more information, see https://developer.github.com/webhooks/.

GitHub Plugin on Jenkins (only for Jenkins admins)

The GitHub plugin (https://plugins.jenkins.io/github) must be installed on Jenkins. This will give us the ability to configure Jenkins to use our GitHub repository.

Go in the page: Manage Jenkins -> Configure System -> GitHub:


JenkinsGitHubPluginConfig.png

Build project configuration

In Jenkins, under the project settings page "Source Code Management":

  • set option to "Git",
  • provide URL to your repo (e.g. https://github.com/d4science/gx-rest.git),
  • set the credentials to git.gcube (only if the repository is NOT public),
  • specify */master as branch to build,

JenkinsGitHubSourceCodeManagement.png

The next step is to tell Jenkins to build when the Github hook is called. In the "Build Triggers" section:

  • check "GitHub hook trigger for GITScm polling"
  • check "Poll SCM" option with no schedule defined. This setup basically tells Jenkins to poll your GitHub repo only when requested via the webhook.

JenkinsGitHubBuildTriggers.png

Webhook on the GitHub repository

For each GitHub repository or organization that you need to configure, perform the following steps:

  • Navigate to the “Settings” tab.
  • Select the “Webhooks” option on the left menu
  • Click “Add Webhook”

GitHubWebhookConfig.png

In the Webhook form:

  • For “Payload URL”:
  • Select “application/json” as the encoding type
  • Leave “Secret” blank (unless a secret has been created and configured in the Jenkins “Configure System -> GitHub plugin” section)

GitHubWebhookConfig2.png

  • Select “Let me select individual events”
    • Enable Pushes event
    • Enable Pull Requests event

GitHubWebhookConfig3.png

  • Make sure “Active” is checked
  • Click “Add Webhook” or “Update Webhook”

GitHubWebhookConfig4.png

Jenkins will now receive push and pull request notifications for that repository, and related builds will be automatically triggered.

See https://help.github.com/en/github/extending-github/about-webhooks.

Testing the GitHub/Jenkins round trip

1) We push a new commit with a message "Update README.md" in the GitHib repository:

GitHubCommitBuild.png

2) If the webhook has been properly set up, on the Jenkins interface we should see that the build of the linked project has started:

JenkinsGitHubBuild2.png

3) Clicking on the ongoing build, we can appreciate that it has been triggered by the commit:

JenkinsGitHubBuild3.png

4) Projects configured downstream will be also built if the build completes successfully.


Back to the CI guide.