Search code examples
githubjenkinsjenkins-pipelinejenkins-pluginswebhooks

Automatic build on Jenkins initiated from GitHub. Not working. 502 returned


I am new to Jenkins but have successfully created a Pipeline that will build our project hosted on GitHub. The user can even specify the branch (master, develop, test, etc.) that they want to build. Now, I would like to implement automatic builds whenever there is a push to a certain branch (develop for example). Pushes to other branches should not result in an automatic build. I am having trouble getting this going and even finding information on it. I have found:

https://www.devopsschool.com/blog/how-to-build-when-a-change-is-pushed-to-github-in-jenkins/

My first question is: Are there any other sources of information for setting up automatic builds in Jenkins when the backing source repository is GitHub (as opposed to Bitbucket for example)?. The above mention article is from 2021. Some of the steps and screenshots do not correspond to recent versions of Jenkins.

Specifically with the article mentioned, I have completed all the steps. Probably not successfully or things would be working! Of note in Step 2 I have added GitHub Server. I filled in API URL as https://api.github.com. Hitting 'Test Connection' results in:

Credentials verified for user davidrunner, rate limit: 4998

which seems like a good sign. I did not click 'Manage hooks' checkbox as document did not seem to check it. I believe I followed step 3 and configured step 4 on the GitHub repository.

However, when I commit and push a test change to either the branch 'master' or 'develop' I see under 'Recent Deliveries' for Webhooks a Red triangle with exclamation mark. The response is empty with '502'. I believe this means the connection was rejected by the Jenkins machine.

My next questions are: How can I troubleshoot this? What should I check? I stress again that I can certainly pull the code from Jenkins and build. So, clearly communications between GitHub and Jenkins machine is possible.

Update

I tried the suggestion of Iterokun @Iterokun in the comments below.

When I directly use: 1xx.xx.xxx.xx:8080/github-webhook I see the following errors in Jenkins logs:

May 22, 2024 3:39:06 PM INFO org.jenkinsci.plugins.github_branch_source.ApiRateLimitChecker$RateLimitCheckerAdapter checkRateLimit
LocalChecker for rate limit was not set for this thread. Configured using system settings with API URL 'https://api.github.com'.
May 22, 2024 3:41:21 PM SEVERE org.jenkinsci.plugins.github.webhook.GHEventPayload$PayloadHandler parse
**Unknown content type null**
May 22, 2024 3:41:21 PM WARNING hudson.init.impl.InstallUncaughtExceptionHandler handleException
Caught unhandled exception with ID 288b80b3-2bbf-49ee-a010-c0d2bab5e8d4
**java.lang.Exception: Method POST required**
    at org.kohsuke.stapler.HttpResponses.error(HttpResponses.java:92)

I also tried a curl Post command and got the following:

PS C:\andrew\Firmware> curl.exe -X POST http://1xx.xx.xxx.xx:8080/github-webhook/
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>**Error 400 Hook should contain event type**</title>
</head>
<body><h2>**HTTP ERROR 400 Hook should contain event type**</h2>
<table>
<tr><th>URI:</th><td>/github-webhook/</td></tr>
<tr><th>STATUS:</th><td>400</td></tr>
<tr><th>MESSAGE:</th><td>Hook should contain event type</td></tr>
<tr><th>SERVLET:</th><td>Stapler</td></tr>
</table>
<hr/><a href="https://eclipse.org/jetty">Powered by Jetty:// 10.0.20</a><hr/>

On the Jenkins log, I see:

May 22, 2024 7:26:07 PM SEVERE org.jenkinsci.plugins.github.webhook.GHEventPayload$PayloadHandler parse Unknown content type null


Solution

  • The first thing to try is firing a post request from your laptop to the url you configured in the webhook.

    curl -X POST url
    

    You will not get 200, since you are not supplying the expected payload, but it shouldn't be 502 either.

    Once you confirm that Jenkins itself is not the source of 502 you can check the network connectivity from outside your network, like from a laptop tethered to your cell phone.

    If you discover that it's your network configuration that is blocking the calls you can try opening traffic for the IPs that the webhook calls come from. Those are documented in the meta API endpoint under "hooks".