Here's one environment variable MODEL in my Jenkins declarative script. But I am not able to use this MODEL variable into the email body. Though it is working fine in the email subject!
pipeline {
agent any
stages {
stage(‘Test’) {
environment {
MODEL = “Some ML Model v1.2.3”
}
steps {
...
}
post {
always {
emailext attachLog: true,
attachmentsPattern: ‘**/reports/*.html’,
mimeType: “text/html”,
to: 'myemail#company.com’,
subject: “Test - ${env.MODEL} Build [${env.BUILD_NUMBER}]“,
body: '''<html>
<p><b>Regression Test Report</b></p>
<p>$MODEL</p>
<p>${MODEL}</p>
<p>"${MODEL}"</p>
<p>"${env.MODEL}"</p>
<p>""'${MODEL}'""</p>
<p>""'${env.MODEL}'""</p>
<p>"""${env.MODEL}"""</p>
<p>${ENV,var="MODEL"}</p>
<p>"${ENV,var="MODEL"}"</p>
<p>Build URL: $BUILD_URL</p>
<p>Build Status: $BUILD_STATUS</p>
<br />
<p><b>Console Output</b></p>
<pre>${BUILD_LOG_REGEX, regex="^.*test session starts.*$", showTruncatedLines=false}</pre>
<pre>${BUILD_LOG_EXCERPT, start="^.*test.*==$", end="^.*in.*==$"}</pre>
...
</html>'''
}
}
}
}
}
I have tried multiple things as shown, but this script is just producing following email body:
Also went through several links like the following but still did not get the drill. Please suggest something.
Jenkins: Passing user defined variables to Email-Ext plugin
For such cases you need to use triple double quotes when defining template:
body:"""
var: ${var}
envvar: ${env.VAR}
"""
I tryed '''+VAR+''' and it's work fine!
Related
How do I use the email-id in prior stages from the variable EmailDL which I have used in the email text plugin in the declarative pipeline template in the last stage?
Is there any Global variable for the EmailDL/recipient/To like $BUILDID which can be used?
we have EmailDL to receive Job Status Emails to collect the email id & use it in the template
post {
always {
emailext (
body:
subject: "XXXXXX",
mimeType: 'text/html',
to: "${EmailDL}"
)
}
}
All works good but how do I reuse the parameter EmailDL outside of the Post stages?
I would suggest define it in environment like this
environment {
EmailDL = "abc#.com"
}
and use it anywhere using ${EmailDL}
I am using Jenkins with the email extension plugin and declarative pipelines. In https://jenkinsserver/configure i configured the "Default Subject" and "Default Content" which i want to use in a pipeline script.
When i add the following code to a pipeline script, everything works perfectly fine.
post {
always {
emailext (
to: 'my#my.dom',
replyTo: 'my#my.dom',
subject: "foo",
body: "bar",
mimeType: 'text/html'
);
}
}
But i don't want to specify something in the pipeline script, everything should be done whith the data specified in the global configuration. When i remove everything and just call emailext (); it failes with the comment that subject is missing. What can i do to work with default values specified globally?
As stated in the plugin documentation:
The email-ext plugin uses tokens to allow dynamic data to be inserted into recipient list, email subject line or body. A token is a string that starts with a $ (dollar sign) and is terminated by whitespace. When an email is triggered, any tokens in the subject or content fields will be replaced dynamically by the actual value that it represents.
This pipeline block should use the default subject and content from Jenkins configuration:
post {
always {
emailext (
to: 'my#my.dom',
replyTo: 'my#my.dom',
subject: '$DEFAULT_SUBJECT',
body: '$DEFAULT_CONTENT',
mimeType: 'text/html'
);
}
}
Make sure to use single quotes, otherwise Groovy will try to expand the variables.
Im trying to send an email within jenkinsfile, the email subject contain two variable one of them exists on jenkinsfile APP_NAME and the other one is jenkins Global variable BUILD_STATUS
im getting null instead of the actual value for the build status
environment {
mvnHome = tool name: 'myMvn', type: 'maven'
mvnCMD = "${mvnHome}/bin/mvn"
APP_NAME = 'test'
}
post {
success {
emailext body: '$DEFAULT_CONTENT',
to: '$DEFAULT_RECIPIENTS',
subject: "${APP_NAME} Health Check: ${env.BUILD_STATUS}",
attachmentsPattern: "**/target/${APP_NAME}.jpg"
}
}
when i changed the subject in the form below
'$APP_NAME Health Check: $BUILD_STATUS' with single quote i got the actual build status but APP_NAME appears on email $APP_NAME instead of actual name
how i can solve this conflict BUILD_STATUS needs single quote but APP_NAME needs double quote
Solved By creating new variable includes the BUILD_STATUS global variable
environment {
DEFAULT_SUBJECT = 'Health Check: $BUILD_STATUS'
}
Then call this variable as shown below
emailext body: '$DEFAULT_CONTENT',
to: '$DEFAULT_RECIPIENTS',
subject: "${APP_NAME} ${DEFAULT_SUBJECT}",
attachmentsPattern: "**/target/${APP_NAME}.jpg"
}
In my Jenkins pipelines I generally use post declarative function to send me an email incase the pipeline has failed.
A simple syntax of the post function is as under:
post {
failure {
mail to: 'team#example.com',
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
body: "Something is wrong with ${env.BUILD_URL}"
}
}
In the above email, I also want to mention which stage (lets say the pipeline has 5 to 6 stages) of the pipeline has failed. How can I do that? Any help is much appreciated.
An extension to the above requirement will be to provide the user with the actual error log (of the stage that has failed) also as a part of the failure notification email.
Idea is, when a user receives a failure notification from jenkins, he should know which stage of the pipeline has failed along with the error log.
Thanks in advance.
There is a variable called env.STAGE_NAME which you can use. However, in your case you will probably need to store the stage name in a different variable, because when you get the env.STAGE_NAME in a post stage the result will be Declarative: Post Actions. Instead, you will need to store the stage name in a variable in all stages. So once a stage fails - Jenkins will not continue with the next stages and therefore you will have the "failed" stage name.
Here's an example:
def FAILED_STAGE
pipeline {
agent { label "master" }
stages {
stage("Stage 1") {
steps {
script {
FAILED_STAGE=env.STAGE_NAME
echo "stage 1"
}
}
}
stage("Stage 2") {
steps {
script {
FAILED_STAGE=env.STAGE_NAME
echo "stage 2"
error "failed for some reason."
}
}
}
stage("Stage 3") {
steps {
script {
FAILED_STAGE=env.STAGE_NAME
echo "stage 3"
}
}
}
}
post {
failure {
echo "Failed stage name: ${FAILED_STAGE}"
}
}
}
There might be a better way to do it, but I haven't found it so far.
Regarding the logs - As of JENKINS-40526 you could possibly use the API and get the log file from there, but I am not sure you can get the parameters you need from within the pipeline. The other solution would be to use emailext and email the entire build log file:
emailext attachLog: true, body: '', compressLog: true, subject: 'Build failed.', to: 'somebody#somewhere.com'
I need to pass some variables to an email template after build. I use environment blocks for that (some of them also are created in scripts).
Is this possible?
environment {
SUBSCRIPTION = credentials('subscription')
CERT = credentials('cert')
}
post {
always {
emailext attachLog: true,
body: '''${SCRIPT,template="email.template"}''',
compressLog: true,
mimeType: 'text/html',
subject: "SUCCESS: ${env.JOB_NAME} [${env.BUILD_NUMBER}]",
to: 'email#email.com'
}
}
I made workaround for that, in post step I trigger another job with all data that I need (pass as params) than inside scripted email template I can use them