Trigger Jenkins pipeline build from Github Tag - jenkins

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>'
)
}

Related

Trigger Tekton pipeline from Bitbucket webhook

In one of our project we have to integrate Bitbucket webhook with Tekton pipeline that means whenever developer commits any changes to Bitbucket repository, Tekton pipeline is triggered and will perform several steps to build.
Earlier we have used GitHub webhook with Tekton and it was working fine.
For Bitbucket webhook, we are getting below error:
failed to replace JSONPath value for param pusher-name: $(body.actor.name): name is not found".
I checked the payload coming from Bitbucket webhook and found there is no such field name.
I used bitbucket-push cluster trigger binding plugin.
Can anyone help us to resolve this issue?
You can use tekton triggers and event listener for that.
With BitBucket, the author name should be somewhere in the actor.username, actor.display_name or actor.nickname
Here's a sample bitbucket push payload
You may have other errors. Switching from one git provider to another, you may have to re-do your interceptors, triggertemplates, ...
Meanwhile, it's unclear what you refer to, by "I used butbucket-push cluster trigger ...". Where did you find this? Checking Tekton Triggers samples, there's no author.name. Sounds like your issue relates to GitHub-specific configurations.

How to pass git branch name dynamically in web hook URL?

I am using a parameterized declarative downstream job in Jenkins.
During webhook where I need to pass the git_repo and git_branch as a parameter.
And I have defined this parameter in the Gitlab repository
Example
https://myjenkins.com/job/myjob-builder-downstream/buildWithParameters?token=1qqq1f54ff88e373b3c0&git_repo=git#mygitlab:development/myproduct.git&git_branch=master
During webhook, I don't know how to pass the branch name dynamically to my downstream job?
Thank you in advance for the help.
The major providers(bitbucket, github and gitlab) does not allow us this level of parametrization in the static webhook url registration step:
Bitbucket
Github
Gitlab
So, what is the solution?
These providers offer us an alternative: Webhook post payload interpretation.
How it works?
When github(example) invoke our webhook url, send a body http with a json with a lot of information about the event:
branch name
repository name
username who performed the push event
git commit message
etc
So in the backend of your webhook url, you must parse this json and get your desired values and start your custom logic. Here some samples of these json bodies:
gitlab json payload
github json payload
bitbucket json payload
Unfortunately, jsons are not the same for github, gitlab and bitbucket
Jenkins plugins
You can void this json parse if you use some of the jenkins plugins. One by provider. In you case gitlab-plugin. If you review the source code, you will view the json parse.
easy-webhook-plugin
If you work with several providers or custom plugins does not help you, you could try my generic plugin.
https://github.com/utec/easy-webhook-plugin
How it works?:
Plugin expose a public url similar to your approach or urls of others plugins:
https://myjenkins.com/project/myjob-builder-downstream/buildWithParameters?token=1qqq1f54ff88e373b3c0&git_repo=git#mygitlab:development/myproduct.git&git_branch=master
but with some differences and I think, more clean and easy:
http://my_jenkins.com/easy-webhook-plugin-RDcd4y3LkDcMKKhG/?scmId=gitlab&jobId=hello_word_job
In which you must indicate the scmId (gitlab or bitbucket) and an id of any jenkins job.
When git push is performed, gitlab will send the json to this url, my plugin will parse it and forward some standard parameters to your job:
repositoryName
branchName
authorId
eventMessage
You could access to these parameters with the classic "params" variable in jenkins and do whatever you want!
node {
echo 'New build detected with these incoming parameters: '+params
}
Follow the official readme and or feel free to contact me with an issue
Jenkins gitlab plugin has predefined variables:
https://plugins.jenkins.io/gitlab-plugin/
see Defined variables section. But in order to utilize them, you need to enable This project is parametrized section without adding any variable, this triggers webhook to automatically populate variables like gitlabSourceBranch or gitlabTargetBranch and many more in future builds..
Not sure if it's a bug, but without enabling this once, variables were not populated.

Trigger Jenkins Job from Bitbucket on Pull Request

Hoping to gather insight from professionals. My end goal is to trigger a jenkins build whenever a bitbucket pull request happens. If anyone could give me an ELI5(explain like I am 5) answer it would be greatly appreciated. Sorry if this is the wrong format, I am new to jenkins and stackoverflow.
What I have done so far:
Created webhook in bitbucket and gave the url to my jenkins job. example: http://jenkinsURL:8080/job/boulevard-dev/generic-webhook-trigger/invoke?token=myPull_Request_Token
Pull request webhook trigger
In Jenkins, under source code management I have: Source Code Management Settings. This is currently fetching a ton of branches, failing, then building the master branch when the job starts?
For build triggers, other stackoverflow articles have pointed me to the "Generic Webhook Trigger". https://github.com/jenkinsci/generic-webhook-trigger-plugin
I am not entirely sure how this generic webhook trigger should effectively be setup? Hoping someone has experience using it and could explain what is needed.
This is what have seen referenced in other articles.Build Triggers settings Build triggers settings 2
Questions:
What does a correct setup / example of the generic webhook trigger look like?
Currently, my job triggers when a change is made to master or merged to master, how can I specify to my job that I want the bitbucket pull request branch to be built?
Also, I found this, not sure if its related to my issue or not? https://jira.atlassian.com/browse/BCLOUD-5814
As per your requirement, you can trigger a Jenkins build whenever a bitbucket pull request happens by following the below steps, in my case, it's working fine.
Step(1) - Configure Jenkins
(i) Add your bitBucket repo and branch to source code management
(ii) On build Triggers setup Poll SCM to * * * * * for run every minute to check pull request from bitBucket.
Step(2) - configure Bit Bucket Hook
(i) Go to settings and add a new hook, now setup pull request trigger as per your requirement.
Step(3) - Make a pull request and see the new job automatically triggered on Jenkins.

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

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

How to trigger a Jenkins job from a status change in Jira

I have been looking for a while now for a way to trigger a Jenkins job from the status or a ticket/story in Jira changing status. To give a more detailed example when my team moves a ticket to the 'ready for test' column we would like to be able to trigger a sanity test pack in Jenkins, the ideal situation would then be that we are able to post the results (generated as a html) as a comment on the ticket within Jira. Failing that we would like to be able to publish the results as simply pass/fail.
I have recently been looking at the Jenkins Jira plugin but this does not seem to have the functionality to work both ways, in other words it can post results after a job has run but you cannot trigger the job from a change of status in Jira. Is there any such plugin available or is it something that we will need to create ourselves?
Cheers in advance
So, basicly there are webhooks in jira (https://developer.atlassian.com/jiradev/jira-architecture/webhooks). With it you can configure it to trigger specific url on issue status change. The specific url should be jenkins API, for example for triggering a build you should call an external url like (if you are building with parameters): http://server/job/myjob/buildWithParameters?token=TOKEN&PARAMETER=Value
Some more info https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API
I have written a Jenkins plugin that supports JIRA status change trigger: jira-trigger-plugin.
This plugin also injects an environment variable of JIRA_ISSUE_KEY, so you can utilise this information to publish your result back to JIRA e.g. using REST API.
Please follow as mentioned below:
GIT:
You can set the web hooks for git/ bitbucket /stash on commit which inturn should change the state of task in JIRA.
JIRA:
Once you in JIRA. define the workflow for your task.
In this particular workflow you can set a post-function where the web hook should be configured. In this configuration of webhook in events define JQL as below:
status CHANGED FROM "To Do" TO "ready to test".
In the same mention the job that needs to be fired in the URL section.
You can look through webhooks in Jira :
https://support.atlassian.com/jira-cloud-administration/docs/manage-webhooks/
You also need to add the Generic Webhook Trigger plugin to your Jenkins :
https://plugins.jenkins.io/generic-webhook-trigger/
You can find here an example on how to use the Generic Webhook Trigger plugin with Bitbucket Github and Gitlab. https://github.com/jenkinsci/generic-webhook-trigger-plugin/tree/master/src/test/resources/org/jenkinsci/plugins/gwt/bdd
It involved multiple steps
If you are using Git/Stash/BitBucket (which i implemented),
You can simply
configure the commit hooks in your Stash/Bitbucket
Attach events to your JIRA workflow
Jenkins JOB - post build events - configure Notify
Stash plugin Set the Poll SCM
That's it

Resources