private static void DeployScheduledPublications(AspNetDeployEntities entities) { List <Publication> publications = entities.Publication .Include("Package.BundleVersion") .Include("Environment") .Where(p => p.State == PublicationState.Queued && !p.Package.Publications.Any(p2 => p2.CreatedDate > p.CreatedDate && p2.EnvironmentId == p.EnvironmentId)) .ToList(); List <IGrouping <BundleVersion, Publication> > groupedPublications = publications .GroupBy(p => p.Package.BundleVersion) .Where(bv => !bv.Key.IsDeleted) .ToList(); groupedPublications.ForEach(group => { TaskRunnerContext.SetBundleVersionState(group.Key.Id, BundleState.Deploying); try { foreach (Publication publication in group) { try { DeploymentJob job = new DeploymentJob(); job.Start( publication.Id, machineId => TaskRunnerContext.SetMachineState(machineId, MachineState.Deploying), (machineId, isSuccess) => TaskRunnerContext.SetMachineState(machineId, isSuccess ? MachineState.Idle : MachineState.Error)); } catch (Exception e) { throw new AspNetDeployException("Publication failed: " + publication.Id, e); } } } catch (Exception e) { TaskRunnerContext.SetBundleVersionState(group.Key.Id, BundleState.Error); Factory.GetInstance <ILoggingService>().Log(e, null); if (e.IsCritical()) { throw; } return; } TaskRunnerContext.SetBundleVersionState(group.Key.Id, BundleState.Idle); }); }
private static void TakeSources(AspNetDeployEntities entities) { List <SourceControlVersion> sourceControlVersions = entities.SourceControlVersion .Include("ProjectVersions") .Include("Properties") .Include("ProjectVersions.BundleVersions") .Include("SourceControl.Properties") .Where(scv => scv.ArchiveState == SourceControlVersionArchiveState.Normal) .ToList(); sourceControlVersions .ForEach(sourceControlVersion => { TaskRunnerContext.SetSourceControlVersionState(sourceControlVersion.Id, SourceControlState.Loading); sourceControlVersion.ProjectVersions .SelectMany(pv => pv.BundleVersions) .Where(bv => !bv.IsDeleted) .ToList() .ForEach(bv => TaskRunnerContext.SetBundleVersionState(bv.Id, BundleState.Loading)); try { (new SourceControlJob(sourceControlVersion.Id)).UpdateAndParse(); TaskRunnerContext.SetSourceControlVersionState(sourceControlVersion.Id, SourceControlState.Idle); } catch (Exception e) { TaskRunnerContext.SetSourceControlVersionState(sourceControlVersion.Id, SourceControlState.Error); Factory.GetInstance <ILoggingService>().Log(new AspNetDeployException("Take sources failed: " + sourceControlVersion.Id, e), null); if (e.IsCritical()) { throw; } } sourceControlVersion.ProjectVersions .SelectMany(pv => pv.BundleVersions) .Where(bv => !bv.IsDeleted) .ToList() .ForEach(bv => TaskRunnerContext.SetBundleVersionState(bv.Id, BundleState.Idle)); }); }
private static void BuildProjects(AspNetDeployEntities entities) { List <BundleVersion> bundleVersions = entities.BundleVersion .Include("Properties") .Include("ProjectVersions.SourceControlVersion.Properties") .ToList(); foreach (BundleVersion bundleVersion in bundleVersions) { List <ProjectVersion> projectVersions = bundleVersion.ProjectVersions.Where(pv => ( pv.ProjectType.HasFlag(ProjectType.Database) || pv.ProjectType.HasFlag(ProjectType.WindowsApplication) || pv.ProjectType.HasFlag(ProjectType.Service) || pv.ProjectType.HasFlag(ProjectType.Console) || pv.ProjectType.HasFlag(ProjectType.Web) || pv.ProjectType.HasFlag(ProjectType.GulpFile) || pv.ProjectType.HasFlag(ProjectType.Test) )) .ToList(); IList <ProjectVersion> errorProjects = projectVersions.Where(pv => pv.GetStringProperty("LastBuildResult") == "Error").ToList(); foreach (ProjectVersion projectVersion in errorProjects) { TaskRunnerContext.SetProjectVersionState(projectVersion.Id, ProjectState.Error); } List <ProjectVersion> projectVersionsToRebuild = projectVersions.Where(pv => pv.SourceControlVersion.GetStringProperty("Revision") != pv.GetStringProperty("LastBuildRevision")).ToList(); if (projectVersionsToRebuild.Count == 0) { if (errorProjects.Count > 0) { TaskRunnerContext.SetBundleVersionState(bundleVersion.Id, BundleState.Error); } continue; } List <BundleVersion> affectedBundleVersions = projectVersionsToRebuild .SelectMany(pv => pv.BundleVersions) .Where(bv => !bv.IsDeleted) .ToList(); DateTime buildStartDate = DateTime.UtcNow; foreach (BundleVersion affectedBundleVersion in affectedBundleVersions) { TaskRunnerContext.SetBundleVersionState(affectedBundleVersion.Id, BundleState.Building); } bundleVersion.SetStringProperty("LastBuildStartDate", DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)); entities.SaveChanges(); ProjectsBuildStrategy buildStrategy = new ProjectsBuildStrategy(entities); buildStrategy.Build(projectVersionsToRebuild); bool buildFailed = projectVersionsToRebuild.Any(pv => pv.GetStringProperty("LastBuildResult") == "Error"); foreach (BundleVersion affectedBundleVersion in affectedBundleVersions) { if (buildFailed) { TaskRunnerContext.SetBundleVersionState(affectedBundleVersion.Id, BundleState.Error); continue; } TaskRunnerContext.SetBundleVersionState(affectedBundleVersion.Id, BundleState.Idle); } if (bundleVersion.ProjectVersions.All(pv => TaskRunnerContext.GetProjectVersionState(pv.Id) == ProjectState.Idle)) { bundleVersion.SetStringProperty("LastBuildDuration", (DateTime.UtcNow - buildStartDate).TotalSeconds.ToString(CultureInfo.InvariantCulture)); } entities.SaveChanges(); } }
private static void PackageBundles(AspNetDeployEntities entities) { List <BundleVersion> bundleVersions = entities.BundleVersion .Include("ProjectVersions.SourceControlVersion.Properties") .Where(bv => !bv.IsDeleted) .ToList(); List <ProjectVersion> projectVersions = bundleVersions .SelectMany(scv => scv.ProjectVersions) .Where(pv => pv.ProjectType.HasFlag(ProjectType.WindowsApplication) || pv.ProjectType.HasFlag(ProjectType.Database) || pv.ProjectType.HasFlag(ProjectType.ZipArchive) || pv.ProjectType.HasFlag(ProjectType.GulpFile) || pv.ProjectType.HasFlag(ProjectType.Service) || pv.ProjectType.HasFlag(ProjectType.Console) || pv.ProjectType.HasFlag(ProjectType.Web) ) .Where(pv => pv.SourceControlVersion.GetStringProperty("Revision") != pv.GetStringProperty("LastPackageRevision")) .ToList(); List <BundleVersion> bundleVersionsToPack = projectVersions .SelectMany(pv => pv.BundleVersions) .Distinct() .Where(bv => bv.ProjectVersions.All( pv => pv.SourceControlVersion.ArchiveState == SourceControlVersionArchiveState.Normal && ( pv.ProjectType == ProjectType.ZipArchive || pv.GetStringProperty("LastBuildResult") == "Done") )) .ToList(); List <BundleVersion> errorBundles = new List <BundleVersion>(); bundleVersionsToPack.ForEach(bundleVersion => { /*if (bundleVersion.ProjectVersions.Any( pv => * TaskRunnerContext.GetProjectVersionState(pv.Id) != ProjectState.Idle || * pv.GetStringProperty("LastBuildResult") != "Done")) * { * return; * }*/ TaskRunnerContext.SetBundleVersionState(bundleVersion.Id, BundleState.Packaging); try { PackageJob job = new PackageJob(); job.Start(bundleVersion.Id); } catch (Exception e) { TaskRunnerContext.SetBundleVersionState(bundleVersion.Id, BundleState.Error); Factory.GetInstance <ILoggingService>().Log(e, null); errorBundles.Add(bundleVersion); if (e.IsCritical()) { throw; } } }); bundleVersionsToPack .Except(errorBundles) .ToList() .ForEach(bv => TaskRunnerContext.SetBundleVersionState(bv.Id, BundleState.Idle)); }