Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Jenkins Pipeline and choose triggers for email notifications?

Before Pipeline, I used Email-ext plugin to define triggers for sending emails (on first failure, and when back to success).

But in the Pipeline version, I can't control the triggers, I can only "send always", or send based on current build result.

How do I send emails in pipeline based on more general triggers? (first failure, first success, etc)

Thanks!

like image 294
Oren Shpigel Avatar asked Nov 07 '22 08:11

Oren Shpigel


1 Answers

While setting up jenkins pipeline, you have a Pipeline Syntax link below the Groovy Script Editor like in the picture below.

enter image description here

Clicking on the link takes you to snippet generator like in the picture below. enter image description here

Here I've selected emailext : Extended plugin option. You can select simple mail option from the dropdown as well and generate the snippet.

To determine the build status of the current job Jenkins provides the environmental variables which you can use while writing your groovy script. You can find the list of global environment variables by clicking the link provided at the end of Snippet Generator page.enter image description here

Below is the list of properties of currentBuild environment variable for your reference.


    The currentBuild variable may be used to refer to the currently running build.
    It has the following readable properties:

number
build number (integer)
result
typically SUCCESS, UNSTABLE, or FAILURE (may be null for an ongoing build)
currentResult
typically SUCCESS, UNSTABLE, or FAILURE. Will never be null.
resultIsBetterOrEqualTo(String)
Compares the current build result to the provided result string (SUCCESS, UNSTABLE, or FAILURE) and returns true if the current build result is better than or equal to the provided result.
resultIsWorseOrEqualTo(String)
Compares the current build result to the provided result string (SUCCESS, UNSTABLE, or FAILURE) and returns true if the current build result is worse than or equal to the provided result.
displayName
normally #123 but sometimes set to, e.g., an SCM commit identifier
description
additional information about the build
id
normally number as a string
timeInMillis
time since the epoch when the build was scheduled
startTimeInMillis
time since the epoch when the build started running
duration
duration of the build in milliseconds
durationString
a human-readable representation of the build duration
previousBuild
another similar object, or null
nextBuild
similarly
absoluteUrl
URL of build index page
buildVariables
for a non-Pipeline downstream build, offers access to a map of defined build variables; for a Pipeline downstream build, any variables set globally on env
changeSets
a list of changesets coming from distinct SCM checkouts; each has a kind and is a list of commits; each commit has a commitId, timestamp, msg, author, and affectedFiles each of which has an editType and path; the value will not generally be Serializable so you may only access it inside a method marked @NonCPS
rawBuild
a hudson.model.Run with further APIs, only for trusted libraries or administrator-approved scripts outside the sandbox; the value will not be Serializable so you may only access it inside a method marked @NonCPS
Additionally, for this build only (but not for other builds), the following properties are writable:

  • result
  • displayName
  • description

Hope this helps. Good luck!

like image 114
warezthief Avatar answered Nov 15 '22 12:11

warezthief