Variable's environment scope validation isn't enforced through the Octopus Client

I am using the Octopus Client (version 4.15.5) to copy variables in a variable set between a dev and production Octopus (version 3.11.4) server. I ran into an issue when I copied over a variable that was scoped to an environment whose ID is different between the environments. The variable copied over fine (no warning/error messages), but the variable didn’t work as expected. Upon further inspection I found that the Variable Set screen showed the variable as unscoped and the Variable Scope screen showed a blank space where the environment’s name usually shows, but these seem to be placeholders for a environment that has a different ID on my production server.

Here’s a snippet of the C# code that I’m using.

var devLibraryVariableSet = devRepository.LibraryVariableSets.FindAll().First(t => t.Name == "My.VariableSet");
var devVariableSetLink = devLibraryVariableSet.Links.First(t => t.Key == "Variables");
var devVariableSetResource = devRepository.VariableSets.Get(devVariableSetLink.Value);
var devVariables = devVariableSetResource.Variables.Where(v => v.Name.Contains("My.Variable"));

var prodLibraryVariableSet = prodRepository.LibraryVariableSets.FindAll().First(t => t.Name == "My.VariableSet");
var prodVariableSetLink = prodLibraryVariableSet.Links.First(t => t.Key == "Variables");
var prodVariableSetResource = prodRepository.VariableSets.Get(prodVariableSetLink.Value);
var prodVariables = prodVariableSetResource.Variables;

foreach (var variable in devVariables)
{
	//if it doesn't already exist
	if(!prodVariables.Any(v => v.Id == variable.Id))
	{
		prodVariables.Add(variable);
	}
}

prodRepository.VariableSets.Modify(prodVariableSetResource);

I have a workaround, which is to adjust my copy logic to lookup the environment’s ID based on name; however, I expect that this is a bug that you would like to know about and possibly fix.

Attached are the screenshots of the screens.

VariableScope.png

VariableSet-Environments.png

Hi,

Thanks for getting in touch. It certainly does look like you’ve come across a bug there, in that the modify isn’t validating the variables scopes. I’ve raised an issue related to that, which you can track on GitHub.

That being said, there is no assurance at all that the ids related to the scopes will align between the servers, so you should definitely be looking them up by name to map them across. We’ve actually been talking about this within the team recently, in relation to the Remote Release Promotions RFC, and we expect it will be using names rather than Ids in the bundles for exactly this reason.

Hope that helps and if I can assist further just let me know.

Regards
Shannon