How to set up github webhook trigger on pushing in certain branch - jenkins

I have Jenkins pipeline, and configured github webhook to trigger pipeline.
How to make triggering pipeline when the certain branch was pushed, instead of triggering pipeline by pushing to every branch ?

Webhook is generic for all, there is no filter on the side github or bitbucket, all you need to handle based on payload.
you can use Generic+Webhook+Trigger+Plugin,The plugin will allow you to parse certain data from the payload, and can conditionally trigger a build depending on the branch name.
Apply the filter with branch name
generic-webhook-trigger-plugin-specific-branch

Related

jenkins generic webhook trigger git. Option Filter not working

I am using jenkins generic webhook trigger git. i want to trigger build only when a push is made on branch which starts with PO.Used optional filter as ^(ref\heads\PO-[a-z0-9_-])+$ but was not working, can someone please help. when Optional filter is blank. the build gets triggered on master branch.

Trigger Jenkins pipeline build from Github Tag

I'm trying to trigger a Jenkins Pipeline build (NOT MultiBranch Pipeline) when a specific format of tag is pushed to my GitHub repository. So any branch pushed to the repository will trigger a build if it is tagged with a format of Major.Minor.Patch e.g. 123.123.123
I've setup a webhook which works fine and hits Jenkins (I can see it in the Github Hook Log on Jenkins configuration page). But unfortunately it does not trigger the build.
I've tried setting the refspec to:
+refs/tags/*:refs/remotes/origin/tags/*
And I've accompanied this with a branch identifier:
:origin/tags/[0-9]+\.[0-9]+\.[0-9]+
I have read every article I can find, and scoured StackOverflow but I'm at a loss. I can make it work by setting the branch identifier to **/tags/** but this is too open and triggers a lot of redundant builds.
If anyone can assist in achieving this goal it would be massively appreciated. Also, I'm not certain whether I should be using Pipeline to MultiBranch Pipeline to achieve this?
Starting to lose faith that Jenkins is a good choice so before I jump ship please help!
Thanks!
I prefer using the generic webhook trigger plugin
This allows you to assign a token to a specific Pipeline job so that it will be triggered when your GitHub webhook sends a http request the job's corresponding url:
http://JENKINS_URL/generic-webhook-trigger/invoke?token={token-goes-here}
The Github docs describe the http payload content for a push event
You can parse the http payload using a JSONPath expression to obtain the tag string and then filter whether to run the Jenkins job using the Major.Minor.Patch regex
I've not tested it but here's what the pipeline code might look like:
triggers {
GenericTrigger(
genericVariables: [
[key: 'tagString',
value: '$.ref',
expressionType: 'JSONPath']
],
token: 'example-token',
printContributedVariables: true,
printPostContent: true,
// only trigger if tag follows Major.Minor.Patch regex
regexpFilterText: '$tagString',
regexpFilterExpression: '<tag-regex-here>'
)
}

Trigger building the Github's webhook head_commit in Jenkins

I have configured a Jenkins Pipeline that correctly receives webhooks push notifications from GitHub, on push events.
The authentication is done through a deployment key.
Jenkins configuration seems to rely a lot on the concepts of branches and pull requests.
Given my workflow, which consists mostly of creating branches for any needs, and merge changes back onto master once things would look fine on the CI, I do not really care about seeing my builds categorized by branch or PR.
I would prefer to see builds based on commits' hashes. I do not see the need to tell Jenkins what branch I want it to match.
My current configuration is not selecting the commit revision I desire. I do not understand how it decides which branch or commit to build, once it receives a push notification. In Jenkins I have set the branch to **.
Checking the webhook payload, I noticed that it contains both the list of commits but also something called head_commit, with its id property being the commit's hash I want to see being built.
I would like Jenkins to do the following:
Check what's the head commit in the webhook notification (head_commit.id in the payload)
Build that precise commit, no matter the branch it is part of
Setting a green tick or a red cross beside the commit's hash on Github
Is this possible?

Catch the event of removing a branch from github webhook

I need to make my own script to remove the branch on the removal event from the Github. I use multibranch pipeline and Blue Ocean plugin.
I'm assuming you already have webhooks set up for push events. You will need to set up a similar webhook, but instead of using the push hook, use a delete hook, which is fired any time a branch or tag is deleted.

Is it possible to configure Jenkins to NOT run a pipeline on branch discovery?

I have a multi-branch pipeline that has been configured to use branch auto-discovery. However I don't want Jenkins to automatically start a pipeline job when it discovers a new branch. I instead want the pipeline job to be started by another means (e.g. using a timer or via a REST API call).
Is this possible?
Yes, add the Suppress automatic SCM triggering property in the branch sources of your multibranch project.
You might revert to explicitly naming the branches you want to include
with auto-discovery using Filter by name (with wildcards).
The documentation states: Space-separated list of name patterns to consider. You may use * as a wildcard; for example: master release*
So, adding any new branch to the list of included branches will ensure any changes after such addition will trigger processing as usual.
Any new branch will not be processed until it is explicitly included with the list.

Resources