Trying to get jenkins to include the readme as an attachment with a pipeline
stage('email Alex!'){
mail(
body: 'your component is released',
attachmentsPattern: '**/*.md',
from: env.DEFAULT_REPLYTO,
replyTo: env.DEFAULT_REPLYTO,
subject: 'README',
to: 'alexander.lovett#bt.com'
)
}
In this test the dir stucture is:
--currentDir
|--Project
|--README.md
I just get an email with the body and no attachment though :/
Does anyone know how to do this?
You should install this plugin :
https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin
And replace your code with this :
stage('email Alex!'){
emailext(
body: 'your component is released',
attachmentsPattern: '**/*.md',
from: env.DEFAULT_REPLYTO,
replyTo: env.DEFAULT_REPLYTO,
subject: 'README',
to: 'alexander.lovett#bt.com'
)
}
Related
I am trying to send S3 url file in mail body, but I am getting error
Errno::ENOENT: No such file or directory # rb_sysopen
I want something like this, but I am unable to achieve this one
#path = s3_url
attachments["output.pdf"] = {
mime_type: "application/pdf",
content: HTTParty.get(#path).response.try(:body)
}
mail(to: 'xyz#gmail.com', subject: "Test Attchment", body: File.read(URI.parse(#path)))
Fetch the PDF using Net::HTTP or HTTPary and read the response to set the attachment in the mailer.
Try it this way:
# change the file name if required
mail.attachments["output.pdf"] = {
mime_type: "application/pdf",
content: HTTParty.get(s3_path).response.try(:body)
}
Question
I am running Jenkins for job automation and using Okta for authentication. I would like to create a Jenkins job that I can run on demand to create a user in Okta. The user will have the the attributes required by Okta: email, username, etc.
How can I accomplish this in Jenkins?
Initial Setup
I wrote a Jenkinsfile that will create an Okta user via the Okta API Documentation. Before you can run this script you need to install the following plugin's in Jenkins.
Credentials Binding
Pipeline Step Utilities
Http Request Plugin
After installing the aforementioned plugins you will need to create an Okta API Token and save it in Jenkin's Credential Manager of kind Secret Text ( and give it an ID of okta-api-token ).
Proof-of-Concept
The following is a proof-of-concept Jenkinsfile that will use the following plugins to create a user in Okta
pipeline {
agent {
label 'master'
}
options {
buildDiscarder( logRotator( numToKeepStr: "30" ) )
}
parameters {
string(name: 'firstName', description: 'New users first name')
string(name: 'lastName', description: 'New users last name')
string(name: 'email', description: 'New users email')
string(name: 'mobilePhone', description: 'New users phone')
password(name: 'password', description: 'Enter Password')
}
environment {
oktaDomain = "yourdomain.com"
}
stages {
stage('Execute') {
steps {
script {
// Create payload based on https://developer.okta.com/docs/reference/api/users/#request-example-3
def payload = """
{ "profile":{"firstname": "$firstName","lastNAme": "$lastName","email": "$email","login": "$email","mobilePhone": "$mobilePhone"}, "credentials": { "password:{ "value": "$password"}}}
"""
// Send HTTP Post request with API Token saved in credential manager
withCredentials([string(credentialsId: 'apiToken', variable: 'okta-api-token')]) {
def response = httpRequest(
acceptType: 'APPLICATION_JSON',
contentType: 'APPLICATION_JSON',
httpMode: 'POST',
requestBody: payload,
url: "https://${oktaDomain}/api/v1/users?activate=true",
customHeaders: [[Authentication: "SSWS ${apiToken}"]]
)
}
def json = readJSON text: response.content
echo json['id']
}
}
}
}
post {
changed {
emailext subject: 'Your Okta user has been created',
body: 'Your Okta user has been created',
replyTo: '$DEFAULT_REPLYTO',
to: "$email"
}
}
}
Assuming you followed the steps listed above you should only need to change the oktaDomain variable to your Okta domain.
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 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.
In Jenkins pipeline I'm using email-ext with emailextrecipients as follows:
emailext(
subject: email_subject,
mimetype: 'text/html',
to: emailextrecipients([[$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider']]),
body: email_body
)
And I want to add a specific email address, e.g. admin#myshop.com, to the list generated using emailextrecipients. I want that addressee (me or a manager or admin) to always get the email, but the addressee might be a culprit or requester and I don't want emailext to send two emails to that addressee.
Is there a way to merge 'admin#myshop.com' with emailextrecipients?
I don't know how I missed this, but the answer is in the email-ext doc. Use the to: for the additional email addresses, and use recipientProviders: instead of to: emailextrecipients:
emailext(
subject: email_subject,
mimetype: 'text/html',
to: 'admin#myshop.com',
recipientProviders: [[$class: 'CulpritsRecipientProvider'], [$class: 'RequesterRecipientProvider']],
body: email_body
)
A slight variation to Generic Ratzlaugh's answer, in case you need to use conditional logic for email destinations:
def recipientProviders = [];
recipientProviders.add([$class: 'CulpritsRecipientProvider']);
recipientProviders.add([$class: 'DevelopersRecipientProvider']);
recipientProviders.add([$class: 'RequesterRecipientProvider']);
emailext(
subject: email_subject,
mimetype: 'text/html',
to: 'admin#myshop.com',
recipientProviders: recipientProviders,
body: email_body
)