Create step from action template programmatically

I know I’ve cloned steps and what not and dynamically built projects on the fly from project steps that already existed in c# applications. Is it possible to build out a project from scratch using action templates? Trying to do that now, but can’t find any examples on it, and can’t seem to figure out how to change an ActionTemplateResource into a DeploymentActionResource. Appreciate any help.

So I think I got it figured out, anyone interested in the code to do so, this was just a quick console app I put together to test it out.

using System;
using System.Collections.Generic;
using Octopus.Client;
using Octopus.Client.Model;

namespace OctopusTestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var endpoint = new OctopusServerEndpoint("", "");
            var repository = new OctopusRepository(endpoint);
            
            var project = new ProjectResource
            {
                Name = "TestStepAdding",
                ProjectGroupId = "ProjectGroups-21",
                LifecycleId = "Lifecycles-23"
            };

            if (repository.Projects.FindByName(project.Name) == null)
            {
                Console.WriteLine($"{project.Name} didn't exist, creating...");
                repository.Projects.Create(project);
                Console.WriteLine($"New Project {project.Name} Created!");
            }
            Console.WriteLine($"New Project {project.Name} Found");
            var newproject = repository.Projects.FindByName(project.Name);

            var chainStepTemplate = repository.ActionTemplates.FindByName("Step Template");

            Console.WriteLine("Template Found");

            var deploymentProcess = repository.DeploymentProcesses.Get(newproject.DeploymentProcessId);

            var deploymentStep = new DeploymentStepResource();

            deploymentStep.Name = "New Step";
            deploymentStep.Condition = DeploymentStepCondition.Success;
            deploymentStep.StartTrigger = DeploymentStepStartTrigger.StartAfterPrevious;

            var action = new DeploymentActionResource
            {
                Name = deploymentStep.Name,
                ActionType = chainStepTemplate.ActionType,

            };
            Console.WriteLine("Adding actions");
            action.Properties.Add(new KeyValuePair<string, PropertyValueResource>("Octopus.Action.Template.Id", chainStepTemplate.Id));
            
            action.Properties.Add(new KeyValuePair<string, PropertyValueResource>("Octopus.Action.Template.Version", chainStepTemplate.Version.ToString()));

            foreach (var parameter in chainStepTemplate.Parameters)
            {
                var property = new KeyValuePair<string, PropertyValueResource>(parameter.Name, parameter.DefaultValue);
                
                action.Properties.Add(property);
                Console.WriteLine($"{parameter.Name}");
            }
            
            foreach (var property in chainStepTemplate.Properties)
            {
                action.Properties.Add(property);
                Console.WriteLine($"{property.Key}");
            }

            deploymentStep.Actions.Add(action);
            Console.WriteLine("Action added");
            deploymentProcess.Steps.Add(deploymentStep);
            Console.WriteLine("Step added to Deployment Process");
            try
            {
                repository.DeploymentProcesses.Modify(deploymentProcess);
                Console.WriteLine("Deployment Process was modified");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}

Hi,

Thanks for getting in touch! I’m glad you’ve solved your problem.

The code looks great and if you don’t mind we would appreciate a PR to our public repository so other users can take advantage of it.

Regards,

Pawel

Thanks Pawel, I have submitted a pull request under the projects folder.

Perfect. Thank you very much!