Use Gitlab Webhook to trigger Jenkins automatic operation

Source: Internet
Author: User
Tags webhook

In this paper, we can trigger Jenkins to perform the corresponding actions for how to set up Gitlab and Jenkins to implement the commit event on each gitlab, mainly divided into the following steps:

1. New Gitlab test Case

Enter the personal Gitlab account, in the upper right corner of the plus sign, select Gitlab New Project, you can create a new personal Gitlab project:

All the rest go to default settings, fill in the name of project, you can create a new project,

2. Set Jenkins

2.1 First install GitLab plugin

In "System Management", "plug-in management", see the installed plug-in, enter GitLab to see if it is installed, if not, then check the optional plug-ins, search GitLab, after installation restart.

2.2 Create a new test project

Create a new job,

Source Management Choose Git, enter the URL of the newly created Gitlab and the personal Api_token:

Currently only the Master branch, the subsequent can be based on different branches corresponding to set different URLs, listening to different branches of the situation.
In the Build trigger option, tick build when a changes is pushed to GitLab, the last URL of this option is the URL path of the project, note that if it is native, localhost will be displayed, and localhost can be changed to a personal IP.
Note that this URL will be used for the next step. You can set the build action in the project to execute a Python script: print ("Gitlab webhook Jenkins"). Click Apply and save.

3. Setting the Gitlab Webhook

Gitlab version in the constant transformation, the version of the Gitlab, you can find the Webhook settings entry:

The URL filled in is just the test, tick the push Event, so that every time there is a push operation, it will trigger the Webhook, execute the fill in the URL of the operation:

Click Save, and then click Test to test to see if it can be executed and will generally report these errors:

3.1 Anonymous Build

Anonymous build is not supported, this is back to Jenkins, in system management--global security management, check that anonymous users have readable permissions

Then click Apply and save, go back to Gitlab, and continue testing. If you continue to hold this error, enter the project you just built, click on the build trigger to select the build when the "a change" is pushed advanced option in the lower right corner, there is a secret token, click Generate, will generate a security code:

Copy to the following URL in Webhook:

Then save, then test, and then pass, and this will trigger Jenkins to perform one operation:

Look at the console output:

3.2 Valid crumb

If the error is reported, in the security settings, do not tick CSRF,

Test pass, the subsequent can expand on this basis, listen to different Gitlab, the same Gitlab different branches of the push operation, to trigger the Jenkins side to perform the corresponding operation.

4. The principle of GitLab Webhook

Hook is to set the hook in the corresponding event, when the corresponding event triggered, such as the push event, the merge event and other operations, will trigger the hook below the script execution, and Gitlab Webhook, will trigger the execution of this Webhook URL , similar to the open URL mentioned earlier in the Jenkins call, triggers the Jenkins execution job.

Use Gitlab Webhook to trigger Jenkins automatic operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.