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 test_fail.py with following content.
    #!/usr/bin/python
    import pytest
    
    def test_fail1():
      assert 1 == 2, "This is expected to fail"

    If you run py.test test_fail.py 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 
    
    test_fail.py 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
    
    test_fail.py:5: 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:
    jenkins-post-build-email-notification
  4. Enter one or more emails (separated by space) as shown below and select send email on every unstable build. Click on Save.
    jenkins-email-notification-form
  5. Change project build command to:

    py.test --junitxml=result.xml test_fail.py
    

    jenkins-test-project-settings-build-commant-for-test-fail

  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.
    jenkins-manage-configure-from-email
  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
    See 
    
    ------------------------------------------
    Started by user anonymous
    Building in workspace 
    Updating http://svnserver.com/path_to_tests/tests at revision '2016-01-20T05:17:12.947 +0000'
    At revision 4801
    no change for http://svnserver.com/path_to_tests/tests since the previous build
    ...
    ...
    
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments