is there a way to get the variables for a release and environment over the API?
I want to get the variables from the TEST environment and from the PRODUCTION environment and diff them.
This way I want to find variables that are expected to be set per environment, but have not been.
Using the raw api, first find the project in https://demo.octopusdeploy.com/api/projects and find the Variables element under Links. That will give you the url of the variable set.
Alternatively open the dev tools in the browser and capture the request when you open the variables page in the web UI.
Iâm not sure what you mean âper environmentâ, could you expand on what you mean by that? The returned variables should contain the scope of each variable, so you can filter out those variables that apply to other environments.
The Scope property contains an Environment property with an array of environments that a variable applies to. If the Environment property does not exist, it applies to all environments.
The following script will find the names of variables that are scoped to Test, and all the names of variables scoped to Prod. It then finds those in the test set, but not prod set. Also included is a list of all variables scoped to Prod or all environments that may fit what you require better.
var project = repository.Projects.FindByName("My Project");
var set = repository.VariableSets.Get(project.VariableSetId);
var testEnvironment = set.ScopeValues.Environments.First(e => e.Name == "Test").Id;
var prodEnvironment = set.ScopeValues.Environments.First(e => e.Name == "Prod").Id;
var testScopedVariables = set.Variables
.Where(v => v.Scope.ContainsKey(ScopeField.Environment) && v.Scope[ScopeField.Environment].Any(e => e == testEnvironment))
.Select(v => v.Name);
var prodScopedVariables = set.Variables
.Where(v => v.Scope.ContainsKey(ScopeField.Environment) && v.Scope[ScopeField.Environment].Any(e => e == prodEnvironment))
.Select(v => v.Name);
var prodAndUnscopedVariables = set.Variables
.Where(v => !v.Scope.ContainsKey(ScopeField.Environment) || v.Scope[ScopeField.Environment].Any(e => e == prodEnvironment))
.Select(v => v.Name);
var unset = testScopedVariables.Except(prodScopedVariables, StringComparer.InvariantCultureIgnoreCase);
I didnât have time to wait for an answer so I found out most of what you sent via brute force. This wouldâve saved me a good deal of time (not your fault on the timing). This isnât the first time some api examples have been created for your forum questions that would be useful for others. Why arenât yâall adding these to your documentation. A simple copy/paste of this to an âexamplesâ section would be a big help for all of us banging away at the api. I love octopus and the api â once you can find the right bits â has really helped us automate our environment creation/deployment; but the api is an absolute nightmare to navigate and much of it makes little sense â even after you find out how to use it.
Thank you for the feedback. We do have a API documentation and samples site that covers most of the API, but I do see that Variables are not properly documented. We are planning on creating better API documentation and discover-ability this year.