I'm using the Email-Ext plugin to send a results email at the end of my build in the post build stage of a declarative pipeline. In the body of the email I'm trying to insert some environment variables and some user defined environment variables.
The built in variables show up fine, however any user defined variables I define are just empty in the body of the email or don't work in the attachmentPattern field either:
My environment variables:
pipeline {
agent any
environment {
buildFolder = "build_${BUILD_NUMBER}"
robotFolder = "build_${BUILD_NUMBER}/robotDemo"
serverName = "abc123"
robotResults = "${buildFolder}/*.txt"
}
The email-ext format I am using in my post{} build:
emailext attachLog: true, attachmentsPattern: 'build_${env.BUILD_NUMBER}/Robot_Results.txt', body: '<b>Job Name:</b> ${ENV, var="JOB_NAME"}<br><b>Build Number:</b> ${ENV, var="BUILD_NUMBER"}<br><br><b>Build URL:</b><br>${ENV, var="BUILD_URL"}<br><br><b>Log Files:<br><br><br><pre>${BUILD_LOG_EXCERPT, start="^====", end="^report.html"}</pre><pre>${ENV, var="serverName"}</pre>', mimeType: 'text/html', subject: 'ABS Results', to: 'richard.scott#sap.com'
For example: ${ENV, var="JOB_NAME"}, shows up fine. However ${ENV, var="serverName"} doesn't show at all.
I've tried variations such as...
${serverName}
${env.serverName}
...but they don't work either.
Any ideas how to use user defined environment variables like 'serverName' above in the email-ext plug-in?
Thanks & best regards, Richard.
You’re using single quotes, but only double quotes allow variables to be interpolated.
I would suggest you to use a Groovy script for rendering the email contents.
You can either use the predefined Groovy script like this:
emailext body: '''${SCRIPT, template="groovy-html.template"}'''
Or add your own template like described in the documentation. I couldn't figure our if it's possible to load that template from the workspace, but you could try it with:
emailext body: '''${SCRIPT, template="$WORKSPACE/groovy-html.template"}'''
The syntax for getting values from the environment or parameters when interpolating is:
${env.MY_ENVVAR}
${param.MY_BUILDPARAM}
(env and param is ja Groovy map holding all the values)
And for all other variables:
${varName}
Related
I want to use property file in DSL job which will take my project name in job name and svn location . Can anyone have idea how to write and syntax?
For handling properties files stored outside your repository, you have a plugin called "Config File Provider Plugin".
You use it like this:
stage('Add Config files') {
steps {
configFileProvider([configFile(fileId: 'ID-of-file0in-jenkins', targetLocation: 'path/destinationfile')]) {
// some block
}
}
}
It is capable of replacing tokens in json and xml or the whole file (as in the example)
For handling data comming from the SVN or project name you can access the environment variables. See this thread and this link
In Jenkins classic interface it is possible to create a link in the output console, but I can't see how I can make something clicable in the Blue Green interface.
You can get the solution here :
How to display a hyperlink in hudson/jenkins build output console
You can also use a workaround for that which is fairly customisable than the method mentioned above.
After finishing your build you can add functionality to your build to send a mail with the report link.
You can do it through the Jenkinsfile or using the job configuration as well.
The Jenkinsfile groovy script is as follows.
script {
emailext subject: ' REPORT-Version/'+ "${G_BRANCH}" + ' $DEFAULT_SUBJECT' ,
body: "Click the link below to show Mocha Testing Results for your current build :<br><br>BAE 4.5 Release Report" ,
attachLog: true,
replyTo: '$DEFAULT_REPLYTO',
to: '$DEFAULT_RECIPIENTS'
}
Through Job configuration you can go to Job --> Configure --> Post Build Actions --> Editable Email Notifications.
Well, it works almost out of the box. Blue Ocean automatically recognizes URL patterns and transform it in a clickable link. For example:
echo "CUCUMBER: ${BUILD_URL}cucumber-html-reports/overview-features.html"
Unfortunately it looks like it isn't possible to create a clickable arbitrary text.
I need to replace a line in a file. If the line starts with the term "url", I need to replace the value.
file.txt --
...
url : http://www.google.com
..
I need to change this value to
url : http://www.facebook.com
I tried the following code but it did not work -
FACEBOOK_URL = "http://www.facebook.com"
sh("sed -i \\"s?^url.*\\$?url: ${FACEBOOK_URL}?\\" file.txt")
I'm using a Jenkins Pipeline. I need to replace the string using a variable.
Jenkins 2 Pipeline builds use Groovy and it is very easy to read the file using readfile and then we can do the changes
def text = readFile "file.txt"
text.replaceAll("url.*", "url: ${FACEBOOK_URL}")
The above code will help in replacing the text in the file, if you want to write the content to file, you can use writeFile
You can use this for replacing a string in a file in Jenkins 2 Pipeline builds:
def text = readFile file: "file.txt"
text = text.replaceAll("%version%", "${VERSION}")
writeFile file: "file.txt", text: text
I am using Cppcheck Plugin. I am trying to send Cppcheck Results URL in email once the Jenkins job is completed.
I tried setting Default Content to following:
View this URL for Cppcheck Results: $BUILD_URL + "cppcheckResult/"
But the email which I got had following text:
View this URL for Cppcheck Results: https://myurl/job/fscopy/7/+"cppcheckResult/"
Can someone help me figuring out how can I send correct URL in email which is following:
https://myurl/job/fscopy/7/cppcheckResult/
I managed to resolved it by creating CPPCHECK environment variable and using it as below:
View this URL for Cppcheck Results: $BUILD_URL$CPPCHECK
which gave me output in email as following:
View this URL for Cppcheck Results: https://myurl/job/fscopy/7/cppcheckResult/
I've a jenkins job with few parameters setup and I've a JSON file in the workspace which has to be updated with the parameters that I pass through jenkins.
Example:
I have the following parameters which I'll take input from user who triggers the job:
Environment (Consider user selects "ENV2")
Filename (Consider user keeps the default value)
I have a json file in my workspace under run/job.json with the following contents:
{
environment: "ENV1",
filename: "abc.txt"
}
Now whatever the value is given by user before triggering a job has to be replaced in the job.json.
So when the user triggers the job, the job.json file should be:
{
environment: "ENV2",
filename: "abc.txt"
}
Please note the environment value in the json which has to be updated.
I've tried https://wiki.jenkins-ci.org/display/JENKINS/Config+File+Provider+Plugin plugin. But I'm unable to find any help on parameterizing the values.
Kindly suggest on configuring this plugin or suggest any other plugin which can serve my purpose.
Config File Provider Plugin doesn't allow you to pass parameters to configuration files. You can solve your problem with any scripting language. My favorite approach is using Groovy plugin. Hit a check-box "Execute system Groovy script" and paste the following script:
import groovy.json.*
// read build parameters
env = build.getEnvironment(listener)
environment = env.get('environment')
filename = env.get('filename')
// prepare json
def builder = new JsonBuilder()
builder environment: environment, filename: filename
json = builder.toPrettyString()
// print to console and write to a file
println json
new File(build.workspace.toString() + "\\job.json").write(json)
Output sample:
{
"environment": "ENV2",
"filename": "abc.txt"
}
With Pipeline Utility Steps plugin this is very easy to achieve.
jsonfile = readJSON file: 'path/to/your.json'
jsonfile['environment'] = 'ENV2'
writeJSON file: 'path/to/your.json', json: jsonfile
I will keep it simple. A windows batch file or a shell script (depending on the OS) which will read the environment values and open the JSON file and make the changes.