程序化克隆八达通部署流程步骤,并修改克隆后的步骤。

问题描述 投票:0回答:1

我们正在开发一个Pipeline,我们必须为其添加100多个步骤,并为每个步骤修改两个东西。 与其通过用户界面痛苦地做这些事情,不如用编程的方式来做。

下面是我为此勾画出的一些C#(我是一个C#开发人员,PowerShell技能极其有限,这就是为什么我用C#来做这件事)。"从这里开始是我模糊的地方 "注释上面的行是工作代码,但注释下面的行只是伪代码。

谁能给我解释一下如何编写注释下面的行(或者,相当于PowerShell)?我无法找到API调用。

我找不到这方面的API调用,谢谢

    namespace ODClientExample
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> ListOfWindowsServices = new List<string>();
            ListOfWindowsServices.Add("svc1");
            ListOfWindowsServices.Add("svc2");
            ListOfFWindowsServices.Add("svc3");
            var server = "https://mysite.whatever/";
            var apiKey = "API-xxxxxxxxxxxxxxxxxx";   // I generated this via the Octopus UI         

            var endpoint = new OctopusServerEndpoint(server, apiKey);
            var repository = new OctopusRepository(endpoint);
            var project = repository.Projects.FindByName("Windows Services");

            // From here on is where I'm fuzzy:
            //
            var procesSteps = GetProcessSteps(project);
            var processStepToClone = GetProcesStepByName(processSteps, "SomeProcessStep");
            foreach (string svcName in ListofSvcNames)
            {
                processStepToClone.StepName = svcName;
                processStepToClone.PackageID = svcName;
            }
        }
    }
}

我又取得了一点进展。 我现在能够访问流程中的步骤,并添加一个步骤。 然而,当我的代码调用repository.DeploymentProcesses.Modify时,我得到了这个异常。

  • 请为包的ID提供一个值。
  • 请选择将从该包下载的feed。
  • 请选择'svc1'步骤将适用于的一个或多个角色。

这是我最新的代码。

    static void Main(string[] args)
    {
        List<string> ListOfFexWindowsServices = new List<string>();
        ListOfFexWindowsServices.Add("svc2");
        ListOfFexWindowsServices.Add("svc3");
        ListOfFexWindowsServices.Add("svc4");
        string server = "https://mysite.stuff/";
        string apiKey = "API-xxxxxxxxxxxxxxxxxxxxxxxx";   // I generated this via the Octopus UI         

        OctopusServerEndpoint endpoint = new OctopusServerEndpoint(server, apiKey);
        OctopusRepository repository = new OctopusRepository(endpoint);
        ProjectResource projectResource = repository.Projects.FindByName("MyProject");
        DeploymentProcessResource deploymentProcess = repository.DeploymentProcesses.Get(projectResource.DeploymentProcessId);

        var projectSteps = deploymentProcess.Steps;
        DeploymentStepResource stepToClone = new DeploymentStepResource();
        foreach (DeploymentStepResource step in projectSteps)
        {
            if (step.Name == "svc1")
            {
                stepToClone = step;
                break;
            }
        }

        foreach (string serviceName in ListOfFexWindowsServices)
        {
            DeploymentStepResource newStep = new DeploymentStepResource();
            PopulateNewStep(newStep, stepToClone, serviceName);
            deploymentProcess.Steps.Add(newStep);
            repository.DeploymentProcesses.Modify(deploymentProcess);
        }
    }

    static void PopulateNewStep(DeploymentStepResource newStep, DeploymentStepResource stepToClone, string serviceName)
    {
        newStep.Name = serviceName;
        newStep.Id = Guid.NewGuid().ToString();
        newStep.StartTrigger = stepToClone.StartTrigger;
        newStep.Condition = stepToClone.Condition;
        DeploymentActionResource action = new DeploymentActionResource
        {
            Name = newStep.Name,
            ActionType = "Octopus.TentaclePackage",
            Id = Guid.NewGuid().ToString(),
        };

        PopulateActionProperties(action);
        newStep.Actions.Add(action);
        // ISSUE:  Anything else to do (eg, any other things from stepToClone to copy, or other stuff to create)?
        newStep.PackageRequirement = stepToClone.PackageRequirement;

    }

    static void PopulateActionProperties(DeploymentActionResource action)
    {
        action.Properties.Add(new KeyValuePair<string, PropertyValueResource>("Octopus.Action.WindowsService.CustomAccountPassword", "#{WindowsService.Password}"));

        // TODO:  Repeat this sort of thing for each Action Property you see in stepToClone.
    }
octopus-deploy
1个回答
0
投票
void Main()
{
    var sourceProjectName = "<source project name>";
    var targetProjectName = "<target project name>";
    var stepToCopyName = "<step name to copy>";

    var repo = GetOctopusRepository();
    var sourceProject = repo.Projects.FindByName(sourceProjectName);
    var targetProject = repo.Projects.FindByName(targetProjectName);

    if (sourceProject != null && targetProject != null)
    {
        var sourceDeploymentProcess = repo.DeploymentProcesses.Get(sourceProject.DeploymentProcessId);
        var targetDeploymentProcess = repo.DeploymentProcesses.Get(targetProject.DeploymentProcessId);

        if (sourceDeploymentProcess != null && targetDeploymentProcess != null)
        {
            Console.WriteLine($"Start copy from project '{sourceProjectName}' to project '{targetProjectName}'");

            CopyStepToTarget(sourceDeploymentProcess, targetDeploymentProcess, stepToCopyName);

            // Update or add the target deployment process
            repo.DeploymentProcesses.Modify(targetDeploymentProcess);

            Console.WriteLine($"End copy from project '{sourceProjectName}' to project '{targetProjectName}'");
        }
    }
}

private OctopusRepository GetOctopusRepository()
{
    var octopusServer = Environment.GetEnvironmentVariable("OCTOPUS_CLI_SERVER");
    var octopusApiKey = Environment.GetEnvironmentVariable("OCTOPUS_CLI_API_KEY");
    var endPoint = new OctopusServerEndpoint(octopusServer, octopusApiKey);

    return new OctopusRepository(endPoint);
}

private void CopyStepToTarget(DeploymentProcessResource sourceProcess, DeploymentProcessResource targetProcess, string sourceStepName, bool includeChannels = false, bool includeEnvironments = false)
{
    var sourceStep = sourceProcess.FindStep(sourceStepName);

    if (sourceStep == null)
    {
        Console.WriteLine($"{sourceStepName} not found in {sourceProcess.ProjectId}");
        return;
    }

    Console.WriteLine($"-> copy step '{sourceStep.Name}'");

    var stepToAdd = targetProcess.AddOrUpdateStep(sourceStep.Name);
    stepToAdd.RequirePackagesToBeAcquired(sourceStep.RequiresPackagesToBeAcquired);
    stepToAdd.WithCondition(sourceStep.Condition);
    stepToAdd.WithStartTrigger(sourceStep.StartTrigger);

    foreach (var property in sourceStep.Properties)
    {
        if (stepToAdd.Properties.ContainsKey(property.Key))
        {
            stepToAdd.Properties[property.Key] = property.Value;
        }
        else
        {
            stepToAdd.Properties.Add(property.Key, property.Value);
        }
    }

    foreach (var sourceAction in sourceStep.Actions)
    {
        Console.WriteLine($"-> copy action '{sourceAction.Name}'");

        var targetAction = stepToAdd.AddOrUpdateAction(sourceAction.Name);
        targetAction.ActionType = sourceAction.ActionType;
        targetAction.IsDisabled = sourceAction.IsDisabled;

        if (includeChannels)
        {
            foreach (var sourceChannel in sourceAction.Channels)
            {
                targetAction.Channels.Add(sourceChannel);
            }
        }

        if (includeEnvironments)
        {
            foreach (var sourceEnvironment in sourceAction.Environments)
            {
                targetAction.Environments.Add(sourceEnvironment);
            }
        }

        foreach (var actionProperty in sourceAction.Properties)
        {
            if (targetAction.Properties.ContainsKey(actionProperty.Key))
            {
                targetAction.Properties[actionProperty.Key] = actionProperty.Value;
            }
            else
            {
                targetAction.Properties.Add(actionProperty.Key, actionProperty.Value);
            }
        }
    }
}

上面的代码示例可以在 八达通客户端 Api 示例

© www.soinside.com 2019 - 2024. All rights reserved.