Migrate global Library VariableSet from one Octopus instance to another?

Hi All,

I did look at code samples (PowerShell, C#, linq), but could not resolve my confusion around Exporting and Importing LibraryVariableSets. I have tried both PowerShell and C# mainly with Octopus Client. I have successfully Exported all LibraryVariableSets to json files, but they come out without the actual Variables just the LibraryVariableSet defintion.

I guess I am struggling with the object model and the difference between LibraryVariableSet vs Variable Set.
I’ll appreciate if you could you please point me to a code sample or simply specify a few lines of code in particular on how to:

  • create new or update existing LibraryVariableSet (no need for serialisation I am simply reading them from source OD instance and importing/updating in another OD instance)
  • create or update the variables within a LibraryVariableSet - haven’t figured how to bring those along with the LibraryVariableSet object

What I am trying to achieve is when importing, if a library variable set with the same name already exists, the variables should be merged. If a variable in the export doesn’t exist on the destination, it will be created. If a variable with the same name and scopes already exists, the variable on the destination will be left untouched. Unfortunately, I can not use project export with dependent Library VariableSets because of this error?

Thank you for your help!

Emil

Hi Emil,

Thanks for getting in touch! I’m just messaging in here quickly to let you know I’ve responded to your other related question in the other thread, and we can continue digging into the solution there. :slight_smile:

One thing I forgot to address is your mention that your export of the variable set variables come out with just the LibraryVariableSet definition, instead of the expected variables. I think the same thing happened in my testing when I attempted to hit libraryvariablesets/LibraryVariableSets-ID endpoint instead of variables/variableset-LibraryVariableSets-ID endpoint.

Hope that helps!

Best regards,

Kenny

Hi @Kenneth_Bates,

I appreciate your prompt response. After some headbanging combined with trial and error I did come-up with the code to migrate LibraryVariableSets from one instance to another and I am posting it here for future reference. I noticed I can not push a new branch to open a PR to get it added to the LINQPad Octopus.Client samples here. However anyone can use LINQPad5 to run the following code:

// Following code will migrate all LibraryVariableSets from "Default space of 
// source Octopus instance to a dedicated space name on a target Octopus instance
class Program
{
	static void Main(string[] args)
	{
		// Create connection to the Octopus Deploy instance used as a source
		var octopusURL = Environment.GetEnvironmentVariable("OCTOPUS_URL_SRC");
		var apiKey = Environment.GetEnvironmentVariable("OCTOPUS_APIKEY_SRC");
		var octopusEndpoint = new OctopusServerEndpoint(octopusURL, apiKey);
		var repositorySrc = new OctopusRepository(octopusEndpoint);

		// Create connection to the target Octopus Deploy instance
		var targetOctopusURL = Environment.GetEnvironmentVariable("OCTOPUS_URL_TRG");
		var targetApiKey = Environment.GetEnvironmentVariable("OCTOPUS_APIKEY_TRG");
		var targetSpaceName = "TestSpace";
		var targetOctopusEndpoint = new OctopusServerEndpoint(targetOctopusURL, targetApiKey);
		var targetRepository = new OctopusRepository(targetOctopusEndpoint);
		var targetSpace = targetRepository.Spaces.FindByName(targetSpaceName);
		var targetClient = new OctopusClient(targetOctopusEndpoint);
		// Set Space context for the target repository
		var targetRepoForSpace = targetClient.ForSpace(targetSpace);

		// Get list of All Library variable sets we want to import into the target Octopus instance
		var sourceLibraryVariableSets = repositorySrc.LibraryVariableSets.FindAll();

		if (sourceLibraryVariableSets.Count > 0)
		{
			// For each LibraryVariableSet in the source Octopus instance
			foreach (var variableSet in sourceLibraryVariableSets)
			{
				// print current source LibraryVariableSet name and Description
				Console.WriteLine($"VariableSet: '{variableSet.Name}', '{variableSet.Description}'");

				// Import current library variable set into the target Octopus instance
				var targetVariableSet = targetRepoForSpace.LibraryVariableSets.FindByName(variableSet.Name);
				if (targetVariableSet == null)
				{
					// Create a new variable set if it doesn't exist in the target space
					var newVariableSet = new LibraryVariableSetResource
					{
						Name = variableSet.Name,
						Description = variableSet.Description,
						ContentType = variableSet.ContentType,
						SpaceId = targetSpace.Id
					};
					targetRepoForSpace.LibraryVariableSets.Create(newVariableSet);
				}
				else
				{
					// Merge/update the variables if the variable set already exists in the target space
					var sourceVariables = repositorySrc.VariableSets.Get(variableSet.VariableSetId);
					var targetVariables = targetRepoForSpace.VariableSets.Get(targetVariableSet.VariableSetId);

					foreach (var sourceVar in sourceVariables.Variables)
					{
						// Only import unscoped variable values
						if (sourceVar.Scope.Count == 0 ) //.IsEmpty)
						{
							var targetVar = targetVariables.Variables.FirstOrDefault(v => v.Name == sourceVar.Name);
							if (targetVar == null)
							{
								// Add the new variable if it doesn't exist in the target variable set
								targetVariables.Variables.Add(sourceVar);
							}
							else
							{
								// Update the variable value if it exists in the target variable set
								targetVar.Value = sourceVar.Value;
							}
						}
						else
						{
							Console.WriteLine($"`t{sourceVar.Name}: skipping value '{sourceVar.Value}', because it is scoped '{sourceVar.Scope}'");
						}
					}

					targetRepoForSpace.VariableSets.Modify(targetVariables);
				}
			}

			Console.WriteLine($"Finished importing LibraryVariableSets and their variable values from '{octopusURL}' to '{targetOctopusURL}'");
		}
		else
		{
			Console.WriteLine($"No LibraryVariableSets found in '{octopusURL}'");
		}
	}
}

I am happy for you to close this thread.

Thanks,
Emil

1 Like

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