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.
Related
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.
I am facing one issue while building my project in Jenkins.
Actually recently we have migrated our code from GITLAB to GITHUB.
Earlier we were using variables by which are directly available in GITLAB.
But now we are accessing the Webhook data by post content parameter in Jenkins.
When we are pushing the code from IDE to GITHUB my pipeline is working fine.
But when I am running my pipeline using build with parameter.
This line of code is creating issue
env.gitLabSourceBranch = env.GIT_BRANCH.tokenize('/')[2]
env.GIT_BRANCH is coming from the post Webhook trigger.
I want a solution that when we trigger pipeline as build with parameter it should take the GITBRANCH value as null and bypass this firstline.
I want a solution that when we trigger pipeline as build with parameter it should take the GITBRANCH value as null and bypass this firstline.
Try this:
env.gitLabSourceBranch = env.GIT_BRANCH ? env.GIT_BRANCH.tokenize('/')[2] : null
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¶meter2=parameter2_value
I currently have a Jenkins pipeline job that runs a groovy Jenkinsfile to run a go script and returns either an empty string or an error string in which case an email is sent. I want to set this return value to a Global Variable such that the next build that starts can check the previous jobs' return value and determine if it needs to send out another email (or suppress if its the same error)
I'm open to any suggestions (EnvInject Plugin is vulnerable hence I haven't considered using it currently)
I am using "Parameterized Trigger Plugin" to trigger child job. I am passing predefined parameter as NUMBER=$VERSION_NUMBER and child job failed saying FILE_NAME."12345" do not exist. I went to location and found property file is there and realized that child job is searching properties file as FILE_NAME."12345"(FILE_NAME.$NUMBER) and actual name of file is FILE_NAME.12345, So root cause seems like that pligin is passing value with "".
Please suggest if there is a way to pass value without "".
Thanks !!
try using ${NUMBER} instead of $NUMBER