how to trigger gitlab webhook on merge only - jenkins

Thanks for any help in advance,
I currently have a Gitlab Webhook successfully triggering a Jenkins job on the 'merge request events' option:
enter image description here
However, this triggers the Jenkins job for each stage of the merge request,
so whenever a request is created, updated, merged and closed - the URL is triggered and the Job is run for each of these stages.
Is it possible to limit the Webhook to **only ** trigger on a merge approval (so when the merge button is pressed only).
enter image description here
the Webhook payload passes in each stage under the "action" tag, but only care about the "merged" action.
As for the creation, update or closure of the request, I dont need the job to run at these stages.
..
I have tried to resolve this at the Jenkins side - I'm using the Generic Webhook Trigger Plugin and i'm passing in the JSON payload for the trigger - so I can manipulate the job per "action" value being sent though.
However, this was insufficient as the Gitlab Webhook will always trigger from the Gitlab side per 'merge event', resulting in multiple calls to the Jenkins job.

I have found the answer,
this comes in the way of the Generic Webhook Trigger Plugin.
As I have pulled the 'action' variable from the Gitlab Webhook payload:
adding Json payload variable
I can now add this variable to the "Optional Filter" within the same Jenkins plugin:
use the variable as a filter with the relevant expression
here I can re-use the merge action variable and look for the passed in value of "merge".
Now my job will only run if this expression if found within the variable i am pulling though.
Also.. I can add multiple variables into the 'Optional Filter' and make them dependant on the expressions being passed in.

Related

Parameterized Jenkins Jobs Conflict with WebHooks

I have a parameterized Jenkins job that has 1 parameter that accepts webooks to kick off builds.
I use the parameter in the branch specifier and it works except for one use case.
The parameter is called a, the branch specifier in the job configuration is defined as refs/${a} the default value for a is set to heads/master
this all works, but as soon as I kick off the job manually (passing in the default parameter value), WebHooks no longer kick off the job after I kick off the job manually.
Any ideas?

Issue with Generic Webhook Trigger plugin in Jenkins

I am trying to use Generic Webhook Trigger plugin in Jenkins to trigger build in case any PR is raised on my GitHub repo. For starters I defined a variable "current_status" mapping it to "action" field within the json payload to be received from GitHub. While the build is getting triggered on raising a PR but the value for current_status is coming as null. The content-type for my GitHub webhook is "application/json"
The GitHub payload generated against the PR event has action field in it :
"action": "opened",
But when I try to print this variable using println "${params.current_status}" in my pipeline, the value is printed as null.
Also when I try to execute a step based on the value of the variable using
when {
expression { return params.current_status == "opened" }
}
the stage is skipped even though the value as per the action in the GitHub payload is "opened"
For debugging the issue when I selected the option to print the contributed variables in the job log I could see value of the current_status value as opened
But when I refer this variable in my pipeline its value comes out to be null somehow.
As a workaround made my pipeline parmeterized, using the same name for the variable as the one defined in the Generic Webhook Trigger Plugin section (current_status) and then referred to it within my Jenkinsfile and it worked.(the value for the variable reflected the value received in the json payload from GitHub).
Finally I found the solution. For anyone who might be facing the same issue you can refer to the variable defined within the Generic Webhook Trigger plugin directly as a Groovy variable. In my case I tried to the use the variable current_status directly without referrring it via params and it worked as expected.
The other approach of defining the variable via parmeterized pipeline might be helpful wherein we would want to run the build manually.

How to pass multiple build parameter value for a Jenkins job using Rest Assured?

I want to remotely build a jenkins job using multiple parameter through restassured API jobs in java.
I tried using the following link
http://localhost:8080/job/jobname/buildWithParameters?token=TOKEN&FEATURE="parameter1_value";FEATURE2=parameter2_value
but this url results in triggering a job with passing both the parameter values within a single parameter.
get("http://localhost:8080/job/jobname/buildWithParameters?token=TOKEN&FEATURE="parameter1_value";FEATURE2=parameter2_value");
This is the response json of jenkins job
The parameter defined in configuration of a Jenkins job should be a "String Parameter" in order to set them using URL.
And the modified URL should be like -
http://localhost:8080/job/jobname/buildWithParameters?parameter=parameter1_value&parameter2=parameter2_value

How to Pass Upstream Job Build Parameters to Downstream Jobs configured in a MultiJob Phase?

I have Upstream Job(MultiJob) which takes a String Parameter called freshORrerun, to take string value as "fresh" or "rerun" string value, which i need to pass on to downstream(standalone build) jobs to check the value is "fresh" or "rerun". Based on which, in child jobs's i will trigger complete tests run (pybot) or rerun (rebot) of failed tests.
here i have attached the screenshots how i have configured. When i print the passed string in child job it is empty.
Overall Job configuration.
Multi Job phase config and child Jobs
I have many no.of robot tests running them takes a lot of time. i need a way to run only failures of previous run, so that it gives me quick picture of how many got fixed. Could Some one please help me with this.
Click the 'Add parameters' button, select 'predefined parameters' and add: freshORrerun=${freshORrerun} to the list.
You can do it using one plugin called parameterized job trigger in which you will get options to pass parent job parameters to child job.
Note:- For this, you have to create parameters in child job also. These parameters will be overwritted.
plugin link

Jenkins: How to put a new job for each scm-change into the build-queue?

I am using Jenkins for Continous-Integration.
I configured a job, which polls the scm for changes. I have one executor. When there is more than one scm-change, but the executor is already working, there is still only one job added to queue, where I want it to queue more than one job.
I already tried my job "parametrized" as a workaround, but as long as polling does not set any parameters¹ (even not the default ones²), this does not help, too.
Is there any way to get for each scm-change a new build in the job-queue?
[1] https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build
[2] I tried to combine this scenario with https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Dynamic+Parameter+Plug-in
You can write a script with the Jenkins Adaptive Plugin to be triggered by SVN and create a new build regardless of what is currently running.
Another option would be to create two jobs, one that monitors SCM and one that runs the build. Every time there is an SCM change you have the first job add an instance of the second to the queue and complete immediately so that it can continue to poll.
Described scenario is possible in Jenkins by using a workaround which requires two steps:
[JobA_trigger] One Job which triggers another job 'externally', via curl or jenkins-cli.jar¹.
[JobA] The actual job which has to be a parametrized one.
In my setup, JobA_trigger polls SCM periodically. If there is a change, JobA is triggered via curl and the current dateTime is submitted². This 'external' triggering is necessary to submit parameters to JobA.
# JobA_trigger "execute shell"
curl ${JENKINS_URL}job/JobA/buildWithParameters?SVN_REVISION=`date +"%Y-%m-%d"`%20`date +"%H:%M:%S"`
# SVN_REVISION, example (decoded): "2012-11-07 12:56:50" ("%20" is url-encoded space)
JobA itself is parametrized and accepts a String-Param "SVN_REVISION". Additionally I had to change the SVN-URL to
# Outer brackets for usage of SVN revision dates³ - must be avoided if working on a revision-number.
https://svn.someaddress.com/trunk#{${SVN_REVISION}}
Using this workaround, for each scm-change there is new run of JobA queued which has the related svn-revision/dateTime attached as a parameter and is used as the software-state which is being tested by this job.
¹ https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+CLI
² I decided to have dateTime-bases updates instead of revision-based ones, as I have svn-externals which would be updated to HEAD each, if I would be working revision-based.
³ http://svnbook.red-bean.com/en/1.7/svn.tour.revs.specifiers.html#svn.tour.revs.dates

Resources