Not able to access custom parameters passed to the jenkins build script - jenkins

I want to add some parameters from the web hooks into the mail sent from the Jenkins. I tried solutions provided in StackOverflow and elsewhere. No success yet.
I have the following in place:
Checked This is a parameterized build
The build script looks like http://JENKINS_URL/job/android02/buildWithParameters?token=<My Token>&PARAM=<My Custom Params>
In the mail content, I try to access the custom parameters by ${PARAM}, $PARAM.
In the mail, however, I'm not getting the values I'm setting. If I set default values for PARAM, it is correctly displayed in the mail I receive. I tried http://JENKINS_URL/job/android02/build?token=<My Token>&PARAM=<My Custom Params> URL as well thinking just in case if it works. The mail is sent as Editable email Notification config.
Basically, everything is working except that I'm not able to access the custom parameters I have passed via Trigger builds remotely option.
Edit1:
If I keep a default value, say, test for the parameter, PARAM, in the mail received, I can see test displayed. But I need to get the value I'm passing in the build script.

From the "tool tip" that you get when you click on the question mark next to "":
${ENV,var="VARIABLENAME"}
Expands to an environment variable (specified here as VARIABLENAME) from the build environment. Note that this does not include any variables set by the build scripts themselves, only those set by Jenkins and other plugins.
So, use ${ENV,var="PARAM"}.

Related

Set Jenkins build cause manually via REST API

I'm looking to update the default build cause when invoking a build via the REST API. I see that this is possible by appending the cause parameter when using the Trigger builds remotely (e.g., from scripts) option, but what about when I make a /build POST request using the normal user:apiToken format?
Essentially I'm looking to replace Started by user xxxx with a custom string. I'll also accept plugin recommendations as a fallback if this isn't possible by default.

Send shell variable to slack notification in jenkins job

I have a shell script which does the deployment. There is a variable set in my shell script and I want to pass that variable in slack notification custom message, but its not working as expected.
I have used envInject plugin to set the shell variable as environment variable and its not working as expected
${CRQ} is completed in staging. Please validate
Console url is : ${BUILD_URL}console
staging_daily_dose - #114 Failure after 4.4 sec (Open)
Console url is : https://jenkins-host/job/staging_daily_dose/114/console```
I want ${CRQ} with the actual value. Can someone help me with the best approach
From various posts it is clear that to add an environment variable that can be used by Slack Notification Plugin needs to be inserted using Inject Environment Variable, So
In The Build step create a properties file and write the required variables, and use the file in the Inject Environment Variables Plugin
Next use the variable in the Slack Notification Plugin
This will result in a slack message like below.
You can refer the variables as $VARIABLE along with the messages in the Post-build Actions -> Slack Notifications -> Include Custom Message section as shown in the image below.
Slack notification configuration:

Jenkins - Using build parameters passed during Trigger of a Job

I have a pipeline in Jenkins, where one project sends parameter to the next one. The trigger is set during the Build step and is the last step in the Build process. I have checked the "For each property file, invoke a build" button, where the properties files to be read are in the work space of the current project.
On build, I can see that the Downstream project following the current one, is given the properties of the property file. But when I try accessing them, it just takes the name of the variable and not the value. This means that I am not able to access the parameters in the property file. Can you tell me how will I be able to access the parameters that were passed during trigger from the property file? I have tried all ways, but I can't seem to access them.
Image of Parametrized Trigger--
I solved the issue: in order to use the parameters sent during the trigger, we need to declare them in the triggered project. They should be kept empty. Once we do that, they will take the values sent across by the previous project.

How to use the 'Issue attribute path' in the parameter mapping of jenkins-trigger-plugin

I am willing to trigger my Jenkins job based on status change event from a JIRA issue. I am able to do it using jenkins-trigger-plugin. However I wish to get some more information about the issue like the description of the issue etc. I could see an environment variable named 'JIRA_ISSUE_KEY' but it gives me the key and nothing else.
As of the current release, the issue attribute path is currently resolved from this Issue object. All of jira-trigger-plugin configuration is documented in Jenkins help buttons, which is viewable in Jenkins job configuration.
description for example is retrievable via description path (which would call issue.getDescription() essentially).

Jenkins - Trigger email based on input parameter

I have several Jenkins jobs where I want an email to be triggered (or not trigger) based on an input parameter.
The use case is that I have one deploy job per project, but that job is parametrized for each environment. We you run the job you can select which environment to deploy to (Dev1, Dev2, QA, etc). Ideally I would like to send out notification emails when a new build is sent to QA, but I do not want to send out notification emails when a new build is sent to Dev, because that happens all the time (with every developer commit) and would flood email boxes.
I've tried googling but haven't yet found a solution. I am currently using the email-ext plugin.
Thanks.
It is a very nasty way to solve the problem, but if you cannot do it by any other means...
Create a dependent job that exists primarily to pass / fail based on the value of the parameter, which would get passed as a parameter to the job. Then you could chain the decision the job made to the email notification.
Of course, if you were going to do this, it would probably be better to just write a small parametrized email sender using javax.mail and have Jenkins run it by "building" an ANT project that actually calls the "java" task. Then you have your own email sender (full control) and could make it a dependent job on the other tasks.
I hope someone chimes in with a simpler way...
In email-ext you can add a "Script - Before Build" or a "Script - After Build" trigger which will trigger on whether the specified groovy script returns true or false.
The help for the script reads:
Use this area to implement your own trigger logic in groovy. The last line will be evaluated as a boolean to determine if the trigger should cause an email to be sent or now.
They don't give many details of what's available in the script, but from the source code on Github, it looks like you have "build" and "project" at least, and some common imports done for you:
cc.addCompilationCustomizers(new ImportCustomizer().addStarImports(
"jenkins",
"jenkins.model",
"hudson",
"hudson.model"));
Binding binding = new Binding();
binding.setVariable("build", build);
binding.setVariable("project", build.getParent());
binding.setVariable("rooturl", JenkinsLocationConfiguration.get().getUrl());
binding.setVariable("out", listener.getLogger());
Caveat, I haven't tried this, but this should work as an example script:
build.buildVariables.get("MY_DEPLOYMENT_ENV") == "QA"
that should get the value of a String or Choice parameter you've created called "MY_DEPLOYMENT_ENV" and trigger the email if the current value is "QA"

Resources