Create or Update Octopus Variables Through The API

Hello!

As I have mentioned many times before, I have conflicting opinions when it comes to Octopus Deploy. On the one hand I think it does things that are a bit lousy, like variables only being strings for PowerShell (so it is not possible to include HashTables scoped easily). And whilst we’re on the subject of variables, the page to enter/edit variables is a tedious process. Firstly it has an autocomplete feature for the scoping, which is fine for tiny installs but is painfully slow on larger servers. Secondly the option to delete or undo changes is not immediately apparent. Thirdly there’s a bug where you have to click off a field and then back on it to make it editable. All this makes for an experience that no one enjoys if you have many variables to add/update.

But, focusing on the positives, Octopus has a fantastic API. Basically you can get away with creating a couple of functions as helpers to interact with it. The documentation is clear, and it’s possible to do many things with the API, mainly because everything Octopus does is through it’s own API. And so it is possible to add/edit variables this way.

And so, referring back to my mantra of automate something when it gets tedious I have written a script that will add/edit variables for a given Octopus Project. The variables have to be in a certain csv format, and currently the only level of scoping supported is environment. But what I wanted to do is get the script out here today, and then create a Module at a later date with some extra functionality (like more scoping options.)

What it doesn’t so is delete variables, so there’s no risk of losing work that isn’t saved.

Going forward I’d love to have all the variables for a project in a csv file in source control, then before an Octopus Project is created it updates the variables. But that’s some way away.

You will need NuGet installed already, and ability to connect to Nuget to download some dlls. But these are available via installing the Tentacle service from Octopus.

As with every script off the internet: test, test, and test again!

Leave a Reply

Your email address will not be published. Required fields are marked *