Beispiel #1
0
        public static async Task <string> DownloadAndImportAsync(IVsoConnectionInfo connectionInfo, ILogSink logger, string teamProject, string buildNumber, string buildDefinitionName, IOperationExecutionContext context, string artifactName)
        {
            var downloader = new ArtifactDownloader(connectionInfo, logger);

            using (var artifact = await downloader.DownloadAsync(teamProject, buildNumber, buildDefinitionName, artifactName).ConfigureAwait(false))
            {
                logger.LogInformation("Downloading artifact file from VSO and importing into BuildMaster artifact library...");

                var shim = new BuildMasterContextShim(context);

                await SDK.CreateArtifactAsync(
                    applicationId : shim.ApplicationId,
                    releaseNumber : shim.ReleaseNumber,
                    buildNumber : shim.BuildNumber,
                    deployableId : shim.DeployableId,
                    executionId : null,
                    artifactName : artifact.Name,
                    artifactData : artifact.Content,
                    overwrite : true
                    ).ConfigureAwait(false);

                logger.LogInformation($"{artifact.Name} artifact imported.");

                return(artifact.BuildNumber);
            }
        }
        public async Task <string> ImportAsync()
        {
            this.Logger.LogInformation($"Importing artifact \"{this.ArtifactName}\" from TeamCity...");

            if (this.BuildConfigurationId == null)
            {
                if (this.BuildConfigurationName != null && this.ProjectName != null)
                {
                    await SetBuildConfigurationIdFromName().ConfigureAwait(false);
                }
                else
                {
                    throw new ExecutionFailureException("If BuildConfigurationId is not specified directly, a project name and configuration name are required.");
                }
            }

            if (string.IsNullOrEmpty(this.BuildNumber))
            {
                this.Logger.LogDebug("BuildNumber was not specified, using lastSuccessful...");
                this.BuildNumber = "lastSuccessful";
            }

            string relativeUrl = string.Format("repository/download/{0}/{1}/{2}", this.BuildConfigurationId, this.BuildNumber, this.ArtifactName);

            if (!string.IsNullOrEmpty(this.BranchName))
            {
                this.Logger.LogDebug("Branch name was specified: " + this.BranchName);
                relativeUrl += "?branch=" + Uri.EscapeDataString(this.BranchName);
            }

            this.Logger.LogDebug(string.Format("Importing TeamCity artifact \"{0}\" from {1}...", this.ArtifactName, this.ConnectionInfo.GetApiUrl() + relativeUrl));

            string tempFile = null;

            try
            {
                using (var client = new TeamCityWebClient(this.ConnectionInfo))
                {
                    tempFile = Path.GetTempFileName();
                    this.Logger.LogDebug($"Downloading temp file to \"{tempFile}\"...");
                    try
                    {
                        await client.DownloadFileTaskAsync(relativeUrl, tempFile).ConfigureAwait(false);
                    }
                    catch (WebException wex)
                    {
                        var response = wex.Response as HttpWebResponse;
                        if (response != null && response.StatusCode == HttpStatusCode.NotFound)
                        {
                            this.Logger.LogWarning("The TeamCity request returned a 404 - this could mean that the branch name, build number, or build configuration is invalid.");
                        }

                        throw;
                    }
                }

                this.Logger.LogInformation("Importing artifact into BuildMaster...");
                using (var file = File.OpenRead(tempFile))
                {
                    await SDK.CreateArtifactAsync(
                        applicationId : (int)this.Context.ApplicationId,
                        releaseNumber : this.Context.ReleaseNumber,
                        buildNumber : this.Context.BuildNumber,
                        deployableId : this.Context.DeployableId,
                        executionId : this.Context.ExecutionId,
                        artifactName : TrimWhitespaceAndZipExtension(this.ArtifactName),
                        artifactData : file,
                        overwrite : true
                        );
                }
            }
            finally
            {
                if (tempFile != null)
                {
                    this.Logger.LogDebug("Removing temp file...");
                    FileEx.Delete(tempFile);
                }
            }

            this.Logger.LogInformation(this.ArtifactName + " artifact imported.");

            return(await this.GetActualBuildNumber().ConfigureAwait(false));
        }
Beispiel #3
0
        public async Task <string> ImportAsync()
        {
            this.Logger.LogInformation($"Importing artifact \"{this.ArtifactName}\" from Jenkins...");

            string zipFileName        = null;
            string jenkinsBuildNumber = await this.ResolveJenkinsBuildNumber().ConfigureAwait(false);

            if (string.IsNullOrEmpty(jenkinsBuildNumber))
            {
                this.Logger.LogError($"An error occurred attempting to resolve Jenkins build number \"{this.BuildNumber}\". "
                                     + $"This can mean that the special build type was not found, there are no builds for job \"{this.JobName}\", "
                                     + "or that the job was not found or is disabled."
                                     );

                return(null);
            }

            try
            {
                this.Logger.LogInformation($"Importing {this.ArtifactName} from {this.JobName}...");
                var client = new JenkinsClient(this.ConnectionInfo, this.Logger);

                zipFileName = Path.GetTempFileName();
                this.Logger.LogDebug("Temp file: " + zipFileName);

                this.Logger.LogDebug("Downloading artifact...");
                await client.DownloadArtifactAsync(this.JobName, jenkinsBuildNumber, zipFileName).ConfigureAwait(false);

                this.Logger.LogInformation("Artifact downloaded.");

                using (var file = FileEx.Open(zipFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    await SDK.CreateArtifactAsync(
                        (int)this.Context.ApplicationId,
                        this.Context.ReleaseNumber,
                        this.Context.BuildNumber,
                        this.Context.DeployableId,
                        this.Context.ExecutionId,
                        TrimWhitespaceAndZipExtension(this.ArtifactName),
                        file,
                        true
                        ).ConfigureAwait(false);
                }
            }
            finally
            {
                try
                {
                    if (zipFileName != null)
                    {
                        this.Logger.LogDebug("Removing temp file...");
                        FileEx.Delete(zipFileName);
                    }
                }
                catch (Exception ex)
                {
                    this.Logger.LogWarning("Error deleting temp file:" + ex.Message);
                }
            }

            this.Logger.LogInformation(this.ArtifactName + " artifact imported.");

            return(jenkinsBuildNumber);
        }