protected override async Task ProcessCommandInternalAsync( AgentTaskPluginExecutionContext context, string targetPath, string artifactName, CancellationToken token) { string hostType = context.Variables.GetValueOrDefault("system.hosttype")?.Value; if (!string.Equals(hostType, "Build", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException( StringUtil.Loc("CannotUploadFromCurrentEnvironment", hostType ?? string.Empty)); } // Project ID Guid projectId = new Guid(context.Variables.GetValueOrDefault(BuildVariables.TeamProjectId)?.Value ?? Guid.Empty.ToString()); ArgUtil.NotEmpty(projectId, nameof(projectId)); // Build ID string buildIdStr = context.Variables.GetValueOrDefault(BuildVariables.BuildId)?.Value ?? string.Empty; if (!int.TryParse(buildIdStr, out int buildId)) { // This should not happen since the build id comes from build environment. But a user may override that so we must be careful. throw new ArgumentException(StringUtil.Loc("BuildIdIsNotValid", buildIdStr)); } string fullPath = Path.GetFullPath(targetPath); bool isFile = File.Exists(fullPath); bool isDir = Directory.Exists(fullPath); if (!isFile && !isDir) { // if local path is neither file nor folder throw new FileNotFoundException(StringUtil.Loc("PathNotExist", targetPath)); } else if (isDir && Directory.EnumerateFiles(fullPath, "*", SearchOption.AllDirectories).FirstOrDefault() == null) { // if local path is a folder which contains nothing throw new ArgumentException(StringUtil.Loc("DirectoryIsEmptyForArtifact", fullPath, artifactName)); } // Upload to VSTS BlobStore, and associate the artifact with the build. context.Output(StringUtil.Loc("UploadingBuildDrop", fullPath, buildId)); BuildDropServer server = new BuildDropServer(); await server.UploadDropArtifactAsync(context, projectId, buildId, artifactName, fullPath, token); context.Output(StringUtil.Loc("UploadArtifactFinished")); }
protected override async Task ProcessCommandInternalAsync( AgentTaskPluginExecutionContext context, string targetPath, string artifactName, CancellationToken token) { // Create target directory if absent string fullPath = Path.GetFullPath(targetPath); bool isDir = Directory.Exists(fullPath); if (!isDir) { Directory.CreateDirectory(fullPath); } // Project ID // TODO: use a constant for project id, which is currently defined in Microsoft.VisualStudio.Services.Agent.Constants.Variables.System.TeamProjectId (Ting) string guidStr = context.Variables.GetValueOrDefault("system.teamProjectId")?.Value; Guid.TryParse(guidStr, out Guid projectId); ArgUtil.NotEmpty(projectId, nameof(projectId)); // Build ID int buildId = 0; string buildIdStr = context.GetInput(ArtifactEventProperties.BuildId, required: false); // Determine the build id if (Int32.TryParse(buildIdStr, out buildId) && buildId != 0) { // A) Build Id provided by user input context.Output(StringUtil.Loc("DownloadingFromBuild", buildId)); } else { // B) Build Id provided by environment buildIdStr = context.Variables.GetValueOrDefault(BuildVariables.BuildId)?.Value ?? string.Empty; if (int.TryParse(buildIdStr, out buildId) && buildId != 0) { context.Output(StringUtil.Loc("DownloadingFromBuild", buildId)); } else { string hostType = context.Variables.GetValueOrDefault("system.hosttype")?.Value; if (string.Equals(hostType, "Release", StringComparison.OrdinalIgnoreCase) || string.Equals(hostType, "DeploymentGroup", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException(StringUtil.Loc("BuildIdIsNotAvailable", hostType ?? string.Empty)); } else if (!string.Equals(hostType, "Build", StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException(StringUtil.Loc("CannotDownloadFromCurrentEnvironment", hostType ?? string.Empty)); } else { // This should not happen since the build id comes from build environment. But a user may override that so we must be careful. throw new ArgumentException(StringUtil.Loc("BuildIdIsNotValid", buildIdStr)); } } } // Download from VSTS BlobStore context.Output(StringUtil.Loc("DownloadArtifactTo", targetPath)); BuildDropServer server = new BuildDropServer(); await server.DownloadDropArtifactAsync(context, projectId, buildId, artifactName, targetPath, token); context.Output(StringUtil.Loc("DownloadArtifactFinished")); }