Bird
Raised Fist0
Jenkinsdevops~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of adding email notifications in Jenkins pipelines?
easy
A. To increase the number of pipeline stages
B. To speed up the pipeline execution
C. To automatically inform team members about pipeline success or failure
D. To reduce the size of the pipeline script

Solution

  1. Step 1: Understand the role of email notifications

    Email notifications are used to keep team members informed about the status of the pipeline automatically.
  2. Step 2: Identify the correct purpose

    Speeding up execution, increasing stages, or reducing script size are unrelated to email notifications.
  3. Final Answer:

    To automatically inform team members about pipeline success or failure -> Option C
  4. Quick Check:

    Email notifications = Inform team automatically [OK]
Hint: Email notifications alert teams about pipeline results [OK]
Common Mistakes:
  • Thinking email notifications speed up pipelines
  • Confusing notifications with pipeline structure changes
  • Assuming notifications reduce script size
2. Which Jenkins pipeline syntax correctly sends an email notification only on failure using the post section?
easy
A. post { failure { emailext to: 'team@example.com', subject: 'Build Failed', body: 'Check the logs.' } }
B. post { always { emailext to: 'team@example.com', subject: 'Build Failed', body: 'Check the logs.' } }
C. post { success { emailext to: 'team@example.com', subject: 'Build Failed', body: 'Check the logs.' } }
D. post { failure { mail to: 'team@example.com', subject: 'Build Failed', body: 'Check the logs.' } }

Solution

  1. Step 1: Identify correct post condition for failure

    The failure block runs only when the pipeline fails, which is correct here.
  2. Step 2: Check the email sending syntax

    emailext is the correct step for sending emails with extended options; mail is less common and may lack features.
  3. Final Answer:

    post { failure { emailext to: 'team@example.com', subject: 'Build Failed', body: 'Check the logs.' } } -> Option A
  4. Quick Check:

    Failure + emailext = Correct syntax [OK]
Hint: Use 'post { failure { emailext ... } }' for failure emails [OK]
Common Mistakes:
  • Using 'always' instead of 'failure' for failure-only emails
  • Using 'success' block for failure notifications
  • Using 'mail' instead of 'emailext' without plugin support
3. Given this Jenkins pipeline snippet, what will happen when the build succeeds?
post {
  success {
    emailext to: 'dev@example.com', subject: 'Build Success', body: 'Good job!'
  }
  failure {
    emailext to: 'dev@example.com', subject: 'Build Failed', body: 'Please check.'
  }
}
medium
A. Both success and failure emails are sent
B. An email with subject 'Build Failed' is sent to dev@example.com
C. No email is sent
D. An email with subject 'Build Success' is sent to dev@example.com

Solution

  1. Step 1: Understand post conditions for success

    The success block triggers only when the build finishes successfully.
  2. Step 2: Identify which email is sent on success

    The email with subject 'Build Success' is sent to 'dev@example.com' in the success block.
  3. Final Answer:

    An email with subject 'Build Success' is sent to dev@example.com -> Option D
  4. Quick Check:

    Success block sends success email [OK]
Hint: Success block emails only on successful builds [OK]
Common Mistakes:
  • Confusing failure email with success email
  • Thinking no email is sent on success
  • Assuming both emails send regardless of build result
4. You wrote this Jenkins pipeline snippet to send failure emails, but no emails are sent on failure:
post {
  failure {
    mail to: 'team@example.com', subject: 'Build Failed', body: 'Fix it!'
  }
}

What is the likely problem?
medium
A. The 'failure' block should be 'always' to send emails on failure
B. The 'mail' step requires proper configuration; use 'emailext' instead
C. The 'to' field must be inside quotes like "to: 'team@example.com'"
D. The 'body' field is missing a required parameter

Solution

  1. Step 1: Check the email step used

    The mail step is basic and may not work without proper setup; emailext from the Email Extension plugin is recommended for reliable emails.
  2. Step 2: Confirm correct step for failure emails

    Using emailext ensures emails are sent with more options and better support in pipelines.
  3. Final Answer:

    The 'mail' step requires proper configuration; use 'emailext' instead -> Option B
  4. Quick Check:

    Use emailext for failure emails [OK]
Hint: Use 'emailext' not 'mail' for reliable email notifications [OK]
Common Mistakes:
  • Using 'mail' without proper configuration
  • Confusing 'failure' with 'always' block
  • Incorrect quoting of parameters
5. You want to send an email notification only if the build fails and the failure is caused by a test failure. Which Jenkins pipeline snippet correctly implements this logic?
hard
A. post { failure { script { if (currentBuild.rawBuild.getLog(100).any { it.contains('TEST-FAILURE') }) { emailext to: 'qa@example.com', subject: 'Test Failure', body: 'Tests failed!' } } } }
B. post { always { emailext to: 'qa@example.com', subject: 'Test Failure', body: 'Tests failed!' } }
C. post { failure { emailext to: 'qa@example.com', subject: 'Test Failure', body: 'Tests failed!' } }
D. post { success { emailext to: 'qa@example.com', subject: 'Test Failure', body: 'Tests failed!' } }

Solution

  1. Step 1: Understand the requirement for conditional email on test failure

    The email should send only if the build fails and the failure is due to test failures, so a condition checking the build log is needed.
  2. Step 2: Analyze the options for conditional logic

    post { failure { script { if (currentBuild.rawBuild.getLog(100).any { it.contains('TEST-FAILURE') }) { emailext to: 'qa@example.com', subject: 'Test Failure', body: 'Tests failed!' } } } } uses a script block to check the build log for 'TEST-FAILURE' text before sending the email, matching the requirement exactly.
  3. Step 3: Exclude other options

    post { always { emailext to: 'qa@example.com', subject: 'Test Failure', body: 'Tests failed!' } } sends email always, ignoring failure condition. post { failure { emailext to: 'qa@example.com', subject: 'Test Failure', body: 'Tests failed!' } } sends email on any failure, not just test failures. post { success { emailext to: 'qa@example.com', subject: 'Test Failure', body: 'Tests failed!' } } sends email on success, which is incorrect.
  4. Final Answer:

    post { failure { script { if (currentBuild.rawBuild.getLog(100).any { it.contains('TEST-FAILURE') }) { emailext to: 'qa@example.com', subject: 'Test Failure', body: 'Tests failed!' } } } } -> Option A
  5. Quick Check:

    Conditional script in failure block = post { failure { script { if (currentBuild.rawBuild.getLog(100).any { it.contains('TEST-FAILURE') }) { emailext to: 'qa@example.com', subject: 'Test Failure', body: 'Tests failed!' } } } } [OK]
Hint: Use script block to check logs inside failure post section [OK]
Common Mistakes:
  • Sending emails on all failures without checking cause
  • Using 'always' or 'success' blocks incorrectly
  • Not using script block for conditional logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard