Example #1
0
 private BuildMasterAgent CreateAgent()
 {
     if (this.ServerId == null)
     {
         return(BuildMasterAgent.CreateLocalAgent());
     }
     else
     {
         return(BuildMasterAgent.Create((int)this.ServerId));
     }
 }
Example #2
0
            public BuildMasterAgent CreateAgent()
            {
                var vars = DB.Variables_GetVariablesAccessibleFromScope(
                    Variable_Name: "TfsDefaultServerName",
                    Application_Id: this.ApplicationId,
                    IncludeSystemVariables_Indicator: true
                    );

                var variable = (from v in vars
                                orderby v.Application_Id != null ? 0 : 1
                                select v).FirstOrDefault();

                if (variable == null)
                {
                    return(BuildMasterAgent.CreateLocalAgent());
                }
                else
                {
                    return(BuildMasterAgent.Create(InedoLib.UTF8Encoding.GetString(variable.Variable_Value)));
                }
            }
Example #3
0
        internal TfsBuildInfo GetBuildInfo(string teamProject, string buildDefinition, string buildNumber, bool includeUnsuccessful)
        {
            var agent = this.ServerId != null?BuildMasterAgent.Create((int)this.ServerId) : BuildMasterAgent.CreateLocalAgent();

            using (agent)
            {
                var methodExecuter = agent.GetService <IRemoteMethodExecuter>();
                return(methodExecuter.InvokeFunc(this.GetBuildInfoInternal, teamProject, buildDefinition, buildNumber, includeUnsuccessful));
            }
        }
Example #4
0
        public override void Import(IBuildImporterContext context)
        {
            var configurer = (TfsConfigurer)this.GetExtensionConfigurer();

            this.LogDebug($"Searching for build {this.TfsBuildNumber} for team project {this.TeamProject} definition {this.BuildDefinition}...");
            var tfsBuild = configurer.GetBuildInfo(this.TeamProject, this.BuildDefinition, this.TfsBuildNumber, this.IncludeUnsuccessful);

            if (tfsBuild == null)
            {
                this.LogError("Query did not return any builds.");
                return;
            }

            this.LogInformation("TFS Build Number: " + tfsBuild.BuildNumber);

            if (string.IsNullOrWhiteSpace(tfsBuild.DropLocation))
            {
                this.LogError("TFS configuration error: the selected build definition does not have a drop location specified.");
                return;
            }

            this.LogInformation("Drop location: " + tfsBuild.DropLocation);

            var agent = configurer.ServerId != null?BuildMasterAgent.Create((int)configurer.ServerId) : BuildMasterAgent.CreateLocalAgent();

            using (agent)
            {
                var fileOps = agent.GetService <IFileOperationsExecuter>();

                this.LogDebug("Querying drop location...");

                var directoryResult = fileOps.GetDirectoryEntry(
                    new GetDirectoryEntryCommand
                {
                    Path            = tfsBuild.DropLocation,
                    Recurse         = true,
                    IncludeRootPath = true
                }
                    );
                var exception = directoryResult.Exceptions.FirstOrDefault();
                if (exception != null)
                {
                    throw exception;
                }

                var matches = Util.Files.Comparison.GetMatches(tfsBuild.DropLocation, directoryResult.Entry, new[] { "*" })
                              .Where(e => !IsSamePath(e.Path, tfsBuild.DropLocation))
                              .ToList();

                if (!matches.Any())
                {
                    this.LogWarning("No files were found in the drop folder.");
                    return;
                }

                this.LogDebug($"Creating {this.ArtifactName} artifact...");
                var artifactId = new ArtifactIdentifier(context.ApplicationId, context.ReleaseNumber, context.BuildNumber, context.DeployableId, this.ArtifactName);
                using (var artifact = new ArtifactBuilder(artifactId))
                {
                    artifact.RootPath = tfsBuild.DropLocation;

                    foreach (var match in matches)
                    {
                        artifact.Add(match, fileOps);
                    }

                    artifact.Commit();
                }
            }

            if (this.CreateBuildNumberVariable)
            {
                this.LogDebug($"Setting $TfsBuildNumber build variable to {tfsBuild.BuildNumber}...");
                DB.Variables_CreateOrUpdateVariableDefinition(
                    Variable_Name: "TfsBuildNumber",
                    Environment_Id: null,
                    ServerRole_Id: null,
                    Server_Id: null,
                    ApplicationGroup_Id: null,
                    Application_Id: context.ApplicationId,
                    Deployable_Id: null,
                    Release_Number: context.ReleaseNumber,
                    Build_Number: context.BuildNumber,
                    Execution_Id: null,
                    Promotion_Id: null,
                    Value_Text: tfsBuild.BuildNumber,
                    Sensitive_Indicator: YNIndicator.No
                    );

                this.LogInformation("$TfsBuildNumber build variable set to: " + tfsBuild.BuildNumber);
            }
        }
Example #5
0
        public override void Import(IBuildImporterContext context)
        {
            var configurer = (NuGetConfigurer)this.GetExtensionConfigurer();

            if (configurer.AlwaysClearNuGetCache)
            {
                this.LogDebug("Clearing NuGet cache...");
                if (NuGetConfigurer.ClearCache())
                {
                    this.LogDebug("Cache cleared!");
                }
                else
                {
                    this.LogWarning("Error clearing cache; a file may be locked.");
                }
            }

            var nugetExe = configurer != null ? configurer.NuGetExe : null;

            if (string.IsNullOrEmpty(nugetExe))
            {
                nugetExe = Path.Combine(Path.GetDirectoryName(typeof(NuGetBuildImporter).Assembly.Location), "nuget.exe");
            }

            var packageSource = Util.CoalesceStr(this.PackageSource, configurer != null ? configurer.PackageSource : null);

            var args = "install \"" + this.PackageId + "\" -ExcludeVersion -NoCache";

            if (!string.IsNullOrEmpty(this.PackageVersion))
            {
                args += " -Version \"" + this.PackageVersion + "\"";
            }
            if (this.IncludePrerelease)
            {
                args += " -Prerelease";
            }
            if (!string.IsNullOrEmpty(packageSource))
            {
                args += " -Source \"" + packageSource + "\"";
            }

            var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            try
            {
                Directory.CreateDirectory(tempPath);

                args += " -OutputDirectory \"" + tempPath + "\"";

                if (!string.IsNullOrWhiteSpace(this.AdditionalArguments))
                {
                    args += " " + this.AdditionalArguments;
                }

                this.LogDebug($"Executing {nugetExe} {args}");
                this.LogInformation("Executing NuGet...");

                using (var process = new LocalProcess(new RemoteProcessStartInfo {
                    FileName = nugetExe, Arguments = args
                }))
                {
                    process.OutputDataReceived += this.Process_OutputDataReceived;
                    process.ErrorDataReceived  += this.Process_ErrorDataReceived;
                    process.Start();
                    process.WaitForExit();

                    if (process.ExitCode != 0)
                    {
                        this.LogError($"NuGet failed with exit code {process.ExitCode}.");
                        return;
                    }
                }

                this.LogInformation("NuGet indicated successful package install.");

                var packageRootPath = Path.Combine(tempPath, this.PackageId);
                var artifactName    = this.PackageId;

                if (this.CaptureIdAndVersion || this.IncludeVersionInArtifactName)
                {
                    try
                    {
                        var nupkgPath = Path.Combine(packageRootPath, this.PackageId + ".nupkg");
                        this.LogDebug($"Attempting to gather metadata from {nupkgPath}...");
                        var nuspec = NuGetPackage.ReadFromNupkgFile(nupkgPath);

                        var packageId      = nuspec.Id;
                        var packageVersion = nuspec.Version.OriginalString;

                        if (this.CaptureIdAndVersion)
                        {
                            this.LogDebug("Setting $ImportedPackageId = " + packageId);
                            SetBuildVariable(context, "ImportedPackageId", packageId);

                            this.LogDebug("Setting $ImportedPackageVersion = " + packageVersion);
                            SetBuildVariable(context, "ImportedPackageVersion", packageVersion);
                        }

                        if (this.IncludeVersionInArtifactName)
                        {
                            artifactName = packageId + "." + packageVersion;
                        }
                    }
                    catch (Exception ex)
                    {
                        this.LogError("Could not read package metadata: " + ex.ToString());
                        return;
                    }
                }

                var rootCapturePath = Path.Combine(packageRootPath, (this.PackageArtifactRoot ?? string.Empty).Replace('/', '\\').TrimStart('/', '\\'));
                this.LogDebug($"Capturing files in {rootCapturePath}...");

                var rootEntry = Util.Files.GetDirectoryEntry(
                    new GetDirectoryEntryCommand
                {
                    Path            = rootCapturePath,
                    IncludeRootPath = true,
                    Recurse         = true
                }
                    ).Entry;

                using (var agent = BuildMasterAgent.CreateLocalAgent())
                {
                    var fileOps    = agent.GetService <IFileOperationsExecuter>();
                    var matches    = Util.Files.Comparison.GetMatches(rootCapturePath, rootEntry, new[] { "!\\" + this.PackageId + ".nupkg", "*" });
                    var artifactId = new ArtifactIdentifier(context.ApplicationId, context.ReleaseNumber, context.BuildNumber, context.DeployableId, artifactName);

                    this.LogInformation($"Creating artifact {artifactName}...");
                    using (var artifact = new ArtifactBuilder(artifactId))
                    {
                        artifact.RootPath = rootCapturePath;

                        foreach (var match in matches)
                        {
                            artifact.Add(match, fileOps);
                        }

                        artifact.Commit();
                    }

                    this.LogInformation("Artifact created.");
                }
            }
            finally
            {
                try
                {
                    DirectoryEx.Delete(tempPath);
                }
                catch
                {
                }
            }
        }