Wednesday, June 15, 2022

Powershell snippets for Octopus Deploy

Extract url to triggering build from an Octopus Deploy release

Our build pipelines create releases in Octopus Deploy and send along the commit information. It is possible to find the buildid and url to the build from the Octopus.Release.Notes variable.


if ($OctopusParameters["Octopus.Release.Notes"] -match "Build \[(.+)\]\((?<url>.+)\)") 

{

   $buildUrl = $Matches.url

   $buildId=($buildUrl -split "=")[-1]

} else {

   $buildUrl="Build info missing. Manually created release?"

   $buildId ="Unkown"

}


Octopus.Deployment.Changes contains change information and a markdown version of that information is in  Octopus.Deployment.ChangesMarkdown.


Parameter validation in step templates

Validate parameters in step template and create variables from the parameters.Inside the foreach, the first line checks 'required' parameters have a value.
The second if statetment checks for '#{' in the parameter value.
The last line creates a variable with the name of the parameter and sets the value.
{
    # Check for required variables
    if ( @("pShareName","pPhysicalPath") -contains $pName) { if (!$OctopusParameters[$pName]) {Write-Warning "Parameter $pName cannot be empty!"; exit 1}}
    # Check for #{ in any variables
    if ($OctopusParameters[$pName] -and $OctopusParameters[$pName].indexOf("#{") -ne -1) { Write-Warning $("Parameter {0} contains '{1}'! Check variable exists and is scoped properly." -f $pName,$OctopusParameters[$pName]); exit 1 }
    # set a local variable
    Set-Variable -Name $pName -Value $OctopusParameters[$pName]
}