public System.IO.Stream GetReleaseTaskAttachmentContent()
        {
            string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;

            // Get a release client instance
            VssConnection     connection    = Context.Connection;
            ReleaseHttpClient releaseClient = connection.GetClient <ReleaseHttpClient>();

            WebApiRelease release = releaseClient.GetReleaseAsync(project: projectName, releaseId: this._newlyCreatedRelease).Result;

            // Get release task attachments
            ReleaseEnvironment environment = release.Environments.FirstOrDefault();
            DeploymentAttempt  deployStep  = environment.DeploySteps.First();
            Guid planId = deployStep.ReleaseDeployPhases.First().RunPlanId.Value;

            List <ReleaseTaskAttachment> releaseTaskAttachment = releaseClient.GetReleaseTaskAttachmentsAsync(project: projectName, releaseId: release.Id, environmentId: environment.Id, attemptId: deployStep.Attempt, planId: planId, type: "myattachmenttype").Result;

            ReleaseTaskAttachment firstReleaseTaskAttachment = releaseTaskAttachment.First();
            Guid   timelineId     = firstReleaseTaskAttachment.TimelineId;
            Guid   recordId       = firstReleaseTaskAttachment.RecordId;
            string attachmentType = firstReleaseTaskAttachment.Type;
            string attachmentName = firstReleaseTaskAttachment.Name;

            System.IO.Stream attachmentData = releaseClient.GetReleaseTaskAttachmentContentAsync(project: projectName, releaseId: release.Id, environmentId: environment.Id, attemptId: deployStep.Attempt, planId: planId, timelineId: timelineId, recordId: recordId, type: attachmentType, name: attachmentName).Result;

            Context.Log("{0} {1}", attachmentName.PadLeft(6), attachmentType);

            return(attachmentData);
        }
Exemplo n.º 2
0
        public WebApiRelease UpdateRelease()
        {
            string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;

            // Get a release client instance
            VssConnection     connection    = Context.Connection;
            ReleaseHttpClient releaseClient = connection.GetClient <ReleaseHttpClient>();

            List <WebApiRelease> releases = releaseClient.GetReleasesAsync(project: projectName).Result;
            int releaseId = releases.FirstOrDefault().Id;

            WebApiRelease release = releaseClient.GetReleaseAsync(project: projectName, releaseId: releaseId).Result;

            IDictionary <string, ConfigurationVariableValue> varaibles = release.Variables;

            varaibles.Add(
                new KeyValuePair <string, ConfigurationVariableValue>("System.Debug", new ConfigurationVariableValue
            {
                AllowOverride = true, IsSecret = false, Value = "false"
            }
                                                                      )
                );

            // Update the release
            WebApiRelease updatedRelease = releaseClient.UpdateReleaseAsync(release: release, project: projectName, releaseId: releaseId).Result;

            Context.Log("{0} {1}", release.Id.ToString().PadLeft(6), updatedRelease.Name);

            return(release);
        }
Exemplo n.º 3
0
        /// <summary>
        /// View build artifacts
        /// </summary>
        /// <param name="teamProjectName"></param>
        /// <param name="relId"></param>
        private static void ViewBuildArtifacts(string teamProjectName, int relId)
        {
            var relDetails = ReleaseClient.GetReleaseAsync(teamProjectName, relId).Result;

            if (relDetails.Artifacts != null)
            {
                foreach (var artifact in relDetails.Artifacts)
                {
                    if (artifact.Type == "Build")
                    {
                        Console.WriteLine("Build artifact: REPO - {0}; BRANCH - {1}",
                                          artifact.DefinitionReference["repository"].Name,
                                          artifact.DefinitionReference["branch"].Name);

                        int buildId;
                        if (Int32.TryParse(artifact.DefinitionReference["version"].Name, out buildId))
                        {
                            var commits   = BuildClient.GetBuildChangesAsync(teamProjectName, buildId, top: 10).Result;
                            var workItems = BuildClient.GetBuildWorkItemsRefsAsync(teamProjectName, buildId, top: 10).Result;

                            Console.WriteLine("BUILDID   : {0}", buildId);
                            Console.WriteLine("COMMITS   : {0}", String.Join("; ", from x in commits select x.Id.Substring(0, 8)));
                            Console.WriteLine("WORK ITEMS: {0}", String.Join("; ", from x in workItems select x.Id));
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Check status of created release
        /// </summary>
        /// <param name="teamProjectName"></param>
        /// <param name="releaseId"></param>
        private static void CheckStatus(string teamProjectName, int releaseId)
        {
            var release = ReleaseClient.GetReleaseAsync(teamProjectName, releaseId).Result;

            Console.WriteLine("\nStatus: " + release.Status + "\nEnvironments:");

            foreach (var env in release.Environments)
            {
                Console.Write(env.Name + " : " + env.Status + "; ");
            }
        }
Exemplo n.º 5
0
        static void GetWorkItemsForARelease(ReleaseHttpClient rmClient, int currentReleaseId)
        {
            var currentRelease = rmClient.GetReleaseAsync(project: projectName, releaseId: currentReleaseId).Result;
            var definitionId   = currentRelease.ReleaseDefinition.Id;
            var lastTwoRelease = rmClient.GetReleasesAsync(
                project: projectName,
                definitionId: definitionId,
                continuationToken: currentReleaseId,
                top: 2,
                queryOrder: ReleaseQueryOrder.Descending).Result;

            var baseReleaseId = lastTwoRelease[1].Id;

            var workItems = rmClient.GetReleaseWorkItemsRefsAsync(project: projectName, releaseId: currentReleaseId, baseReleaseId: baseReleaseId).Result;
        }
Exemplo n.º 6
0
        public Release GetRelease(int id, string accessToken)
        {
            if (id < 0)
            {
                throw new ArgumentException("id cannot be less than 0", nameof(id));
            }
            if (string.IsNullOrWhiteSpace(accessToken))
            {
                throw new ArgumentException("parameter cannot be null or whitespace", nameof(accessToken));
            }

            this.logger.LogDebug($"getting release. id:[{id}]");

            ReleaseHttpClient client = this.GetClient <ReleaseHttpClient>(accessToken);

            return(client.GetReleaseAsync(this.vstsConfig.ProjectName, id).Result);
        }
Exemplo n.º 7
0
        public WebApiRelease GetRelease()
        {
            string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;

            // Get a release client instance
            VssConnection     connection    = Context.Connection;
            ReleaseHttpClient releaseClient = connection.GetClient <ReleaseHttpClient>();

            List <WebApiRelease> releases = releaseClient.GetReleasesAsync(project: projectName).Result;

            int releaseId = releases.FirstOrDefault().Id;

            // Show a release
            WebApiRelease release = releaseClient.GetReleaseAsync(project: projectName, releaseId: releaseId).Result;

            Console.WriteLine("{0} {1}", release.Id.ToString().PadLeft(6), release.Name);

            return(release);
        }
Exemplo n.º 8
0
        public string GetReleaseRevisionOfRelease()
        {
            string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;

            // Get a release client instance
            VssConnection     connection    = Context.Connection;
            ReleaseHttpClient releaseClient = connection.GetClient <ReleaseHttpClient>();

            // Get release
            WebApiRelease release = releaseClient.GetReleaseAsync(project: projectName, releaseId: completedReleaseId).Result;

            // Get release revision
            System.IO.Stream releaseRevision = releaseClient.GetReleaseRevisionAsync(project: projectName, releaseId: release.Id, definitionSnapshotRevision: release.DefinitionSnapshotRevision).Result;
            Context.Log("{0} {1}", release.Id.ToString().PadLeft(6), release.Id);

            StreamReader reader      = new StreamReader(releaseRevision);
            string       releaseData = reader.ReadToEnd();

            return(releaseData);
        }
Exemplo n.º 9
0
        public System.IO.Stream GetLogsOfReleaseTask()
        {
            string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;

            // Get a release client instance
            VssConnection     connection    = Context.Connection;
            ReleaseHttpClient releaseClient = connection.GetClient <ReleaseHttpClient>();

            // Get Release Defintion
            ReleaseDefinition releaseDefinition = releaseClient.GetReleaseDefinitionAsync(project: projectName, definitionId: newlyCreatedReleaseDefinitionId).Result;

            // Get all completed environment releases.
            // environmentStatusFilter values Undefined = 0,  NotStarted = 1, InProgress = 2, Succeeded = 4, Canceled = 8, Rejected = 16, Qeued = 32, Scheduled = 64, PartiallySucceeded = 128
            List <WebApiRelease> releases = new List <WebApiRelease>();

            ClientSampleHelpers.Retry(
                TimeSpan.FromMinutes(2),
                TimeSpan.FromSeconds(5),
                () =>
            {
                releases = releaseClient.GetReleasesAsync(project: projectName, definitionId: releaseDefinition.Id, definitionEnvironmentId: releaseDefinition.Environments.First().Id, expand: ReleaseExpands.Environments, environmentStatusFilter: 4).Result;
                return(releases.Count > 0);
            });

            // Get first release first environment first deployment first task log
            WebApiRelease release = releaseClient.GetReleaseAsync(project: projectName, releaseId: releases.First().Id).Result;

            this.completedReleaseId = release.Id;
            ReleaseEnvironment releaseEnvironment = release.Environments.First();
            ReleaseDeployPhase deployPhase        = releaseEnvironment.DeploySteps.First().ReleaseDeployPhases.FirstOrDefault();
            ReleaseTask        task = deployPhase.DeploymentJobs.First().Tasks.First();

            System.IO.Stream taskLog = releaseClient.GetTaskLogAsync(project: projectName, releaseId: release.Id, environmentId: releaseEnvironment.Id, releaseDeployPhaseId: deployPhase.Id, taskId: task.Id).Result;
            Context.Log("{0} {1}", release.Id.ToString().PadLeft(6), release.Id);

            return(taskLog);
        }
 public Task <Release> GetReleaseAsync(
     string project,
     int releaseId)
 {
     return(_client.GetReleaseAsync(project, releaseId));
 }
 public async Task <Release> GetReleaseById(int id)
 {
     return(await _client.GetReleaseAsync(Credentials.PROJECT, id));
 }
Exemplo n.º 12
0
        public ReleaseGates IgnoreGate()
        {
            string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;
            string gateName    = "Query Work Items";
            ReleaseDefinitionGatesStep releaseDefinitionGatesStep = new ReleaseDefinitionGatesStep
            {
                Gates = new List <ReleaseDefinitionGate>
                {
                    new ReleaseDefinitionGate
                    {
                        Tasks = new List <WorkflowTask>
                        {
                            new WorkflowTask
                            {
                                Enabled = true,
                                Name    = gateName,
                                TaskId  = new Guid("f1e4b0e6-017e-4819-8a48-ef19ae96e289"),
                                Version = "0.*",
                                Inputs  = new Dictionary <string, string>
                                {
                                    { "queryId", "c871ca91-e30c-43a0-9306-97a2be93861e" },
                                    { "maxThreshold", "6" },
                                    { "minThreshold", "2" }
                                }
                            }
                        }
                    }
                },
                GatesOptions = new ReleaseDefinitionGatesOptions
                {
                    IsEnabled = true,
                    MinimumSuccessDuration = 2,
                    SamplingInterval       = 5,
                    StabilizationTime      = 10,
                    Timeout = 60
                },
                Id = 0
            };

            ReleaseDefinition definition = new ReleaseDefinition()
            {
                Name         = releaseDefinitionName,
                Revision     = 1,
                Environments = new List <ReleaseDefinitionEnvironment>()
                {
                    new ReleaseDefinitionEnvironment()
                    {
                        Name       = "PROD",
                        Conditions = new List <Condition>()
                        {
                            new Condition()
                            {
                                ConditionType = ConditionType.Event,
                                Name          = "ReleaseStarted"
                            }
                        },
                        DeployPhases = new List <DeployPhase>()
                        {
                            new RunOnServerDeployPhase()
                            {
                                Name          = "Agentless phase",
                                Rank          = 1,
                                WorkflowTasks = new List <WorkflowTask>()
                                {
                                    new WorkflowTask()
                                    {
                                        Name    = "Manual Intervention",
                                        TaskId  = new Guid("bcb64569-d51a-4af0-9c01-ea5d05b3b622"),
                                        Version = "8.*",
                                        Enabled = true
                                    }
                                }
                            }
                        },
                        PreDeployApprovals = new ReleaseDefinitionApprovals()
                        {
                            Approvals = new List <ReleaseDefinitionApprovalStep>()
                            {
                                new ReleaseDefinitionApprovalStep()
                                {
                                    IsAutomated = true,
                                    Rank        = 1,
                                }
                            }
                        },
                        PostDeployApprovals = new ReleaseDefinitionApprovals()
                        {
                            Approvals = new List <ReleaseDefinitionApprovalStep>()
                            {
                                new ReleaseDefinitionApprovalStep()
                                {
                                    IsAutomated = true,
                                    Rank        = 1
                                }
                            }
                        },
                        RetentionPolicy = new EnvironmentRetentionPolicy()
                        {
                            DaysToKeep     = 30,
                            ReleasesToKeep = 3,
                            RetainBuild    = true
                        },
                        PreDeploymentGates = releaseDefinitionGatesStep
                    }
                }
            };

            // Get a release client instance
            VssConnection     connection    = Context.Connection;
            ReleaseHttpClient releaseClient = connection.GetClient <ReleaseHttpClient>();

            // create a release definition
            ReleaseDefinition releaseDefinition = releaseClient.CreateReleaseDefinitionAsync(project: projectName, releaseDefinition: definition).Result;

            this._newlyCreatedReleaseDefinitionId = releaseDefinition.Id;

            // create a release
            WebApiRelease release = ReleasesSample.CreateRelease(releaseClient, _newlyCreatedReleaseDefinitionId, projectName);

            Context.Log("{0} {1}", release.Id.ToString().PadLeft(6), release.Name);

            // Wait till deployment moves to evaluation gates state
            ClientSampleHelpers.Retry(
                TimeSpan.FromMinutes(2),
                TimeSpan.FromSeconds(5),
                () =>
            {
                release = releaseClient.GetReleaseAsync(project: projectName, releaseId: release.Id).Result;
                return(release != null && release.Environments.First().DeploySteps.FirstOrDefault().OperationStatus == DeploymentOperationStatus.EvaluatingGates);
            });

            // Ignore the gate
            GateUpdateMetadata gateUpdateMetadata = new GateUpdateMetadata
            {
                Comment       = "Ignore gate",
                GatesToIgnore = new List <string> {
                    gateName
                }
            };

            int          gateStepId   = release.Environments.FirstOrDefault().DeploySteps.FirstOrDefault().PreDeploymentGates.Id;
            ReleaseGates releaseGates = releaseClient.UpdateGatesAsync(gateUpdateMetadata: gateUpdateMetadata, project: projectName, gateStepId: gateStepId).Result;

            Context.Log("{0} {1}", releaseGates.Id.ToString().PadLeft(6), releaseGates.Id);

            return(releaseGates);
        }
        public List <ReleaseTaskAttachment> GetReleaseAttachment()
        {
            string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;

            ReleaseDefinition definition = new ReleaseDefinition()
            {
                Name         = releaseDefinitionName,
                Revision     = 1,
                Environments = new List <ReleaseDefinitionEnvironment>()
                {
                    new ReleaseDefinitionEnvironment()
                    {
                        Name       = "PROD",
                        Conditions = new List <Condition>()
                        {
                            new Condition()
                            {
                                ConditionType = ConditionType.Event,
                                Name          = "ReleaseStarted"
                            }
                        },
                        DeployPhases = new List <DeployPhase>()
                        {
                            new AgentBasedDeployPhase()
                            {
                                Name            = "Run on agent",
                                Rank            = 1,
                                DeploymentInput = new AgentDeploymentInput()
                                {
                                    QueueId = 1
                                },
                                WorkflowTasks = new List <WorkflowTask>
                                {
                                    new WorkflowTask
                                    {
                                        Name             = taskName,
                                        Enabled          = true,
                                        TimeoutInMinutes = 0,
                                        Inputs           = new Dictionary <string, string> {
                                            { "targetType", "inline" },
                                            { "script", "New-Item -Path 'newfile.txt' -ItemType File\n\nWrite-Host \"##vso[task.addattachment type=myattachmenttype;name=myattachmentname;]$(SYSTEM.DEFAULTWORKINGDIRECTORY)\\newfile.txt\"" }
                                        },
                                        TaskId         = new Guid("e213ff0f-5d5c-4791-802d-52ea3e7be1f1"),
                                        Version        = "2.*",
                                        DefinitionType = "task",
                                        Condition      = "succeeded()",
                                    }
                                }
                            }
                        },
                        PreDeployApprovals = new ReleaseDefinitionApprovals()
                        {
                            Approvals = new List <ReleaseDefinitionApprovalStep>()
                            {
                                new ReleaseDefinitionApprovalStep()
                                {
                                    IsAutomated = true,
                                    Rank        = 1
                                }
                            }
                        },
                        PostDeployApprovals = new ReleaseDefinitionApprovals()
                        {
                            Approvals = new List <ReleaseDefinitionApprovalStep>()
                            {
                                new ReleaseDefinitionApprovalStep()
                                {
                                    IsAutomated = true,
                                    Rank        = 1
                                }
                            }
                        },
                        RetentionPolicy = new EnvironmentRetentionPolicy()
                        {
                            DaysToKeep     = 30,
                            ReleasesToKeep = 3,
                            RetainBuild    = true
                        }
                    }
                }
            };

            // Get a release client instance
            VssConnection     connection    = Context.Connection;
            ReleaseHttpClient releaseClient = connection.GetClient <ReleaseHttpClient>();

            // create a release definition
            ReleaseDefinition releaseDefinition = releaseClient.CreateReleaseDefinitionAsync(project: projectName, releaseDefinition: definition).Result;

            this._newlyCreatedReleaseDefinitionId = releaseDefinition.Id;
            Context.Log("{0} {1} {2}", releaseDefinition.Id.ToString().PadLeft(6), releaseDefinition.Name, projectName);

            // create a release
            WebApiRelease release = ReleasesSample.CreateRelease(releaseClient, _newlyCreatedReleaseDefinitionId, projectName);

            Context.Log("{0} {1}", release.Id.ToString().PadLeft(6), release.Name);
            _newlyCreatedRelease = release.Id;

            // Wait till deployment completed
            ClientSampleHelpers.Retry(
                TimeSpan.FromMinutes(2),
                TimeSpan.FromSeconds(5),
                () =>
            {
                release = releaseClient.GetReleaseAsync(project: projectName, releaseId: release.Id).Result;
                return(release != null && release.Environments.First().Status == EnvironmentStatus.Succeeded);
            });

            // Get release task attachments
            ReleaseEnvironment environment = release.Environments.FirstOrDefault();
            DeploymentAttempt  deployStep  = environment.DeploySteps.First();
            Guid planId = deployStep.ReleaseDeployPhases.First().RunPlanId.Value;
            List <ReleaseTaskAttachment> releaseTaskAttachment = releaseClient.GetReleaseTaskAttachmentsAsync(project: projectName, releaseId: release.Id, environmentId: environment.Id, attemptId: deployStep.Attempt, planId: planId, type: "myattachmenttype").Result;

            Context.Log("{0} {1}", releaseTaskAttachment.First().Name.PadLeft(6), releaseTaskAttachment.First().Type);

            return(releaseTaskAttachment);
        }