1

I have several Jenkins jobs where I want an email to be triggered (or not trigger) based on an input parameter.

The use case is that I have one deploy job per project, but that job is parametrized for each environment. We you run the job you can select which environment to deploy to (Dev1, Dev2, QA, etc). Ideally I would like to send out notification emails when a new build is sent to QA, but I do not want to send out notification emails when a new build is sent to Dev, because that happens all the time (with every developer commit) and would flood email boxes.

I've tried googling but haven't yet found a solution. I am currently using the email-ext plugin.

Thanks.

Mike Pennington
  • 676
  • 10
  • 26

2 Answers2

1

It is a very nasty way to solve the problem, but if you cannot do it by any other means...

Create a dependent job that exists primarily to pass / fail based on the value of the parameter, which would get passed as a parameter to the job. Then you could chain the decision the job made to the email notification.

Of course, if you were going to do this, it would probably be better to just write a small parametrized email sender using javax.mail and have Jenkins run it by "building" an ANT project that actually calls the "java" task. Then you have your own email sender (full control) and could make it a dependent job on the other tasks.

I hope someone chimes in with a simpler way...

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

In email-ext you can add a "Script - Before Build" or a "Script - After Build" trigger which will trigger on whether the specified groovy script returns true or false. The help for the script reads:

Use this area to implement your own trigger logic in groovy. The last line will be evaluated as a boolean to determine if the trigger should cause an email to be sent or now.

They don't give many details of what's available in the script, but from the source code on Github, it looks like you have "build" and "project" at least, and some common imports done for you:

    cc.addCompilationCustomizers(new ImportCustomizer().addStarImports(
            "jenkins",
            "jenkins.model",
            "hudson",
            "hudson.model"));

    Binding binding = new Binding();
    binding.setVariable("build", build);
    binding.setVariable("project", build.getParent());
    binding.setVariable("rooturl", JenkinsLocationConfiguration.get().getUrl());
    binding.setVariable("out", listener.getLogger());

Caveat, I haven't tried this, but this should work as an example script:

build.buildVariables.get("MY_DEPLOYMENT_ENV") == "QA"

that should get the value of a String or Choice parameter you've created called "MY_DEPLOYMENT_ENV" and trigger the email if the current value is "QA"

Sadik Ali
  • 1,129
  • 10
  • 26
Rhubarb
  • 34,705
  • 2
  • 49
  • 38