Is there a way to bulk-load library variable sets?

I’m looking for a method to bulk load variable sets. I’m looking at the potential to load 60-90 variables into a set and doing this by hand doesn’t look to be an enjoyable task. I don’t see a POST method in the API documentation for creating new variables in an existing LibraryVariableSet, which is really what I’m looking for. If there were then I could just Import-CSV and Foreach-Object through things in Powershell to load the variables.

I am looking for the same solution. I only find info on updating and not creating variables.

Hi Gary,

The variable set contains a list of variables and is edited as a whole - i.e., you GET the variable set, add 50 variables to the Variables collection inside the resource, and PUT it back.

This can be done easily with Octopus.Client.

Hope that helps,

Paul

Here is an example:

Paul

I think I can work from that. Any plans to ‘wrap’ Octopus.Client in compiled Powershell Cmdlets?

Here’s the start of a compiled Cmdlet that lets you add variables to VariableSets. Works well enough for me to bulk-load the majority of what I need to do.

[Cmdlet(VerbsCommon.Add, "ODVariable")]
    public class AddODVariable : PSCmdlet
    {
        [Parameter(Mandatory = true)]
        public string Id { get; set; }

        [Parameter(Mandatory = false, ValueFromPipeline = true)]
        public VariableResource Variable { get; set; }

        [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
        public string Name { get; set; }

        [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
        public string Value { get; set; }

        [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
        public SwitchParameter IsSensitive { get; set; }

        [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
        public bool? IsEditable { get; set; }

        private OctopusServerEndpoint octopusServer;
        private OctopusRepository octopusRepo;
        private VariableSetResource variableSet;

        protected override void BeginProcessing()
        {
            WriteDebug(String.Format("Connecting to Octopus Server {0}", Environment.GetEnvironmentVariable("OctopusServer")));
            octopusServer = new OctopusServerEndpoint(Environment.GetEnvironmentVariable("OctopusServer"), Environment.GetEnvironmentVariable("OctopusKey"));
            octopusRepo = new OctopusRepository(octopusServer);
            WriteDebug("Getting VariableSet");
            variableSet = octopusRepo.VariableSets.Get(Id);
        }

        protected override void ProcessRecord()
        {
            VariableResource internalVar = Variable;

            try
            {
                if (internalVar == null)
                {
                    internalVar = new VariableResource();

                    internalVar.Name = Name;

                    if (!String.IsNullOrEmpty(Value))
                    {
                        internalVar.Value = Value;
                    }

                    if (IsSensitive.IsPresent)
                    {
                        internalVar.IsSensitive = true;
                    }

                    if (IsEditable == false)
                    {
                        internalVar.IsEditable = false;
                    }
                    else
                    {
                        internalVar.IsEditable = true;
                    }

                }

                if (variableSet.Variables.Count != 0)
                {
                    if (variableSet.Variables.FirstOrDefault(n => n.Name == internalVar.Name) == null)
                    {
                        WriteVerbose(String.Format("Adding Variable {0} to VariableSet {1}", internalVar.Name, Id));
                        variableSet.Variables.Add(internalVar);
                    }
                    else
                    {
                        WriteWarning(String.Format("Variable {0} already exists in VariableSet {1}", internalVar.Name, Id));
                    }
                }
                else
                {
                    WriteVerbose(String.Format("Adding Variable {0} to VariableSet {1}", internalVar.Name, Id));
                    variableSet.Variables.Add(internalVar);
                }

            }
            catch (Exception ex)
            {
                WriteDebug(ex.StackTrace);
                WriteError(new ErrorRecord(ex, "0001", ErrorCategory.ResourceUnavailable, null));
                WriteError(new ErrorRecord(ex.InnerException, "0002", ErrorCategory.ResourceUnavailable, null));
            }
        }

        protected override void EndProcessing()
        {
            octopusRepo.VariableSets.Modify(variableSet);
            WriteObject(variableSet);
        }

        protected override void StopProcessing()
        {

        }
    }
1 Like

Hi.

Does octopus have a plan to build bulk variable update feature in the UI?

Create a script/code is one way to workaround this. But I would like to see a more convenient way to bulk update variables.
I would like to be able to pass the role of managing variables to Tester and environment/deployment manager.

Maybe a variable set export and import feature?

HI Edwin,

No plans currently, but it sounds like a logical addition. We’re managing the backlog of features on https://octopusdeploy.uservoice.com currently - you may already find this there but if not feel free to pop it into the list.

Regards,
Nick

Hello,

I have created a github project to implement these features as cmdlets. It can be found here:

You can Get-OctoVariable -Project <string> | Export-Csv to bulk export the variables from a project. With a bit more PowerShell you can do an Import-Csv and eventually an Add-OctoVariable or Update-OctoVariable with some formatting code in the middle. At present you can only get LibraryVariableSets, but adding and modifying them is on the list of things to do.

Bug reports and suggestions would be appreciated.

Colin