10raw
10raw

Reputation: 574

Jenkins email notification to user from GitHub who pushed a commit in dev branch

I have a situation where all developers are working in a dev Branch and pushing all commits only to that dev Branch. One gatekeeper will look at those changes and merge it to the master branch.

In Jenkins, I have a job that is triggered by a push to the master branch. I would like to set up a post-build action that checks if the build failed, then sends an email to the developer whose push in dev branch caused the build to fail the build, instead of sending the email to the gatekeeper who merged the developer's change to the master branch and triggered the build. How do I do this?

Upvotes: 4

Views: 5848

Answers (1)

Here_2_learn
Here_2_learn

Reputation: 5451

This is the following procedure that we follow and would recommend to use similar to this:

1) Allow merge to Develop branch from PR only

All the developer changes should be pushed to dev branch only through PR-Pull requests. Configure in such a way that minimal checks should be passed for allowing the PR to merge to develop.

This way all the changes which are going to merge in develop branch will be verified and its the duty of the developer to make sure the checks will be cleared for merging and no more headache for gatekeeper.

Adding snapshot for reference.

enter image description here

UPDATE : Git flow strategy

enter image description here

SOURCE : https://nvie.com/posts/a-successful-git-branching-model/?

2) Define Pipelines & Multibranch

If you are already using pipelines and Multibranch plugins as part of your CI/CD the above step would be easy. If not these are the ideal plugins that should be part of CI/CD.

Some references here :

https://jenkins.io/doc/book/pipeline/

https://wiki.jenkins.io/display/JENKINS/Pipeline+Multibranch+Plugin

https://jenkins.io/doc/tutorials/build-a-multibranch-pipeline-project/

UPDATE: The Multibranch Pipeline project type enables you to implement different Jenkinsfiles for different branches of the same project. In a Multibranch Pipeline project, Jenkins automatically discovers, manages and executes Pipelines for branches which contain a Jenkinsfile in source control.

Refer here for creating multibranch pipelines: https://jenkins.io/doc/book/pipeline/multibranch/

GitHub Organization Folder enable Jenkins to monitor an entire GitHub Organization and automatically create new Multibranch Pipelines for repositories which contain branches and pull requests containing a Jenkinsfile.

3) Enable emailext

Sample snippet here to which will trigger a notifications for the developers who has committed the code.

emailext(
        subject: "Build ${env.JOB_NAME} - ${currentBuild.displayName} ${currentBuild.result}",
        body: """Build ${currentBuild.result}
            ${env.JOB_URL}
            """,
        recipientProviders: [[$class: 'DevelopersRecipientProvider']]
    )

More options for the variable currentBuild is described here : https://qa.nuxeo.org/jenkins/pipeline-syntax/globals#currentBuild

Emailext plugin : https://wiki.jenkins.io/display/JENKINS/Email-ext+plugin

https://jenkins.io/doc/pipeline/steps/email-ext/

Hope this helps to some extent.

Upvotes: 4

Related Questions