Reputation: 492
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.
Upvotes: 2
Views: 3060
Reputation: 2688
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.
Upvotes: 0
Reputation: 16524
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
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:
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:
Unfortunately, jsons are not the same for github, gitlab and bitbucket
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.
If you work with several providers or custom plugins does not help you, you could try my generic 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:
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
Upvotes: 2