I am having problems receiving email notifications for a pipeline I have setup like so:
#!groovy
pipeline {
agent any
stages {
stage('Build Prep') {
steps {
sh '...'
}
}
stage('Build') {
steps {
sh '...'
}
}
stage('Tests') {
steps {
parallel (
"Jasmine": {
sh '...'
},
"Mocha": {
sh '...'
}
)
}
}
stage('Deploy') {
steps {
sh "..."
}
}
}
post {
success {
emailext to:"me#me.com", subject:"SUCCESS: ${currentBuild.fullDisplayName}", body: "Yay, we passed."
}
failure {
emailext to:"me#me.com", subject:"FAILURE: ${currentBuild.fullDisplayName}", body: "Boo, we failed."
}
unstable {
emailext to:"me#me.com", subject:"UNSTABLE: ${currentBuild.fullDisplayName}", body: "Huh, we're unstable."
}
changed {
emailext to:"me#me.com", subject:"CHANGED: ${currentBuild.fullDisplayName}", body: "Wow, our status changed!"
}
}
}
The build logs confirm an email is sent, but I dont get anything in my inbox, nothing in spam either.
I have come across this https://jenkins.io/blog/2016/07/18/pipline-notifications/
But I am unsure if I can use using the syntax I have, I don't have any nodes defined, should I?
Your script works fine. I did receive the email using the above code. Please check your other settings or if you had created a rule for Jenkins emails. You can make your code look easier by adding your notifications to the Jenkins shared library. Please take a look at the below link.
https://jenkins.io/doc/book/pipeline/shared-libraries/
Related
I am trying to build a logic for our UAT team to deploy code only upon approval from manager . I would like to know how can be achieved ? Email approval ?. Please help
You can try something like this
//this will grab user - who is running the job
def user
node {
wrap([$class: 'BuildUser']) {
user = env.BUILD_USER_ID
}
emailext mimeType: 'text/html',
subject: "[Jenkins]${currentBuild.fullDisplayName}",
to: "user#xxx.com",
body: '''click to approve'''
}
pipeline {
agent any
stages {
stage('deploy') {
input {
message "Should we continue?"
ok "Yes"
}
when {
expression { user == 'hardCodeApproverJenkinsId'}
}
steps {
sh "echo 'describe your deployment' "
}
}
}
}
you can also get some more help from this StackOverflow question
Say I have a simple Jenkins pipeline file as below:
pipeline {
agent any
stages {
stage('Test') {
steps {
sh ...
}
}
stage('Build') {
steps {
sh ...
}
}
stage('Publish') {
when {
buildingTag()
}
steps {
sh ...
send_slack_message("Built tag")
}
}
}
post {
failure {
send_slack_message("Error building tag")
}
}
}
Since there's a lot non-tag builds everyday, I don't want to send any slack message about non-tag builds. But for the tag builds, I want to send either a success message or a failure message, despite of which stage it failed.
So for the above example, I want:
When it's a tag build, and stage 'Test' failed, I shall see a "Error building tag" message. (This is a yes in the example)
When it's a tag build, and all stages succeeded, I shall see a "Built tag" message. (This is also a yes in the example)
When it's not a tag build, no slack message will ever been sent. (This is not the case in the example, for example, when the 'Test' stage fails, there's will be a "Error building tag" message)
As far as I know, there's no such thing as "conditional post section" in Jenkins pipeline syntax, which could really help me out here. So my question is, is there any other way I can do this?
post {
failure {
script {
if (isTagBuild) {
send_slack_message("Error building tag")
}
}
}
}
where isTagBuild is whatever way you have to differentiate between a tag or no tag build.
You could also apply the same logic, and move send_slack_message("Built tag") down to a success post stage.
In the postbuild step you can also use script step inside and use if. And inside this if step you can add emailext plugin.
Well, for those who just want some copy-pastable code, here's what I ended-up with based on #eez0's answer.
pipeline {
agent any
environment {
BUILDING_TAG = 'no'
}
stages {
stage('Setup') {
when {
buildingTag()
}
steps {
script {
BUILDING_TAG = 'yes'
}
}
}
stage('Test') {
steps {
sh ...
}
}
stage('Build') {
steps {
sh ...
}
}
stage('Publish') {
when {
buildingTag()
}
steps {
sh ...
}
}
}
post {
failure {
script {
if (BUILDING_TAG == 'yes') {
slackSend(color: '#dc3545', message: "Error publishing")
}
}
}
success {
script {
if (BUILDING_TAG == 'yes') {
slackSend(color: '#28a745', message: "Published")
}
}
}
}
}
As you can see, I'm really relying on Jenkins built-in buidingTag() function to help me sort things out, by using an env-var as a "bridge". I'm really not good at Jenkins pipeline, so please leave comments if you have any suggestions.
Hi I was trying to send email alert whenever there is a build failed for Jenkins
node ('abc') {
stage ('checkout'){
some codes inside
}
stage ('build'){
some code inside
}
stage ('test') {
some code inside
}
stage ('deploy'){
some code inside
}
post {
failure {
mail bcc: '', body: '''Hi,
The pipeline at Jenkins has failed. Pleas go over to the Jenkins and
check it out.
Thanks!''', cc: '', from: '', replyTo: '', subject: 'The pipeline has failed!', to: 'abc#gmail.com'
}
}
}
And I have error message of
java.lang.NoSuchMethodError: No such DSL method 'post' found among
steps
You can achieve this by doing try-catch-finally block.
node ('abc') {
try {
stage ('checkout'){
some codes inside
}
stage ('build'){
some code inside
}
stage ('test') {
some code inside
}
stage ('deploy'){
some code inside
}
stage ('notify'){
Notification for JOB Success
}
}catch(e){
Notification for JOB Failure
}finally{
}
}
See official Jenkins documentation:
https://jenkins.io/doc/pipeline/tour/running-multiple-steps/#finishing-up
(If you don't want to use declarative pipeline, see 'Toggle Scripted Pipeline' section).
As far as declarative pipelines go in Jenkins, I'm having trouble with the when keyword.
I keep getting the error No such DSL method 'when' found among steps. I'm sort of new to Jenkins 2 declarative pipelines and don't think I am mixing up scripted pipelines with declarative ones.
The goal of this pipeline is to run mvn deploy after a successful Sonar run and send out mail notifications of a failure or success. I only want the artifacts to be deployed when on master or a release branch.
The part I'm having difficulties with is in the post section. The Notifications stage is working great. Note that I got this to work without the when clause, but really need it or an equivalent.
pipeline {
agent any
tools {
maven 'M3'
jdk 'JDK8'
}
stages {
stage('Notifications') {
steps {
sh 'mkdir tmpPom'
sh 'mv pom.xml tmpPom/pom.xml'
checkout([$class: 'GitSCM', branches: [[name: 'origin/master']], doGenerateSubmoduleConfigurations: false, submoduleCfg: [], userRemoteConfigs: [[url: 'https://repository.git']]])
sh 'mvn clean test'
sh 'rm pom.xml'
sh 'mv tmpPom/pom.xml ../pom.xml'
}
}
}
post {
success {
script {
currentBuild.result = 'SUCCESS'
}
when {
branch 'master|release/*'
}
steps {
sh 'mvn deploy'
}
sendNotification(recipients,
null,
'https://link.to.sonar',
currentBuild.result,
)
}
failure {
script {
currentBuild.result = 'FAILURE'
}
sendNotification(recipients,
null,
'https://link.to.sonar',
currentBuild.result
)
}
}
}
In the documentation of declarative pipelines, it's mentioned that you can't use when in the post block. when is allowed only inside a stage directive.
So what you can do is test the conditions using an if in a script:
post {
success {
script {
if (env.BRANCH_NAME == 'master')
currentBuild.result = 'SUCCESS'
}
}
// failure block
}
Using a GitHub Repository and the Pipeline plugin I have something along these lines:
pipeline {
agent any
stages {
stage('Build') {
steps {
sh '''
make
'''
}
}
}
post {
always {
sh '''
make clean
'''
}
success {
script {
if (env.BRANCH_NAME == 'master') {
emailext (
to: 'engineers#green-planet.com',
subject: "${env.JOB_NAME} #${env.BUILD_NUMBER} master is fine",
body: "The master build is happy.\n\nConsole: ${env.BUILD_URL}.\n\n",
attachLog: true,
)
} else if (env.BRANCH_NAME.startsWith('PR')) {
// also send email to tell people their PR status
} else {
// this is some other branch
}
}
}
}
}
And that way, notifications can be sent based on the type of branch being built. See the pipeline model definition and also the global variable reference available on your server at http://your-jenkins-ip:8080/pipeline-syntax/globals#env for details.
Ran into the same issue with post. Worked around it by annotating the variable with #groovy.transform.Field. This was based on info I found in the Jenkins docs for defining global variables.
e.g.
#!groovy
pipeline {
agent none
stages {
stage("Validate") {
parallel {
stage("Ubuntu") {
agent {
label "TEST_MACHINE"
}
steps {{
sh "run tests command"
recordFailures('Ubuntu', 'test-results.xml')
junit 'test-results.xml'
}
}
}
}
}
post {
unsuccessful {
notify()
}
}
}
// Make testFailures global so it can be accessed from a 'post' step
#groovy.transform.Field
def testFailures = [:]
def recordFailures(key, resultsFile) {
def failures = ... parse test-results.xml script for failures ...
if (failures) {
testFailures[key] = failures
}
}
def notify() {
if (testFailures) {
... do something here ...
}
}
I have a pipeline script that looks like this:
node {
try {
stage('Prepare') {
// git clone here
}
stage('Compile') {
sh "make ${build_type}"
}
stage('Test') {
sh "./run tests ${build_type}"
}
}
finally {
if (fileExists("test_results.xml")) {
junit "test_results.xml"
}
emailext subject: "Build finished",
body: '${JELLY_SCRIPT, template="some-template.template"}',
to: "some-one#somewhere"
}
}
${build_type} can be "release" or "debug".
When my build receives a trigger, I want my pipeline to run once for every parameter in ${build_type} and then send me one email containing a report about both builds.
How can I achieve this?
I tried to define a parallel block inside the Compile stage and set build_type there, but this doesn't make the other stages to run in parallel.
I hope the following snippet can help you. This way you can include multiple build types dev,qa,prod.
def build_types = "dev;qa"
node {
try {
stage('Prepare') {
// git clone here
}
def buildTypeVar = build_types.tokenize(';')
for(int i=0;i<buildTypeVar.size();i++){
buildType=buildTypeVar.get(i).trim()
stage('Compile ${build_type}') {
sh "make ${build_type}"
}
stage('Test ${build_type}') {
sh "./run tests ${build_type}"
}
}
}
finally {
if (fileExists("test_results.xml")) {
junit "test_results.xml"
}
emailext subject: "Build finished",
body: '${JELLY_SCRIPT, template="some-template.template"}',
to: "some-one#somewhere"
}
}