0
0
Jenkinsdevops~20 mins

Email notifications in pipelines in Jenkins - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Email Notification Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Email Notification Syntax in Jenkins Pipeline
What is the output when the following Jenkins pipeline snippet runs, assuming the email plugin is correctly configured and the build fails?
Jenkins
pipeline {
  agent any
  stages {
    stage('Failing Stage') {
      steps {
        script {
          error('Forced failure')
        }
      }
    }
  }
  post {
    failure {
      emailext(
        subject: "Build Failed: ${env.JOB_NAME} #${env.BUILD_NUMBER}",
        body: "The build failed. Check the console output at ${env.BUILD_URL}console",
        to: 'team@example.com'
      )
    }
  }
}
AAn email is sent to team@example.com with the subject 'Build Failed: <job_name> #<build_number>' and a body containing the build URL.
BNo email is sent because the 'failure' block only triggers on success.
CThe pipeline throws a syntax error due to missing parentheses in the emailext step.
DThe email is sent but only if the build succeeds, not on failure.
Attempts:
2 left
💡 Hint
Check the 'post' section and the 'failure' condition in Jenkins pipelines.
Troubleshoot
intermediate
2:00remaining
Troubleshooting Missing Email Notifications
A Jenkins pipeline uses the emailext step in the post section to send failure notifications. However, no emails are received even though the build fails. Which of the following is the most likely cause?
AThe SMTP server is not configured or unreachable in Jenkins global settings.
BThe pipeline script uses 'mail' instead of 'emailext' step.
CThe 'post' block is placed inside the 'stages' block instead of directly under 'pipeline'.
DThe 'failure' condition is misspelled as 'failuer' in the post block.
Attempts:
2 left
💡 Hint
Check Jenkins global configuration for email settings.
Configuration
advanced
2:00remaining
Configuring Email Recipients Dynamically
Which Jenkins pipeline snippet correctly sends an email notification to a dynamic list of recipients stored in a variable called 'emailList' when the build is unstable?
Jenkins
def emailList = 'dev1@example.com,dev2@example.com'

pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        script {
          currentBuild.result = 'UNSTABLE'
        }
      }
    }
  }
  post {
    unstable {
      // Which option correctly sends email to emailList?
    }
  }
}
Aemailext(to: '${emailList}', subject: 'Build Unstable', body: 'Check the build status.')
Bemailext(to: "${emailList}", subject: 'Build Unstable', body: 'Check the build status.')
Cemailext(to: emailList, subject: 'Build Unstable', body: 'Check the build status.')
Demailext(to: '$emailList', subject: 'Build Unstable', body: 'Check the build status.')
Attempts:
2 left
💡 Hint
Use Groovy string interpolation with double quotes for variables.
🔀 Workflow
advanced
2:00remaining
Email Notification Workflow with Multiple Conditions
In a Jenkins pipeline, you want to send an email notification only if the build is either unstable or failed, but not on success or aborted. Which post block configuration achieves this?
A
post {
  failure, unstable {
    emailext(to: 'team@example.com', subject: 'Build Alert', body: 'Check build')
  }
}
B
post {
  always {
    script {
      if (currentBuild.result == 'FAILURE' || currentBuild.result == 'UNSTABLE') {
        emailext(to: 'team@example.com', subject: 'Build Alert', body: 'Check build')
      }
    }
  }
}
C
post {
  failure {
    emailext(to: 'team@example.com', subject: 'Build Failed', body: 'Check build')
  }
  unstable {
    emailext(to: 'team@example.com', subject: 'Build Unstable', body: 'Check build')
  }
}
D
post {
  changed {
    emailext(to: 'team@example.com', subject: 'Build Changed', body: 'Check build')
  }
}
Attempts:
2 left
💡 Hint
Use separate blocks for failure and unstable conditions.
Best Practice
expert
2:00remaining
Best Practice for Sensitive Email Credentials in Jenkins Pipelines
What is the best practice to securely use SMTP credentials for sending email notifications in Jenkins pipelines?
AHardcode SMTP username and password directly in the pipeline script for easy access.
BUse environment variables set on the Jenkins master node without encryption.
CStore SMTP credentials in a plain text file inside the repository and read them during the pipeline.
DStore SMTP username and password as Jenkins credentials and reference them in the pipeline using withCredentials block.
Attempts:
2 left
💡 Hint
Consider Jenkins secure credentials management features.