Send Email on Deployment Failure

What we want to do is send a email to the development team whenever a deployment fails. I put the following script together that runs as the last step and what is does in checks the variable $OctopusParameters[‘Octopus.Deployment.Error’] and if it has a value sends the email. The problem I’m having is getting the error detail into the email. I thought that this variable would contain that detail but its always null as far as I can tell. $OctopusParameters[‘Octopus.Deployment.ErrorDetail’]. Is there some other way I should go about getting the error details for the step that failed? Thanks for the help!

$recipients = $OctopusParameters[‘recipients’]
$subject = $OctopusParameters[‘Octopus.Project.Name’] + " Failed on: " + $OctopusParameters[‘Octopus.Environment.Name’]

$deploymentError = $OctopusParameters[‘Octopus.Deployment.Error’]

If ($deploymentError){

$emailBody = $OctopusParameters['Octopus.Deployment.ErrorDetail']

$pwd = ConvertTo-SecureString $smtpPassword -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential fusiontech,$pwd

send-mailmessage -from “Jane Doe” -to $recipients -subject $subject -body $deploymentError -priority High -smtpServer “smtp” -Credential $cred -Port “8181”


Hi Matt,

Thanks for getting in touch! We have error and errordetails per step as in the following sample:

#{each step in Octopus.Step}
  #{if step.Status.Code}
    <li>#{step | HtmlEscape} &mdash; <strong>#{step.Status.Code}</strong>
    #{if step.Status.Error}
      <pre>#{step.Status.Error | HtmlEscape}</pre>
      <pre>#{step.Status.ErrorDetail | HtmlEscape}</pre>

This would be the best method to display what you want, as it will record against each step.

Hope that helps!

Hi Vanessa,

Thanks for the reply! I’m sorry but I’m not following this, would the code you referenced above be placed in the email step template? I don’t see that working for us, since we want a single step that runs at the very end and only sends an email if there is an error. I believe that we have to do this via a PowerShell script which is why we put one together. Can this code be used in that PS script that I posted in my original message.

Hi Matt,

You can create an email step that only runs on deployment failure, then use the existing conventions like the code I pasted to show what step failed and its error.
No reason to re-write something that already exists?