Setting up Github post-receive webhook with private Jenkins and private repo

githubJenkins

I'm trying to set up a private GitHub project to send a post-receive request to a private Jenkins instance to trigger a project build on branch push. Using latest Jenkins with the GitHub plugin.

I believe I set up everything correctly on the Jenkins side because when sending a request from a public server with curl like this:

curl http://username:password@ipaddress:port/github-webhook/

results in:

Stacktrace: net.sf.json.JSONException: null object

which is fine because the JSON payload is missing. Sending the wrong username and password in the URI results in:

Exception: Failed to login as username

I interpret this as a correct Jenkins configuration. Both of these requests also result in entries in the Jenkins log. However, when pasting the exact same URI from above into the Github repository Post-Receive URLs Service Hook and clicking on Test Hook, absolutely nothing seems to happen on my server. Nothing in the Jenkins log and the GitHub Hook Log in the Jenkins project says Polling has not run yet.

I have run out of ideas and don't know how to proceed further.

Best Answer

Try using Apache as a proxy in front of Jenkins. I Use NameVirtualHost...

<VirtualHost>
  --Snip---    

<Proxy *>
      AddDefaultCharSet Off
      Order deny,allow
      Allow from all
     --snip-- #You can tighten this to only allow from GITHUB ips.
</Proxy>

    RequestHeader unset Authorization
    RequestHeader set Authorization "Basic [AUTHSTRING]"
    ProxyPass / [AJP|HTTP]://[JENKINS]:[PORT]/
    ProxyPassReverse / [AJP|HTTP]://[JENKINS]:[PORT]/

</VirtualHost>

I run Jenkins in a tomcat container and use AJP, so the var [AJP|HTTP] can be either for the proxy. The [JENKINS] and [PORT] variables should be intuitive.

Now the hard part, [AUTHSTRING]!

Take the USERNAME:PASSWORD part and run it through this command:

$ echo -n 'username:password' | base64
 dXNlcm5hbWU6cGFzc3dvcmQ=

(echo -n is important to remove the newline.) Take the result and put in [AUTHSTRING]

You should be able to remove the user:password from the line at github.

Related Topic