I am working on a multibranch pipeline Jenkins setup and build is triggered using webhook in Git.. Here I have selected Git Branch source as - Git.
When I push any change in git, webhook creates a request body with all push event details. How can I parse "git_http_url" value from this( which will have my git repo url). This value I can then use as ${myrepourl} in jenkins console. Basically I want avoid hardcoding the repo url, it should dynamically take using this parameter.
Please guide.
![webhook request body screenshots][2]][2] [![attached my jenkins console branch source][1]][1] [1]: https://i.stack.imgur.com/sdb0l.png [2]: https://i.stack.imgur.com/icPP9.png
This looks like a not-very-good idea to begin with. I will start by explaining why, outlining the alternatives, and in the end suggesting a solution that might still work if you insist on doing this.
When you configure your pipeline, you need to provide it with Jenkinsfile. It can be pasted inside the configuration ("Pipeline script"), or you can provide a path to it so Jenkins can perform a checkout ("Pipeline script from SCM"). When doing the former, you have one Jenkinsfile, so different branches can't alter it (and so missing the point of having a multibranch). When doing the latter, even if you can parametrize the git repo, you still need to provide a path (as it won't arrive in github notification). In addition, I can trigger your build with my repo, but chances are your pipeline won't be able to properly build my repo anyway. So your pipeline can only build your repo, at which point it's a bit unclear why you insist on not providing your specific pipeline with a path to your specific repo that it specifically knows how to build.
Most people who need multibranch pipelines over Github use one of the plugins specifically written for this purpose, e.g. Github Multibranch plugin or Github organization. These plugins do all the job themselves: they sign up for notifications, process them, and start builds. They also update build status in Github for you.
Finally, if you insist on processing Github notifications yourself, you can use Generic Webhook Trigger plugin that will allow you to trigger the job by
POST
-ing to a specified URL with a token. This may look like this:As per the first configuration line, any JSON posted will get flattened and turned into pipeline variables, with a prefix you define (in this case, "DATA_"). E.g. the field
git_http_url
inside the fieldproject
in Github payload will be defined in the pipeline and available to you asDATA_project_git_http_url
. As per second configuration line, it will also be available asGITHUB_URL
.You can test your pipeline with e.g.
In this case, the contributed variable will be
DATA_hello
and it will have the value ofworld
. (TheGITHUB_URL
variable, naturally, won't be defined.)If you want to turn this into real Github webhook processor, you need to make sure that Github notifs arrive to
<jenkins>/generic-webhook-trigger/invoke?token=my_token
. We usenginx
for that, but there are many other options.