I had a request earlier to include in our provisioning process the programic way to create variable sets via the octopus API. Our database team, as part of the database creation process, would put in the connection information into fresh variable set that would be created by that process.
I have used get commands in the past but am not sure how a put will work. As per this topic, I would have to get the current list of variable sets and add a new one
Are there any examples available for that power the rest API?
I have put together a PowerShell script that you can use a reference for creating a new variable set below.
Add-Type -Path 'C:\Program Files\Octopus Deploy\Octopus\Octopus.Client.dll'
$baseUri = "http://url.to.octopus"
$apiKey = "API-xxxxxxxxxxxxxxxxxxxxxxxxx"
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $baseUri, $apiKey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint
$newVariable = New-Object Octopus.Client.Model.VariableResource
$newVariable.Name = "ApiTest"
$newVariable.Value = "Test"
$newVariable.Scope.Add([Octopus.Platform.Model.ScopeField]::Environment, (New-Object Octopus.Platform.Model.ScopeValue("Environments-1")))
$newVariable.Scope.Add([Octopus.Platform.Model.ScopeField]::Role, (New-Object Octopus.Platform.Model.ScopeValue("web-server", "app-server")))
$variableSet = New-Object Octopus.Client.Model.VariableSetResource
$variableSet | ConvertTo-Json
You can also create a .NET console application and use the Octopus.Client NuGet package if you prefer that instead, the process will be the same as above but in C#/VB.NET of course.
Hope that helps!
This does help quite a bit. I wasn’t aware I could pull the client from either the tentacle or the server. I was under the impression the rest client needed to be used.
A couple questions though, first I am getting an error saying that the resource requested isn’t found for the $repository.VariableSets.Create line. I have listed my server name as just Http://servername/. Do I need to specificy that I want the path to go to something like http://servername/api/variablesets? When I examine each variable, I am able to pull in the methods and use those. The $newVaraible and $varaibleSet also have items listed in it.
the second question is on the topic of the rest service itself rather than using the client, how do I go about creating a new variable? I can pull back the informaiton from the variable set, create a “set” that is a copy of the first item in the variable set collection, modify the name and value, then add it using the add method .I don’t think this is correct since I’m not sure what the ids, and links should be. Also does this need to be converted to json before the PUT command to update the existing sets with a new one?
That’ll teach me for not running the script before sending it…sorry about that!
I have fixed the PS script using the
Octopus.Client.dll to interact with the Rest API, I have also created a script that uses the Rest API directly from PowerShell by calling the
I have attached both these scripts for you, hope this helps you achieve success!
AddVariableSetWithOctopusClient.ps1 (2 KB)
AddVariableSetWithRestApi.ps1 (2 KB)
Thank you for both of those. They work beatifully. I found the issue I was having to with the one on my machine. I’ll modify these slightly to make them work within our environment. Thank you so much for your assistance.
Good to hear that you got it all working in the end!
Thank you and warm regards,