Raj K
Raj K

Reputation: 470

Octopus output variables and accessing them

I've octopus variables under "Library" -> "Variable Sets" variables with names

1. DatabaseExceptions
2. ReportsExceptions
3. ApplicationExceptions

And I've setup of my project "Deployment process" in octopus as steps

1. Database
2. Reports
3. Application
4. Send Email

The above variable are set with exceptions by using 'PowerShell' scripts that are invoked using predeploy.ps1 of the particular step on exceptions in scripts.

At the last step(Send Email) I was trying to send email with the information in exception variables by checking if particular variable has information in it.

I'm doing this in the send Email step body by checking the check box("Body as HTML")

<h2>Deployment Steps</h2>
<ol>
    #{each step in Octopus.Step}
        #{if step.Status.Code}
            <li>#{step | HtmlEscape} &mdash;
                #{if Step.Name == "Database"}
                    #{if Octopus.Action[Database].Output.DatabaseExceptions}
                        <pre>#{Octopus.Action[Database].Output.DatabaseExceptions}</pre>
                    #{/if}
                #{/if}
                #{if Step.Name == "Reports"}
                    #{if Octopus.Action[Reports].Output.ReportsExceptions}
                        <pre>#{Octopus.Action[Reports].Output.ReportsExceptions}</pre>
                    #{/if}
                #{/if}
                #{if Step.Name == "Application"}
                    #{if Octopus.Action[Application].Output.ApplicationExceptions}
                        <pre>#{Octopus.Action[Application].Output.ApplicationExceptions}</pre>
                    #{/if}
                #{/if}
            </li>
        #{/if}
    #{/each}
</ol>

The Database and Reports scripts has errors and my output looks like below which is correct as expected

Deployment Steps
1.  Database — Exception on processing DB scripts - info:...
2.  Reports — Exception on processing Reports - info:...
3.  Application
4.  Send Email

And my Question:

Is it possible to do something as below as my step name and starting of my variable name are same

<h2>Deployment Steps</h2>
<ol>
    #{each step in Octopus.Step}
        #{if step.Status.Code}
            <li>#{step | HtmlEscape} &mdash;
                    #{if Octopus.Action[#{step.Name}].Output.#{step.Name}Exceptions}
                        <pre>#{Octopus.Action[#{step.Name}].Output.#{step.Name}Exceptions}</pre>
                    #{/if}
            </li>
        #{/if}
    #{/each}
</ol>

Upvotes: 0

Views: 3018

Answers (1)

Alex M
Alex M

Reputation: 2548

Unfortunately that doesn't seem to be possible within the email template. See this thread in Octopus support.

Also just to note, you could use the same output variable name in every step required and it would have a different scope for each step, i.e.

Set-OctopusVariable -name "Exceptions" -value $someValue 

And potentially the solution could be to have a separate script step to prepare the exceptions for the email step. Something like:

$exceptions = ""
$OctopusParameters.GetEnumerator() | Where-Object { $_.Key -like '*Output.Exceptions*' } | % { 
  Write-Host " $($_.Key) - $($_.Value)"
  $exceptions += $_.Value + "`r`n"
}

Set-OctopusVariable -name "ExceptionsToEmail" -value $exceptions

And then one could use the variable in the email step without a need to loop through steps:

<pre>
   Exceptions:
    #{Octopus.Action[Prep Email Output].Output.ExceptionsToEmail}
</pre>

Updated solution

As noted above, if one was sharing the same output variable name among steps, then the solution could be simplified, with no need to have a separate script step. All would be done in the email template:

#{each action in Octopus.Action}
    <strong>#{action.Name}</strong>
    #{if action.Output.Exceptions}
        <p> - #{action.Output.Exceptions}</p>
    #{/if}
    #{unless action.Output.Exceptions}
        - Succeeded
    #{/unless}
#{/each}

Which would result in an email output like:

Database - database exception occured
Reports - reports exception occured
Application - some application exception occurred
Some other step - Succeeded 
Send email - Succeeded 

Upvotes: 1

Related Questions