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
Related
I have a jenkins pipeline where I wait for user input to proceed or abort. Is there any way I can send the same in email to let the concerned person aware and he can click on the email to procceed Or abort the pipeline.?
It is possible to execute such action by using Jenkins API, this can be achieved using Jenkins Rest Api you can check this SO question.
I haven't tried this solution yet but I guess this pipeline could help you get a better idea of how I would think about implementing such behavior in a pipeline like this:
pipeline {
agent any
stages {
stage('Mail Notification') {
steps {
echo 'Sending Mail'
mail bcc: '',
body: 'Stop job through this link: ${env.BUILD_URL}/job/${env.JOB_NAME}/${env.BUILD_NUMBER}/stop',
cc: '',
from: '',
replyTo: '',
subject: 'Jenkins Job',
to: 'example#domain.com'
}
}
}
}
Jenkins pipeline is aware of such variables like ${env.BUILD_URL} ${env.JOB_NAME} ${env.BUILD_NUMBER}
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!
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"
}