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);
        }
        public void CreateReleaseWithManualIntervention()
        {
            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 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
                        }
                    }
                }
            };

            // 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;

            this._newlyCreatedRelease1 = ReleasesSample.CreateRelease(releaseClient, _newlyCreatedReleaseDefinitionId, projectName);
            this._newlyCreatedRelease2 = ReleasesSample.CreateRelease(releaseClient, _newlyCreatedReleaseDefinitionId, projectName);

            Context.Log("{0} {1}", _newlyCreatedRelease1.Id.ToString().PadLeft(6), _newlyCreatedRelease1.Name);
            Context.Log("{0} {1}", _newlyCreatedRelease2.Id.ToString().PadLeft(6), _newlyCreatedRelease2.Name);
        }
        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);
        }