-
Notifications
You must be signed in to change notification settings - Fork 127
/
ImportExportUpdateVariables.linq
53 lines (42 loc) · 2 KB
/
ImportExportUpdateVariables.linq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<Query Kind="Statements">
<NuGetReference>Newtonsoft.Json</NuGetReference>
<NuGetReference>Octopus.Client</NuGetReference>
<Namespace>Octopus.Client</Namespace>
<Namespace>Octopus.Client.Model</Namespace>
<Namespace>Octopus.Client.Model.Endpoints</Namespace>
<Namespace>Octopus.Client.Serialization</Namespace>
<Namespace>System.Net</Namespace>
<Namespace>System.Net.Http</Namespace>
<Namespace>Newtonsoft.Json</Namespace>
</Query>
// This script shows how to retrieve, export, import, manipulate and update a project's variables
var endpoint = new OctopusServerEndpoint("http://localhost", "API-AOLL7T3QASOZEJROIAYAGBWA6M");
var repository = new OctopusRepository(endpoint);
var project = repository.Projects.FindByName("Test");
var set = repository.VariableSets.Get(project.VariableSetId);
// Export to a CSV file
File.WriteAllText(@"C:\temp\vars.json", JsonConvert.SerializeObject(set, Newtonsoft.Json.Formatting.Indented));
// Time passes
// Import it again
set = JsonConvert.DeserializeObject<VariableSetResource>(File.ReadAllText(@"C:\temp\vars.json"));
var currentSet = repository.VariableSets.Get(project.VariableSetId);
set.Id = currentSet.Id; // Required if you are importing from a different project
set.OwnerId = currentSet.OwnerId; // Required if you are importing from a different project
set.Version = currentSet.Version; // Required if you want to update regardless of whether they have changed since export
set.Links = currentSet.Links; // Fixed up the links collection
// Add an environment to the Scope of a variable
var variable = set.Variables.First(v => v.Name == "Scoped");
var environmentId = set.ScopeValues.Environments.First(r => r.Name == "Local").Id;
ScopeValue envScope;
if (variable.Scope.TryGetValue(ScopeField.Environment, out envScope))
{
envScope.Add(environmentId);
}
else
{
envScope = new ScopeValue(environmentId);
variable.Scope[ScopeField.Environment] = envScope;
}
variable.Scope[ScopeField.Environment] = envScope;
// Update the variable
repository.VariableSets.Modify(set);