Retrieving current Step Template version using the API

Hi -

I’m working on a report that requires retrieving the current version number of a step template. Here’s the code:

$name = ‘MyTemplate’
$octURI = ‘http://my_octopus.com’
$templates = Invoke-RestMethod -Uri $( $octURI + ‘/api/Spaces-1/actiontemplates/search’ ) -ContentType application/json -Headers $oBaseURIHdr -Method Get
$template = $templates | ? {$_.Name -eq $name}
$templateUsage = Invoke-RestMethod -Uri $( $octURI + ‘/api/Spaces-1/actiontemplates/’ + $template.Id + ‘/usage’ ) -ContentType application/json -Headers $oBaseURIHdr -Method Get

I can’t seem to find the version number in $template or $templateUsage (which seems odd). I would think it would be a property of the the ActionTemplate.

Any suggestions about how I can get the current template version would be appreciated.

Thanks,

Shannon

Hi Shannon,

Thanks for getting in touch! I’m sorry for the delay in getting back to you here. I’m wondering if you are able to use the Octopus.Client to perform your calls, or if you are required to use REST directly as in your example.

If you were to use the Octopus.Client, this version value is available under the ActionTemplate resource. An example below from my test VM.

cd 'C:\My Scripts'
Add-Type -Path 'C:\My Scripts\Octopus.Client.dll'

$apikey = 'API-KEY' # Get this from your profile
$octopusURI = 'http://OctopusServer/' # Your server address

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey 
$repository = New-Object Octopus.Client.OctopusRepository $endpoint

$repository.ActionTemplates.FindByName("TemplateScript")

This one line to retrieve the ActionTemplate by name returned the following:

Name                      : TemplateScript
Description               : 
ActionType                : Octopus.Script
Version                   : 1
CommunityActionTemplateId : 
Properties                : {[Octopus.Action.Script.ScriptSource, Octopus.Client.Model.PropertyValueResource], [Octopus.Action.Script.Syntax, Octopus.Client.Model.PropertyValueResource], 
                            [Octopus.Action.Script.ScriptBody, Octopus.Client.Model.PropertyValueResource]}
Packages                  : {}
Parameters                : {}
SpaceId                   : Spaces-1
Id                        : ActionTemplates-1
LastModifiedOn            : 
LastModifiedBy            : 
Links                     : {[Self, /api/Spaces-1/actiontemplates/ActionTemplates-1], [Usage, /api/Spaces-1/actiontemplates/ActionTemplates-1/usage], [Logo, 
                            /api/Spaces-1/actiontemplates/ActionTemplates-1/logo?cb=2019.12.4], [ActionsUpdate, /api/Spaces-1/actiontemplates/ActionTemplates-1/actionsUpdate]...}

You can see there is a Version property in the top level and you can even append .Verion to the initial request:

$repository.ActionTemplates.FindByName("TemplateScript").Version

This will simply return 1 for my example.

Whilst Octopus is built as an API first program, we strongly recommend using the Octopus.Client where possible as it’s been designed to make these sort of calls much easier to work with.

If you have any questions at all here, or run into any issues, please don’t hesitate to let me know.

Best regards,
Daniel

Awesome. Extremely helpful. Thanks Daniel!

1 Like

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