从excel输出将变量导入Octopus Deploy

时间:2018-03-14 19:44:22

标签: c# import octopus-deploy

我正在尝试使用C#将excel文件中的项目值导入Octopus Deploy。 格式如下:

项目名称|服务器|变量(名称)|变量(值)

APP1,WEB-SBX-WWW3,Configuration Data,vNext\DeployConfiguration.ps1
APP1,WEB-SBX-WWW3,Configuration Document,vNext\Deploy.ps1
APP1,WEB-SBX-WWW3,OutPut,DSC_OutPut
APP1,WEB-SBX-WWW3,INSTALLIIS,Present

阅读excel的代码:

using(FileStream fs = File.OpenRead(CSVLocation)) {
  using(StreamReader reader = new StreamReader(fs)) {
   while (!reader.EndOfStream) {
    string line = reader.ReadLine();
    string[] values = line.Split(new char[] {
     ','
    });
    if (!appList.ContainsKey(values[0])) {
     appList.Add(values[0], values[0]);
    }
    list.Add(new Tuple < string, VariableResource > (values[0], Program.generateVariableResource(values[2], values[3], values[1])));
   }
  }
}

创建要添加的变量列表的代码:

foreach(KeyValuePair < string, string > str in appList) {
 Program.project = Program._repository.Projects.FindOne((ProjectResource p) => p.Name == str.Key, null, null);
 Program.projectSets = Program._repository.VariableSets.Get(Program.project.VariableSetId);
 List < VariableResource > variableResourceList = new List < VariableResource > ();
 foreach(Tuple < string, VariableResource > item in list) {
  if (item.Item1 == str.Key) {
   variableResourceList.Add(item.Item2);
  }
 }

 Program.addVariablesToOctopus(variableResourceList);
 Console.WriteLine(string.Format("Added variables to Octopus for:{0}", str.Key));
}

将代码添加到Octopus的代码 - 我一直收到null错误,我无法弄清楚导致错误的是什么(或没有被发送)?

public static void addVariablesToOctopus(List < VariableResource > variableResourceList) {
 Program.projectSets.Variables = (IList < VariableResource > ) variableResourceList;
 Program._repository.VariableSets.Modify(Program.projectSets);
}

此处出现错误: Program._repository.VariableSets.Modify(Program.projectSets)

public static VariableResource generateVariableResource(string name, string value, string machineName)
        {
            VariableResource variableResource = new VariableResource();
            variableResource.Name = name;
            variableResource.Value = value;
            ScopeSpecification scopeSpecification1 = new ScopeSpecification();
            scopeSpecification1.Add(ScopeField.Machine, new ScopeValue(Program.GetMachineId(machineName)));
            ScopeSpecification scopeSpecification2 = scopeSpecification1;
            variableResource.Scope = scopeSpecification2;
            return variableResource;
        }

0 个答案:

没有答案