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);
        }
Exemple #2
0
        public static List <ReleaseDeployPhase> GetPhases(this ReleaseEnvironment source)
        {
            List <ReleaseDeployPhase> phases = new List <ReleaseDeployPhase>();

            if (source?.DeploySteps != null && source.DeploySteps.Any())
            {
                DeploymentAttempt deploymentAttempt = source.GetMaxDeploymentAttempt();

                foreach (var releaseDeployPhase in deploymentAttempt.ReleaseDeployPhases)
                {
                    phases.Add(releaseDeployPhase);
                }
            }

            return(phases);
        }
Exemple #3
0
        public static List <ReleaseTask> GetReleaseTasks(this ReleaseEnvironment source)
        {
            List <ReleaseTask> tasks = new List <ReleaseTask>();

            if (source?.DeploySteps != null && source.DeploySteps.Any())
            {
                DeploymentAttempt deploymentAttempt = source.GetMaxDeploymentAttempt();

                foreach (var releaseDeployPhase in deploymentAttempt.ReleaseDeployPhases)
                {
                    foreach (var deploymentJob in releaseDeployPhase.DeploymentJobs)
                    {
                        tasks.AddRange(deploymentJob.Tasks);
                    }
                }
            }

            return(tasks);
        }
        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);
        }