I want to get email notified at the start of the build and again notified when the job is completed (Stable/Unstable)

Currently, the email plugin works for failure notifications, but I need it to send alerts for all build status (success, unstable, and failure).
We want to get email notified at the start of the build and again notified when the job is completed for both Stable/Unstable.
Can this be achieve through email-ext plugin, also please advise other options to achieve this requirement.

Hello and welcome to this community, @karthikpb88. :wave:

As far as I know, you can achieve this using the email-ext plugin in Jenkins.
You should be able to configure the plugin to send emails for all build statuses (success, unstable, and failure) and also send notifications at the start and end of the build.

Here is a configuration for the email-ext plugin in a Jenkins pipeline script:

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                script {
                    // Send email at the start of the build
                    emailext(
                        subject: "Build Started: ${env.JOB_NAME} #${env.BUILD_NUMBER}",
                        body: "The build has started.",
                        recipientProviders: [[$class: 'DevelopersRecipientProvider']]
                    )
                }
                // Your build steps here
            }
        }
    }

    post {
        always {
            script {
                // Send email at the end of the build
                emailext(
                    subject: "Build ${currentBuild.currentResult}: ${env.JOB_NAME} #${env.BUILD_NUMBER}",
                    body: "The build has completed with status: ${currentBuild.currentResult}.",
                    recipientProviders: [[$class: 'DevelopersRecipientProvider']]
                )
            }
        }
    }
}

In this script:

  • An email is sent at the start of the build using the emailext step.
  • At the end of the build, another email is sent in the post section, which runs regardless of the build result.
  • The recipientProviders field is set to DevelopersRecipientProvider, which sends the email to the developers of the project. You can customize the recipients as needed. I tested on a fresh installation of Jenkins, and got nothing of course.
Started by user [admin](https://8080-jenkinsdocs-quickstartt-ih1vxjtgrm4.ws-us117.gitpod.io/user/admin)
[Pipeline] Start of Pipeline
[Pipeline] node Running on [docker-ssh-jenkins-agent](https://8080-jenkinsdocs-quickstartt-ih1vxjtgrm4.ws-us117.gitpod.io/computer/docker%2Dssh%2Djenkins%2Dagent/) in /home/jenkins/agent/workspace/sdfdsfds
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] script
[Pipeline] { 
[Pipeline] emailext An attempt to send an e-mail to empty list of recipients, ignored. [Pipeline] } 
[Pipeline] // script 
[Pipeline] } 
[Pipeline] // stage 
[Pipeline] stage 
[Pipeline] { (Declarative: Post Actions) 
[Pipeline] script 
[Pipeline] { 
[Pipeline] emailext An attempt to send an e-mail to empty list of recipients, ignored. [Pipeline] } 
[Pipeline] // script 
[Pipeline] } 
[Pipeline] // stage 
[Pipeline] } 
[Pipeline] // node 
[Pipeline] End of Pipeline Finished: SUCCESS

This configuration ensures that you receive email notifications at the start and end of the build for all build statuses.

Hello there!
Currently, we are using ‘Jenkins as a Service’ and we are freestyle job.
I want the email notification for the below configuration:

- job:
    name: 'Project_name'
    project-type: freestyle
    node: node1
    scm:
      - git:
          url: git@github.com
          credentials-id: xxxxxx
          branches:
            - master
    concurrent: false
    disabled: false
    parameters:
      - choice:
          name: Project_services
          choices:
            - 'Project services in all dev nodes'
    wrappers:
      - vault-secrets:
          vault-url: 'https://vault.com'
          credentials-id: 'xxx'
          fail-if-not-found: true
          skip-ssl-verification: false
          engine-version: '2'
          timeout: 60
          secrets:
            - secret-path: ‘xxxx'
              engine-version: '2'
              secret-values:
                - env-var: ‘xxxx’
                  vault-key: ‘xxxxx’
    builders:
      - shell: |
         sh xxxxx.sh
    triggers:
      - timed: 'H 22 * * 4'
    publishers:
      - email:
          recipients: 'karthikpb88@gmail.com'
          send-to-individuals: true
          notify-every-unstable-build: true
      - trigger:
          project: Health_check
          threshold: SUCCESS

Got it.
Do you have a support line, or is there assistance available through your vendor?

No support line available, I am testing through my test environment.

I’m not sure if it is possible to send emails in freestyle jobs at the start of the build. The email and email-ext only have publishers so they can only run as post-build actions.
While it is possible with flexible-publish and any-buildstep plugins to run regular build steps in post-build actions, the reverse is not possible to my knowledge.

The only way would be to use a system groovy script as the first step and in that script somehow send the email (maybe by directly calling classes from email-ext). But that is a rather ugly way I would say.
Or run a shell step to send a mail via command line (also not nice when you have windows and linux machines)

1 Like

There is a Pre-Build trigger that you can add to the set of triggers in the free style job. It should run before the build and allow you to send emails.

1 Like

There is the pre-scm build step plugin, you could add there a call to another project that does nothing but send an email