Trigger Jenkins from gogs webhook with param - jenkins

I have a webook url http://jenskinsserver/gogs-webhook/?job=build which will trigger the jenkins job on any event from gogs.
Now i want to trigger a parameterized job and i want the param to be sent via webhook url. How to add params in gogs webhook url?

You can simply add a Build Remote Trigger for this to happen. Go toBuild Triggers => Trigger builds remotely (e.g., from scripts) and set the API Token.
You have to configure the General => This project is parameterized and choose whatever you like from the parameters available.
Now in the url, http://jenskinsserver/gogs-webhook/buildWithParameters?token=<api token provided>&param1=value1&param2=value2 provide params like this and access them by using $param1, $param2 in the scripts.

Related

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.

How to trigger a Jenkins job on a different Jenkins server from a Jenkins pipeline

It's easy enough to call one Jenkins job from another Jenkins job if they're both jobs on the same Jenkins instance:
pipeline {
agent any
stages {
stage('call the say_hello job') {
steps {
build 'say_hello'
}
}
}
}
But how do we do this if the 'say_hello' target Jenkins job is on a different Jenkins server than the calling job? (Not a different agent, I mean a completely different Jenkins instance managed by another group in my company.)
Right now we're doing it with calls to the Jenkins httpRequest plugin to trigger them, and then more httpRequest calls to poll the status of the remote job (so we can propagate their result states), but it feels a little kludgy and I was hoping there was a more robust way.
1) Enable the URL job trigger in destination job
2)Enable permission for “auto” in matix based security add auto and set permission accordingly
3) you’ll need to use an API token. From the user list, click on the “configure” icon (the wrench and screw driver) next to the “auto” user.
4)do curl in stage from original job http://auto:8702d1cb53a83f8748d9433ebca494fb#your-jenkins.com/job/JobName/build?token=iFBDOBhNhaxL4T9ass93HRXun2JF161Z
above url consist of api token , authentication token and user ( e.g. auto )

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

Jenkins notification email

I have a jenkins pipeline job and pipeline in groovy script. It sends out email
notification every time a job starts and finishes. Now i was told to include the reason for build(ex: git comments, who initiated the job etc) as part of email notification. I am not able to decide how to achieve this.
You can call the API url
http://"JENKINS_URL"/job/"JOB_NAME"/"JOB_NUMBER"/api/xml
or
http://"JENKINS_URL"/job/"JOB_NAME"/"JOB_NUMBER"/api/json
for JSON output.
then all you have to do is get the "shortDescription" from there which is the build trigger e.g. Triggered by GitLab Merge Request #21: "branch name" => master

How to send first pipe result to second one in Jenkins pipeline?

we use Jenkins as CI tools.
we want to separate login from other process.
we define a job for login, in this job we validate user and if user is valid we get user id.
at other job we need to have user id to generate result,Our problem is how we can send first job result(here:user id) to second one?
You can do this with the use of two plugins:
EnvInject Plugin
Parameterize Trigger Plugin
EnvInject allows you to inject variables into the Jenkins environment so they are available even after that build step.
Parameterize Trigger plugin allows you to pass information in this build job to another build job you want to start as parameters.
Once you've determined the username (I assume in some sort of batch or bash, you don't note the OS) you'll need to write it to a file on the system using a key=value pair. Then use EnvInject to get the value from the file into the jenkins environment. After that you'll use the parameterize trigger plugin to build the next job with parameters. This will require that you check the This build is parameterized box in the second job and that you define the appropriate parameters (perhaps with a default value that you can use to intentionally fail the build if you don't get a good value).

Resources