问题
I have a Jenkins pipeline project and I would like to trigger a build every time a push to the Gitlab repository where the code is hosted is performed.
On Jenkins I have installed the Gitlab plugin and the Git plugin and I have configured the Gitlab section (Manage Jenkins -> Configure System) by adding the Gitlab host URL (https://gitlab.com) and the Gitlab API token previously generated on Gitlab: if I perform a test connection, everything is OK, as expected.
The problem is on Gitlab: I go to Settings -> Integrations and I try to configure a webhook by adding the Jenkins project URL that I have found in the project configuration (highlighted below):
But there is a problem: it seems that requests to localhost from Gitlab are not allowed.
It seems that to avoid this problem is necessary to activate an option (Allow requests to the local network from hooks and services), but this option is available only if I have a Gitlab server managed by myself. Instead, my project is hosted on gitlab.com and so this option cannot be modified.
There is any workaround or alternative solution to configure a webhook in my case?
回答1:
If jenkins is running
- behind the company firewall,
- or on your laptop,
- or on any machine that is not available via a permanent URL from the internet (more exactly, from the gitlab instance you want to wire)
- which is very likely the case, then you need to use the opposite approach: jenkins periodically polling git repository. You can then choose how frequently will the polling happen (in cron
-like manner).
This is also the standard approach offered by jenkins, i.e. this does not require gitlab plugin, just correct credentials.
The downside is, it regularly connects the public gitlab instance which may consume some data; but that is probably insignificant.
回答2:
If you need to login to your jenkins, you can try this webhook url:
http://(jenkins-user):(jenkins-user-token)@(jenkins-url)/job/(job-name)/build?delay=0sec
回答3:
For local instances :
In Gitlab > Admin Area > Network > Outbound requests :
Check "Allow requests to the local network from hooks and services"
回答4:
change localhost to your machine ip address
example: 192.168.33.21 (windows machine ip address)
来源:https://stackoverflow.com/questions/50620226/webhook-configuration-with-gitlab