Jenkins – how to setup build failure email

Getting email of a build failure is a common use case in Jenkins. We would want to get notified whenever any build fails and when it is back to success. Here are steps to setup email on build failure in Jenkins. This tutorial requires Jenkins setup on Ubuntu Linux with a py.test project and publish junit reports post build action.

  1. First create a test python test case which always fails. Create with following content.
    import pytest
    def test_fail1():
      assert 1 == 2, "This is expected to fail"

    If you run py.test you should get output similar to this:

    =============================================================== test session starts ================================================================
    platform linux2 -- Python 2.7.6, pytest-2.8.2, py-1.4.30, pluggy-0.3.1
    rootdir: /path_to_tests/tests, inifile: 
    collected 1 items  F
    ===================================================================== FAILURES =====================================================================
    ____________________________________________________________________ test_fail1 ____________________________________________________________________
        def test_fail1():
    >     assert 1 == 2, "This is expected to fail"
    E     AssertionError: This is expected to fail
    E     assert 1 == 2 AssertionError
  2. Create a test py.test test project and name it test_fail (refer to Jenkins setup tutorial).
  3. In project settings, click post build email notification action as shown below:
  4. Enter one or more emails (separated by space) as shown below and select send email on every unstable build. Click on Save.
  5. Change project build command to:

    py.test --junitxml=result.xml


  6. Setup Jenkins from email if not done already. Go to Manage Jenkins > Configure System. Then in section “Jenkins Location” enter a suitable system admin email. Notifications will come from this email address.
  7. Now run the build for the project manually and you should get a failure email which will look something like:

    Subject: Build failed in Jenkins: test_fail #2
    Started by user anonymous
    Building in workspace 
    Updating at revision '2016-01-20T05:17:12.947 +0000'
    At revision 4801
    no change for since the previous build
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit


Click here to write/view comments