unexpected token: JELLY_SCRIPT in Jekins pipeline - jenkins

When I'm trying to run this in Jenkins automation project,
I got this error,
WorkflowScript: 552: unexpected token: JELLY_SCRIPT # line 552, column 27.
${JELLY_SCRIPT,template="testResults.jelly"}
I have added,to the email body ${JELLY_SCRIPT,template="testResults.jelly"} inside success stage.
Why is this problem occuring?
post {
always {
junit '**/results/*.xml'
}
success {
emailext (
subject: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'", to: '$EMAIL_LIST', attachmentsPattern: '**/results/*',attachLog: true,
body: """<p>UNSTABLE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'</p>
<p>Check console output at ;<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a></p> <br> Unit test: ${env.UNIT_TEST} <br> Integration test: ${env.INTEGRATION_TEST} <br> Upgrader test: ${env.UPGRADER_TEST} <br>
<h1>Unit Test Report</h1>
${env.UNIT_TEST_REPORT}
<br> ${env.UPGRADER_REPORT}
<br> <h3>Find Upgrader Logs in following location : </h3><br>
${env.FTP_PATH}
<br> ${env.FTP_LOCATION} <br> ${env.EMAIL_CONTENT}""",
recipientProviders: [[$class: 'UpstreamComitterRecipientProvider']]
)
cleanWs()
}
unstable {
emailext (
subject: "UNSTABLE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'", to: '$EMAIL_LIST', attachmentsPattern: '**/results/*',attachLog: true,
body: """<p>UNSTABLE: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'</p>
<p>Check console output at ;<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a></p> <br> Unit test: ${env.UNIT_TEST} <br> Integration test: ${env.INTEGRATION_TEST} <br> Upgrader test: ${env.UPGRADER_TEST} <br>
<h1>Unit Test Report</h1>
${env.UNIT_TEST_REPORT}
<br> ${env.UPGRADER_REPORT}
<br> <h3>Find Upgrader Logs in following location : </h3><br>
${env.FTP_PATH}
<br> ${env.FTP_LOCATION} <br> ${env.EMAIL_CONTENT}""",
recipientProviders: [[$class: 'UpstreamComitterRecipientProvider']]
)
cleanWs()
}
}
above is the full code of email template. when add above ${JELLY_SCRIPT,template="testResults.jelly"} inside email body, this error occured.

Related

Jenkins Emailext plugin default template: Passing in user defined variable into template

I am trying to edit the given groovy template file given for the emailext plugin to display Jenkins environment variables. The code I am experimenting is:
pipeline {
agent {
label 'main'
}
environment {
test = "This is a test line."
}
stages {
stage('Debug') {
steps {
sh 'printenv'
sh "echo ${env.test}"
bat 'echo %test%'
}
}
}
post {
always {
echo "Emailing.."
emailext body: '''${SCRIPT, template="groovy-html-edited.template"}''',
subject: currentBuild.currentResult + " : " + env.JOB_NAME,
to: 'example#email.com'
}
}
}
What I want is to be able to display the value of the pipeline environment variable within the email sent to the email address. Within the groovy template, I have tried using the following in an edited groovy template file:
${env.test}
${ENV.test}
${ENV,var="test"}
${env,var="test"}
Within the groovy-html-edited.template, the portion I've added for testing is:
<!-- TEST SECTION-->
<table class="section">
<tr class="tr-title">
<td class="td-title-main" colspan=2>
TEST SECTION
</td>
</tr>
<tr>
<td>${ENV,var='test'}</td>
</tr>
</table>
<br/>
What is the correct variable name to get a pipeline environment variable within the groovy template?
Here is an example of getting env vars from a pipeline within script template:
<%
import hudson.Util
import hudson.Functions
import hudson.model.Result;
import hudson.model.Build
//get data from pipeline
def envOverrides = it.getAction("org.jenkinsci.plugins.workflow.cps.EnvActionImpl").getOverriddenEnvironment()
%>
<!-- TEST SECTION-->
<table class="section">
<tr class="tr-title">
<td class="td-title-main" colspan=2>
TEST SECTION
</td>
</tr>
<tr>
<td>${envOverrides["test"]}</td>
</tr>
</table>
<br/>

Jenkins pipeline with userinput extendedChoice returns the values with [ ] at the beginning and end

I added the below to the pipeline so while the pipeline is running - at some stage I want the user to choose from the parameters but the output returns with parentheses at beginning and end.
def envs = input(id: 'Upgarde', message: 'On which customer do you want to apply the upgrade?', submitter: 'admin', ok: 'Submit', parameters: [extendedChoice(defaultValue: env.ENV.split().toString(), description: '', descriptionPropertyValue: env.ENV.split().toString(), multiSelectDelimiter: '', name: 'Customers to upgrade', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_MULTI_SELECT', value: env.ENV.split().toString())]).split(',')
Screenshot from the Jenkins UI:
enter image description here
Fixed by added .replace("[", "").replace("]", "")

Is it possible to have a Jenkins parameter's name with spaces in it?

I'm trying to make my Jenkins UI more clean.
My Jenkins file calls a function which in turn runs the following:
properties ([
[$class: 'GitLabConnectionProperty', gitLabConnection: 'GitlabConnection'],
[$class: 'ParametersDefinitionProperty', parameterDefinitions: [
[$class: 'BooleanParameterDefinition', defaultValue: false, description: '', name: 'activateInTest'],
[$class: 'ChoiceParameterDefinition', choices: 'false\ntrue\n', description: 'If running newBuild, skip unit tests', name: 'skipUnitTests']
]]
])
Currently, I can access these parameters like this:
if(activateInTest == 'true') {
//Do something
}
After going through other docs and examples. It looked as if I could also access parameters by doing something like params.activateInTest, which did not work. I also tried doing something like params["activateInTest"], but that didn't work either.
The reason I want to access it this way params["..."], is because I would like to have the name of my parameter be "Activate in Test" rather than "activateInTest".
In this example I see the person does use "BooleanParameterDefinition" with spaces in the name. But I can't seem to figure out how to use spaces in the name. Having spaces in the name is my only goal here.
yes, its possible, just use following notation:
${params['Name with space']}
tested on old Jenkins: 2.149
Indeed it is possible, user "string reference" to access it, i.e. params."Activate in Test"
For example:
properties([parameters([
string(name: 'Activate in Test', defaultValue: 'default value')
])])
echo params."Activate in Test"
In Java and Groovy space in a variable does not support! and it's not recommended but Jenkins supports it with 'String referencing'
But If you want to decorate the parameter Display Name it would be something like this
Jenkins Declarative Pipeline
pipeline {
agent any
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
text(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person')
booleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value')
choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')
password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password')
}
stages {
stage('Example') {
steps {
echo "Hello ${params.PERSON}"
echo "Biography: ${params.BIOGRAPHY}"
echo "Toggle: ${params.TOGGLE}"
echo "Choice: ${params.CHOICE}"
echo "Password: ${params.PASSWORD}"
}
}
}
}
Scripted Pipeline
node {
properties(
[
parameters(
[string(defaultValue: '/data', name: 'Directory', description: "Directort Path"),
string(defaultValue: 'Dev', name: 'DEPLOY_ENV', description: "Deploy Environment")
]
)
]
)
stage('debug') {
echo "${params}"
}
}

ext-email plugin, Not sending emails to default recipients from pipeline

I am trying to send emails from a Jenkins scripted pipeline with help of the ext-email plugin.
I have configured the plugin with Default Recipients.
Here is my pipeline:
node {
try {
echo "hi"
} catch (e) {
currentBuild.result = "FAILED"
notifyBuild(currentBuild.result)
throw e
}
finally {
notifyBuild(currentBuild.result)
}
}
def notifyBuild(String buildStatus = 'STARTED') {
buildStatus = buildStatus ?: 'SUCCESSFUL'
def subject = "${buildStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
def summary = "${subject} (${env.BUILD_URL})"
def details = """
<p>STARTED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
<p>Check console output at "${env.JOB_NAME} [${env.BUILD_NUMBER}]"</p>
"""
emailext (
subject: subject,
body: details,
attachLog: true,
to: ${DEFAULT_RECIPIENTS},
recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider'], [$class: 'CulpritsRecipientProvider']]
)
}
I trying to send the email to a user who triggered the job but I did not get emails to DEFAULT_RECIPIENTS. I also tried with to: env.DEFAULT_RECIPIENTS.
I am getting this error:
groovy.lang.MissingPropertyException: No such property: $DEFAULT_RECIPIENTS for class: groovy.lang.Binding
I also having same problem, just now I got solution on this check the below step:
emailext body: '''${SCRIPT, template="groovy-html.template"}''',
subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - Successful",
mimeType: 'text/html',to: '$DEFAULT_RECIPIENTS'
note down the single qoutes around $DEFAULT_RECIPIENTS that is the key to success here.
I don't know why is not working with double qoutes :(
You should go to Manage Jenkins->Configure System->Extended E-mail Notification. There you should fill the Default Recipient field and Save. (If you do not have the Email Extension Plugin, please install it).
Then you can use the $DEFAULT_RECIPIENT variable in the 'to' clause. Please, remove the braces.
Hope this helps.

When using a Jenkins pipeline, is there a way to get the user that responded to an input() action?

Consider the following example
node {
stage('Build') {
echo "do buildy things"
}
stage('Deploy') {
hipchatSend(
color: "PURPLE",
message: "Holding for deployment authorization: ${env.JOB_NAME}, job ${env.BUILD_NUMBER}. Authorize or cancel at ${env.BUILD_URL}",
)
input('Push to prod?') //Block here until okayed.
echo "Deployment authorized by ${some.hypothetical.env.var}"
echo "do deploy things"
}
}
When responding to the input, the user name that clicked the button is stored in the build log.
Is this username made available in a variable that I could use in, say, another hipChatSend?
Supply the field submitterParameter to input:
def userName = input message: '', submitterParameter: 'USER'
echo "Accepted by ${userName}"
The value of submitterParameter doesn't matter if you doesn't have any parameters. But if you have parameters, then it will specify the name of the array element which holds the value:
def ret = input message: '', parameters: [string(defaultValue: '', description: '', name: 'para1')], submitterParameter: 'USER'
echo "Accepted by ${ret['USER']}"

Resources