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

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.

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.

Jenkins Pipeline parameter: list of available git repositories

I'd like to create a pipeline, where one of the parameters is a git repository from a list of available repositories. The list would be similar to the one you see when creating a multibranch pipeline with a bitbucket repository.
Is there a plugin that can do this? Is it hard to make one for myself?
I shoild think the relatively new REST List Parameter would work in these circumstances, as long as the repository host supports the plugin authentication mechanisms. It's kind of an HTTP Request extension for Build with Parameters plugin.
Plugin author (not me) is very responsive to positive feeback and useful enhancement requests.

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

How to pass JOB parameters using Jenkins HTTP POST plugin

I want to pass the Build Name and the BUILD ID to the REST API using Jenkins HTTP POST plugin
how to pass the parameters to it?
I am passing:
http://localhost:55223/api/Demo?BuildName=${JOB_NAME}&BuildID=${BUILD_ID}
I am receiving an error
It looks like the Plugin does not expand environment variables, as evidenced by it's source code. As the plugin has not been updated in 2 years, I don't think there's any chance of the developer adding this anytime soon. If you are still wanting to use the plugin, you could make the changes changes necessary to expand the environment variables and then build it from source. For that, I would recommend looking at the Jenkins classes hudson.EnvVars and hudson.model.Run. More specifically, the Run method getEnvironment(TaskListener listener) and the EnvVars method expand(String s).

How to send additional data elements in Jenkins Notification Plugin?

I use Jenkins Pipeline Jobs and invoke build using its remote API's.
I also use Build Notification plugin to invoke my API once the build is complete for further downstream automation. As per the plugin documentation, it provides a fixed set of data elements from the build. However as part of the build, the Job has generated some data elements which I need to provide it back to my API which gets invoked by Notification plugin (As part of the JSON Payload). Can someone help me how do pass additional data elements through this plugin? or any better ways of doing it?
For example,
1. When the pipeline job is configured with notification endpoint, the jenkins config XML has the following entry
<com.tikal.hudson.plugins.notification.HudsonNotificationProperty plugin="notification#1.11">
<endpoints>
<com.tikal.hudson.plugins.notification.Endpoint>
<protocol>HTTP</protocol>
<format>JSON</format>
<url>http://localhost/api/postStatus</url>
<event>finalized</event>
<timeout>30000</timeout>
<loglines>20</loglines>
</com.tikal.hudson.plugins.notification.Endpoint>
</endpoints>
</com.tikal.hudson.plugins.notification.HudsonNotificationProperty>
A pipeline script just builds an image and the image ID has to be sent in the notification
I did not find a perfect solution in the existing Jenkins Notification Plugin. However the solution that I used it to pass the data as part of the Log Text and parse the information in the other side.

Resources