Get branch name in powershell

Is there a consistent way to get the branch name of the package being deployed in a powershell step? The branch name is correctly sent via the build information API and shows up when looking at the package in the library.

I’ve used this:

#{Octopus.Deployment.PackageBuildInformation[0].Branch}

But is the order of data in the build information array guaranteed?

Also is it possible to attach custom metadata to a package?

Hi Vbushong,

I thought I would just touch base to let you know that we are currently investigating this issue, however probably won’t have an answer for you until tomorrow.

The short answer is, no, the array order is not guaranteed. We are looking for a solution at present and will let you know as soon as we have an answer.

Regards,

Dane

The answer to this isn’t as simple as we would like. To remedy that, we will add a Branch property to the package information on the release (I have created an issue). This will allow you to do Octopus.Release.Package[Acme.Web].Branch

In the meantime, the way to get what you want is to iterate over the collection contained in the Octopus.Deployment.Changes variable and find the item with the Version that matches the release version currently being deployed. You then have to iterate over the collection in the BuildInformation property of that object, and find the package you are interested in (e.g. with ID of Acme.Web. If your deployment has only one package, then there will be only one). This object will then have a Branch property.

Hopefully the object structures documented here help with this, but if you have any problems with the script please feel welcome to reach out, and hopefully we can help.

Thanks - it looks like iterating over Octopus.Deployment.Changes seems to work fine. This is what I ended up with:

$version = '#{Octopus.Release.Number}'
$changesJson = '#{Octopus.Deployment.Changes}'
$changes = ConvertFrom-Json $changesJson
$branchName = foreach ($change in $changes)
{
    if ($change.Version -eq $version)
    {
        # Could iterate over build info if needed
    	$change.BuildInformation[0].Branch
    }
}

Nice! I’m glad to hear you got it working.
And thank you for sharing.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.