Ejemplo n.º 1
0
        public async Task <Model.Build> Handle(
            QueueBuildQuery message,
            CancellationToken cancellationToken)
        {
            var connection = _tfsProvider.GetConnection() as VssConnection;

            var buildServer = connection.GetClient <BuildHttpClient>(); // connect to the build server subpart

            if (message.BuildDefinition.BuildDefinitionIdentifier == 0)
            {
                //fill it in
                var builddDefs = await buildServer.GetDefinitionsAsync2(name : message.BuildDefinition.BuildDefinitionName, project : message.BuildDefinition.ProjectId);

                message.BuildDefinition.BuildDefinitionIdentifier = builddDefs.Single().Id;
            }
            var build = new Microsoft.TeamFoundation.Build.WebApi.Build()
            {
                Definition = message.BuildDefinition.ToEntity(),
                Project    = message.BuildDefinition.ToEntity().Project
            };

            try
            {
                var queuedBuild = await buildServer.QueueBuildAsync(build);

                _logger.LogInformation("Queued build for {id} {name}", build.Definition.Id, message.BuildDefinition.BuildDefinitionName);
                do
                {
                    _logger.LogInformation("Build {buildId} is {status}. Waiting 1 second.", queuedBuild.Id, queuedBuild.Status);
                    Thread.Sleep(1000);
                    queuedBuild = await buildServer.GetBuildAsync(message.BuildDefinition.ToEntity().Project.Name, queuedBuild.Id);
                }while (queuedBuild.Status != BuildStatus.Completed);
                _logger.LogInformation("Build is complete. Status {buildResult}", queuedBuild.Result);
                return(queuedBuild.ToModel());
            }
            finally
            {
                // buildServer.DeleteDefinitionAsync(definitionId: buildDefinitionResult.Id, project: buildDefinitionResult.Project.Id).Wait();
                // Console.WriteLine($"Deleted build");
            }
        }
Ejemplo n.º 2
0
        public async Task <bool> Handle(DeployReleaseQuery message, CancellationToken cancellationToken)
        {
            var connection = _tfsProvider.GetConnection() as VssConnection;

            var releaseServer = connection.GetClient <ReleaseHttpClient>(); // connect to the build server subpart

            var release = await releaseServer.GetReleaseAsync(project : message.ProjectId, releaseId : message.ReleaseIdentifier);

            var releaseEnvironmentUpdateMetadata = new ReleaseEnvironmentUpdateMetadata()
            {
                Status = EnvironmentStatus.InProgress
            };

            int releaseEnvironmentId = release.Environments.First(e => e.DefinitionEnvironmentId == message.EnvironmentIdentifier).Id; //  message.EnvironmentIdentifier;
            // Start deployment to an environment

            var releaseEnvironment = releaseServer.UpdateReleaseEnvironmentAsync(releaseEnvironmentUpdateMetadata, message.ProjectId, message.ReleaseIdentifier,
                                                                                 releaseEnvironmentId).Result;

            return(true);
        }
        public async Task <Model.ReleaseDefinition> Handle(
            CreateReleaseDefinitionQuery message,
            CancellationToken cancellationToken)
        {
            string buildDefinitionName = message.ReleaseDefinition.BuildDefinitions.Single().BuildDefinitionName;

            var connection = _tfsProvider.GetConnection() as VssConnection;

            var releaseServer = connection.GetClient <ReleaseHttpClient>(); // connect to the build server subpart
            var buildServer   = connection.GetClient <BuildHttpClient>();   // connect to the build server subpart

            /*
             * var releaseDefinitions = await releaseServer.GetReleaseDefinitionsAsync(project: message.ReleaseDefinition.ProjectId, searchText: "Feature");
             * foreach (var rd in releaseDefinitions)
             * {
             *  Console.WriteLine($"{rd.Name}");
             *  var rdFull = await releaseServer.GetReleaseDefinitionAsync(project: message.ReleaseDefinition.ProjectId, definitionId: rd.Id);
             *  var json = JsonConvert.SerializeObject(rdFull, Formatting.Indented);
             *
             * }
             */
            var releaseDefinition = new ReleaseDefinition()
            {
                Name      = message.ReleaseDefinition.ReleaseDefinitionName,
                Variables = new Dictionary <string, ConfigurationVariableValue>
                {
                },
                VariableGroups = new List <int>
                {
                },
                Artifacts = new List <Artifact>()
                {
                },
                Environments = new List <ReleaseDefinitionEnvironment>()
                {
                }
            };

            AddEnvironmentVariableGroups(message, releaseDefinition.VariableGroups);

            AddReleaseVariables(message, message.ServiceName, releaseDefinition.Variables);


            int rank = 1;

            foreach (var environment in message.ReleaseDefinition.Environments)
            {
                var newEnv = BuildEnvironment(message, environment, buildDefinitionName);
                newEnv.Rank = rank;
                releaseDefinition.Environments.Add(newEnv);

                rank++;
            }

            foreach (var buildDefinition in message.ReleaseDefinition.BuildDefinitions)
            {
                if (buildDefinition.BuildDefinitionIdentifier == 0)
                {
                    //fill it in
                    var builddDefs = await buildServer.GetDefinitionsAsync2(name : buildDefinition.BuildDefinitionName, project : buildDefinition.ProjectId);

                    buildDefinition.BuildDefinitionIdentifier = builddDefs.Single().Id;
                }
                var artifact = BuildArtifact(message, buildDefinition);
                releaseDefinition.Artifacts.Add(artifact);
                //todo: conditionalize
                var trigger = new ArtifactSourceTrigger()
                {
                    ArtifactAlias     = artifact.Alias,
                    TriggerConditions = new List <ArtifactFilter>
                    {
                    }
                };

                if (message.IsProdPath)
                {
                    trigger.TriggerConditions.Add(new ArtifactFilter()
                    {
                        SourceBranch = "release", UseBuildDefinitionBranch = false
                    });
                }
                else
                {
                    trigger.TriggerConditions.Add(new ArtifactFilter()
                    {
                        SourceBranch = "dev", UseBuildDefinitionBranch = false
                    });
                }

                releaseDefinition.Triggers.Add(trigger);
            }


            var createdReleaseDefinition = await releaseServer.CreateReleaseDefinitionAsync(releaseDefinition, project : message.ReleaseDefinition.ProjectId);

            return(createdReleaseDefinition.ToModel());
        }
Ejemplo n.º 4
0
        public async Task <Model.BuildDefinition> Handle(
            CreateBuildDefinitionQuery message,
            CancellationToken cancellationToken)
        {
            var connection = _tfsProvider.GetConnection() as VssConnection;

            var git   = connection.GetClient <GitHttpClient>();
            var repos = git.GetRepositoriesAsync().Result;

            var theRepo             = repos.First(r => r.Name.ToLower() == message.BuildDefinition.RepoName);
            var sourceControlServer = connection.GetClient <TfvcHttpClient>();  // connect to the TFS source control subpart
            var buildServer         = connection.GetClient <BuildHttpClient>(); // connect to the build server subpart

            var buildDefinition = new BuildDefinition()
            {
                Name    = message.BuildDefinition.BuildDefinitionName,
                Project = new Microsoft.TeamFoundation.Core.WebApi.TeamProjectReference
                {
                    Id   = message.BuildDefinition.ProjectId,
                    Name = message.BuildDefinition.ProjectName
                },
                Repository = new BuildRepository
                {
                    Id            = theRepo.Id.ToString(),
                    Type          = "TfsGit",
                    DefaultBranch = "refs/heads/dev" //todo: change to /release
                },
                Queue = new AgentPoolQueue
                {
                    Id = 7 //todo
                }
            };
            //Continuous integration
            var continuousIntegrationTrigger = new ContinuousIntegrationTrigger()
            {
                BatchChanges = false
            };

            continuousIntegrationTrigger.BranchFilters.Add("+refs/heads/dev");
            continuousIntegrationTrigger.BranchFilters.Add("+refs/heads/release");

            if (!string.IsNullOrEmpty(message.BuildDefinition.RepoPath))
            {
                continuousIntegrationTrigger.PathFilters.Add($"-/");
                continuousIntegrationTrigger.PathFilters.Add($"+/{message.BuildDefinition.RepoPath}");
            }

            buildDefinition.Triggers.Add(continuousIntegrationTrigger);
            //end Continuous Integration

            var process = new DesignerProcess()
            {
            };
            var phase1 = new Phase()
            {
                Name = "Phase 1"
            };
            string repoPath = message.BuildDefinition.RepoPath;

            if (!string.IsNullOrEmpty(message.BuildDefinition.RepoPath))
            {
                repoPath += "/";
            }
            phase1.Steps.Add(BuildStepBuildApps(repoPath));
            phase1.Steps.Add(BuildStepTestApps(repoPath));
            phase1.Steps.Add(BuildStepPublishTestResults(repoPath));
            phase1.Steps.Add(BuildStepSetVars(repoPath));
            phase1.Steps.Add(BuildStepBuildServiceImage(repoPath));
            phase1.Steps.Add(BuildStepBuildOutput(repoPath, message.ServiceName));
            phase1.Steps.Add(BuildStepPushServiceImage(repoPath));
            phase1.Steps.Add(BuildStepPublishArtifact());

            process.Phases.Add(phase1);
            buildDefinition.Process = process;

            BuildDefinition buildDefinitionResult = null;

            try
            {
                buildDefinitionResult = buildServer.CreateDefinitionAsync(buildDefinition).Result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(null);
            }
            Console.WriteLine($"Created build definition {buildDefinition.Name} {buildDefinitionResult.Id}");

            return(buildDefinitionResult.ToModel());
        }
Ejemplo n.º 5
0
        public async Task <bool> Handle(CreateReleaseQuery message, CancellationToken cancellationToken)
        {
            var connection = _tfsProvider.GetConnection() as VssConnection;

            var releaseServer = connection.GetClient <ReleaseHttpClient>(); // connect to the build server subpart
            var buildServer   = connection.GetClient <BuildHttpClient>();   // connect to the build server subpart

            if (message.ReleaseDefinition.ReleaseDefinitionIdentifier == 0)
            {
                //fill it in
                //todo: verify returns only single item
                var rd = await releaseServer.GetReleaseDefinitionsAsync(project : message.ReleaseDefinition.ProjectId, searchText : message.ReleaseDefinition.ReleaseDefinitionName);

                foreach (var rdi in rd)
                {
                    if (rdi.Name == message.ReleaseDefinition.ReleaseDefinitionName)
                    {
                        message.ReleaseDefinition.ReleaseDefinitionIdentifier = rdi.Id;
                        break;
                    }
                }
            }

            var releaseStartMetaData = new ReleaseStartMetadata()
            {
                DefinitionId = message.ReleaseDefinition.ReleaseDefinitionIdentifier,

                IsDraft   = false,
                Artifacts = new List <ArtifactMetadata>()
                {
                }
            };

            foreach (var buildDefinition in message.ReleaseDefinition.BuildDefinitions)
            {
                if (buildDefinition.BuildDefinitionIdentifier == 0)
                {
                    //fill it in
                    var builddDefs = await buildServer.GetDefinitionsAsync2(name : buildDefinition.BuildDefinitionName, project : buildDefinition.ProjectId);

                    buildDefinition.BuildDefinitionIdentifier = builddDefs.Single().Id;
                }

                var lastBuild = buildServer.GetBuildsAsync(project: message.ReleaseDefinition.ProjectId, definitions: new[] { buildDefinition.BuildDefinitionIdentifier }, statusFilter: BuildStatus.Completed).Result
                                .OrderByDescending(b => b.Id)
                                .FirstOrDefault();
                releaseStartMetaData.Artifacts.Add(
                    new ArtifactMetadata
                {
                    Alias             = buildDefinition.BuildDefinitionName,
                    InstanceReference = new BuildVersion
                    {
                        Id   = lastBuild.Id.ToString(),
                        Name = lastBuild.BuildNumber
                    }
                });
            }
            var result = await releaseServer.CreateReleaseAsync(releaseStartMetaData, project : message.ReleaseDefinition.ProjectId);

            return(true);
        }