Create variable with value of Certificate type via API

(Soe141 2014) #1

Hi, is it possible to create or modify a variable with value of Certificate type via API? I found a script here: https://github.com/OctopusDeploy/OctopusDeploy-Api/blob/master/Octopus.Client/PowerShell/Variables/UpdateVariableInProject.ps1 which updates a variable in a project. But I need to specify value to be of Certificate type. Can you please help me with this?

(Michael Richardson) #3

Certainly.

Something like the following:

Add-Type -Path "Octopus.Client.dll" 

$octopusUrl = "http://your-octopus-server" 
$userApiKey = "API-YOURAPIKEY" 
$certificateName = "Acme HTTPS - Production" 
$projectName = "Acme Online" 
$variableName = "HTTPS Certificate" 

$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusUrl,$userApiKey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint

$certificate = $repository.Certificates.FindByName($certificateName)

$project = $repository.Projects.FindByName($projectName)
$variableset = $repository.VariableSets.Get($project.links.variables)

$variable = new-object Octopus.Client.Model.VariableResource
$variable.Type = [Octopus.Client.Model.VariableType]::Certificate 
$variable.Name = $variableName
$variable.Value = $certificate.Id
$variableset.Variables.Add($variable)
$repository.VariableSets.Modify($variableset)

Let me know if that doesn’t point you in the right direction.