How to append string to Jenkins variable "BUILD_URL" - jenkins

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/

Related

Jenkins: Passing user defined variables to Email-Ext plugin

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}

How to display a hyperlink in jenkins Blue Ocean UI?

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.

How to send cucumber html report on mail

I have automation project which i'm running through Jenkins . With build successful or failure all html reports are being displayed after i click on link "Cucumber Feature Level Overview Report" .
Added dependency in pom
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>email-ext</artifactId>
<version>2.25</version>
</dependency>
Added code in jenkins job :-
Section : Editable Email Notification -> Default Content
${FILE,path="https://jenkins.company.com/dareProject/job/Automation_Jobs/job/Dare_Project_job/Cucumber_Steps_Level_Overview_Report/"}
The mail which i'm getting with error :-
ERROR: File 'https://jenkins.company.com/dareProject/job/Automation_Jobs/job/Dare_Project_job/Cucumber_Steps_Level_Overview_Report/' does not exist
And how to display report in Body Text of email ?
The mail which i'm getting with error
In path parameter you must use the location of file in filesystem relative to job's workspace, not the hyperlink to it!
And how to display report in Body Text of email ?
Set correct path in Default Content field and choose HTML (text/html) in Content Type field.

Jenkins: add some text to the build summary

For each build in jenkins there's a page with a short summary of what happened in that build.
In my setup this page contains the list of commit messages that were added in this build, the name of the user who started that build and a note from the git plugin about the commit SHA1 and the branch name.
Is there a way for a normal script that I run as a build step to add something to this page? Is there a plugin which allows this?
I want to add a line with an HTML link to where the user can download this build.
You can use the Description Setter plugin to add a custom HTML description.
A quick example :)
Does it help?
UPDATE: here is the syntax for a Jenkins pipeline script
currentBuild.description = "<a href='http://stackoverflow.com'>Stackoverbuild build" + env.BUILD_ID + "</a>"
I used Badge Plugin for something like this.
Example:
def summary1 = createSummary(icon:"notepad.png", text:"started Builds:<br>")
summary1.appendText("myBuild1: SUCCESS<br>", false)
summary1.appendText("myBuild2: UNSTABLE<br>", false)

Writing to a json file in workspace using Jenkins

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.

Resources