Is it possible to trigger Jenkins jobs with Github Pull Request Builder without using a webhook?

198 Views Asked by At

I am using Jenkins to build and test the code in a github repository. I want to trigger tests automatically when a PR is made on github with particular comments / labels.

I have found the plugin github pull request builder enables these sort of features. However, every tutorial / article I find relies on sending a webhook from github to jenkins on a new PR.

The issue is I do not want to expose my jenkins to github, and rather would want to use a cron-like schedule to poll for new PRs that meet the criteria to trigger a test, and then use the github api to push results to the PR comments.

Does anyone know of any documentation that might use an approach like this? Or have a configuration for a job using this approach? I have tried setting up a Crontab line and trigger phrase on a job in Github Pull request builder, but it does not seem to be successful in triggering a job.

1

There are 1 best solutions below

0
On

I doubt there is official documentation for this, but it is certainly doable.

You will need 2 jobs. One job to run periodically (about every 3 minutes), and another to run the actual build. The polling job needs to poll all the open PRs and recursively check if their latest commit has already been built. To do this, you need to use the GitHub pull request API. If that is the case, then you can just finish the build. If not send the PR ID as a parameter and trigger the second job which holds the actual building part.

You can choose to make this build non-concurrent if you see any problems with overlapping builds. Then, your builds will run one after the other and build the latest commit in each PR.

Before the build starts, make sure you use the GitHub API to update the state of the commit you are preparing to build. Then the polling job will know that it should not queue that commit again for a build.