static void Main(string[] args) { var apiKey = "API-XXXXXXXXXXXXXXXXXXXXXXXXXX"; var octopusURL = "https://octopus.url"; var endpoint = new OctopusServerEndpoint(octopusURL, apiKey); var client = new OctopusClient(endpoint); var repository = new OctopusRepository(endpoint); var actionTemplates = repository.ActionTemplates.GetAll(); foreach (var actionTemplate in actionTemplates) { var usages = client.Get <ActionTemplateUsageResource[]>(actionTemplate.Links["Usage"]); var usagesToUpdate = usages.Where(u => u.Version != actionTemplate.Version.ToString()); if (!usagesToUpdate.Any()) { continue; } var actionsByProcessId = usagesToUpdate.GroupBy(u => u.DeploymentProcessId); var actionIdsByProcessId = actionsByProcessId.ToDictionary(g => g.Key, g => g.Select(u => u.ActionId).ToArray()); var actionUpdate = new ActionsUpdateResource(); actionUpdate.Version = actionTemplate.Version; actionUpdate.ActionIdsByProcessId = actionIdsByProcessId; repository.ActionTemplates.UpdateActions(actionTemplate, actionUpdate); } }
public void CreateClient() { if (_client != null) { return; } var endpoint = new OctopusServerEndpoint(_inputs.OctopusURL, _inputs.OctopusApiKey); var client = new OctopusClient(endpoint); _client = client; return; }
private async Task OnComplete(IDialogContext context, IAwaitable <DeploymentForm> result) { try { var form = await result; var response = OctopusClient.GetDeploymentStatus(form.Project, form.Environment) ?? "Sorry I could find any deployments for that project and environment"; await context.PostAsync(response); } catch (OperationCanceledException) { await context.PostAsync("Operation Cancelled"); } context.Wait(MessageReceived); }
public static IForm <DeploymentForm> BuildForm() { return(new FormBuilder <DeploymentForm>() .Field(new FieldReflector <DeploymentForm>(nameof(Project)) .SetType(null) .ReplaceTemplate(new TemplateAttribute( TemplateUsage.EnumSelectOne, "Pick a project: {||}") { ChoiceStyle = ChoiceStyleOptions.PerLine }) .SetDefine((state, field) => { foreach (var prod in OctopusClient.GetProjects()) { field .AddDescription(prod.Id, prod.Name) .AddTerms(prod.Id, prod.Name); } return Task.FromResult(true); })) .Field(new FieldReflector <DeploymentForm>(nameof(Environment)) .SetType(null) .ReplaceTemplate(new TemplateAttribute( TemplateUsage.EnumSelectOne, "What evironment would you like to check? {||}") { ChoiceStyle = ChoiceStyleOptions.PerLine }) .SetDefine((state, field) => { foreach (var prod in OctopusClient.GetEnvironments()) { field .AddDescription(prod.Id, prod.Name) .AddTerms(prod.Id, prod.Name); } return Task.FromResult(true); })) .AddRemainingFields() .Build()); }
private static void CreateGroupRelease(string groupName, string version, string environmentName) { Console.WriteLine($"Creating releases version {version} for group '{groupName}'..."); var client = new OctopusClient(new OctopusServerEndpoint(server, apiKey)); var repo = new OctopusRepository(client); var group = repo.ProjectGroups.FindByName(groupName); if (group == null) { throw new NullReferenceException($"Group '{groupName}' not found."); } var projects = repo.Projects.FindMany(x => x.ProjectGroupId == group.Id && !x.IsDisabled); Console.WriteLine($"Found {projects.Count} projects..."); var environment = environmentName != null?repo.Environments.FindByName(environmentName) : null; foreach (var project in projects) { var latestRelease = repo.Projects.GetReleases(project).Items .OrderByDescending(r => SemanticVersion.Parse(r.Version)) .FirstOrDefault(); if (latestRelease == null || latestRelease.Version != version) { latestRelease = CreateRelease(repo, project, version); } else { Console.WriteLine($"\t'{project.Name}' with version {version} is up to date"); } if (environment != null && latestRelease != null && repo.Deployments.FindMany(x => x.ReleaseId == latestRelease.Id).Count == 0) { CreateDeploy(repo, project, environment, latestRelease); } } }
public OctoRepository(ServerInstanceElement instance) { ApiUri = instance.ApiUri.TrimEnd('/'); var endpoint = new OctopusServerEndpoint(ApiUri, instance.ApiKey); var client = new OctopusClient(endpoint); _repository = new OctopusRepository(client); SystemInfoResource systemInfo; try { ServerStatusResource serverStatus = _repository.ServerStatus.GetServerStatus(); systemInfo = _repository.ServerStatus.GetSystemInfo(serverStatus); } catch (OctopusException ex) { throw; } Version = new SemanticVersion(systemInfo.Version); using (var databaseModel = new DatabaseModel()) { OctopusServer octopusServer = databaseModel.OctopusServers.SingleOrDefault(octo => octo.ApiUri == ApiUri); if (octopusServer == null) { octopusServer = new OctopusServer(); octopusServer.ApiUri = ApiUri; octopusServer.DisplayName = ApiUri; databaseModel.OctopusServers.Add(octopusServer); databaseModel.SaveChanges(); } OctopusServerId = octopusServer.Id; } }
static void Main(string[] args) { var octoUrl = ""; var apiKey = ""; var client = new OctopusClient(new OctopusServerEndpoint(octoUrl, apiKey)); var repo = new OctopusRepository(client); //Project you want to create or modify var project = new ProjectResource { Name = "", //name of project ProjectGroupId = "", //group project should be added to. to find by name: repo.ProjectGroups.FindByName("").Id) LifecycleId = "" //lifecycle project should use. to find by name: repo.Lifecycles.FindByName("").Id }; var newproject = repo.Projects.FindByName(project.Name); //Check if project exists, if not create it if (newproject == null) { //Creates Project repo.Projects.Create(project); //get the new project newproject = repo.Projects.FindByName(project.Name); } if (newproject == null) { Console.WriteLine($"{project.Name} didn't exist, creating..."); repo.Projects.Create(project); Console.WriteLine($"New Project {project.Name} Created!"); newproject = repo.Projects.FindByName(project.Name); } Console.WriteLine($"New Project {project.Name} Found"); //This will find the step template you want to add var chainStepTemplate = repo.ActionTemplates.FindByName("Step Template"); //Change this to the name of your step template to add Console.WriteLine("Template Found"); //Get the deployment process for the project var deploymentProcess = repo.DeploymentProcesses.Get(newproject.DeploymentProcessId); //Initialize a new Deployment Step Resource var deploymentStep = new DeploymentStepResource { Name = "New Step", //Change this to what you want the step to be named. Condition = DeploymentStepCondition.Success, StartTrigger = DeploymentStepStartTrigger.StartAfterPrevious }; //Initialize new Action var action = new DeploymentActionResource { Name = deploymentStep.Name, ActionType = chainStepTemplate.ActionType, }; Console.WriteLine("Adding actions"); //Add the template Id action.Properties.Add(new KeyValuePair <string, PropertyValueResource>("Octopus.Action.Template.Id", chainStepTemplate.Id)); //Add the template Version action.Properties.Add(new KeyValuePair <string, PropertyValueResource>("Octopus.Action.Template.Version", chainStepTemplate.Version.ToString())); //Add the parameters for the step template and any default values. Modify this to set other values. foreach (var parameter in chainStepTemplate.Parameters) { var property = new KeyValuePair <string, PropertyValueResource>(parameter.Name, parameter.DefaultValue); action.Properties.Add(property); Console.WriteLine($"{parameter.Name}"); } //Add the standard properties foreach (var property in chainStepTemplate.Properties) { action.Properties.Add(property); Console.WriteLine($"{property.Key}"); } //Add the action to your deployment step deploymentStep.Actions.Add(action); Console.WriteLine("Action added"); //Add your step to the deployment process deploymentProcess.Steps.Add(deploymentStep); Console.WriteLine("Step added to Deployment Process"); //Finally save the deployment process. try { repo.DeploymentProcesses.Modify(deploymentProcess); Console.WriteLine("Deployment Process was modified"); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); }