Skip to content

Commit

Permalink
[DownloadBuildArtifactTaskV1] Add clean up destination folder option (#…
Browse files Browse the repository at this point in the history
…3462)

* Migrate changes from tasks repo to plugin code

* Delete whitespace changes

* Update strings.json

* Add exception handling for case when destination directory does not exist

Co-authored-by: Anatoly Bolshakov <[email protected]>
  • Loading branch information
Tatyana Kostromskaya and Anatoly Bolshakov authored Jul 27, 2021
1 parent 390dc24 commit d98b6d2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
58 changes: 56 additions & 2 deletions src/Agent.Plugins/BuildArtifact/BuildArtifactPluginV1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ protected static class TaskProperties
public static readonly string ItemPattern = "itemPattern";
public static readonly string DownloadType = "downloadType";
public static readonly string DownloadPath = "downloadPath";
public static readonly string CleanDestinationFolder = "cleanDestinationFolder";
public static readonly string BuildId = "buildId";
public static readonly string RetryDownloadCount = "retryDownloadCount";
public static readonly string ParallelizationLimit = "parallelizationLimit";
Expand Down Expand Up @@ -89,6 +90,7 @@ protected override async Task ProcessCommandInternalAsync(
string specificBuildWithTriggering = context.GetInput(TaskProperties.SpecificBuildWithTriggering, required: false);
string buildVersionToDownload = context.GetInput(TaskProperties.BuildVersionToDownload, required: false);
string targetPath = context.GetInput(TaskProperties.DownloadPath, required: true);
string cleanDestinationFolder = context.GetInput(TaskProperties.CleanDestinationFolder, required: false);
string environmentBuildId = context.Variables.GetValueOrDefault(BuildVariables.BuildId)?.Value ?? string.Empty; // BuildID provided by environment.
string itemPattern = context.GetInput(TaskProperties.ItemPattern, required: false);
string projectName = context.GetInput(TaskProperties.Project, required: false);
Expand Down Expand Up @@ -128,6 +130,12 @@ protected override async Task ProcessCommandInternalAsync(
{
allowCanceledBuildsBool = false;
}

if (!bool.TryParse(cleanDestinationFolder, out var cleanDestinationFolderBool))
{
cleanDestinationFolderBool = false;
}

var resultFilter = GetResultFilter(allowPartiallySucceededBuildsBool, allowFailedBuildsBool, allowCanceledBuildsBool);

PipelineArtifactServer server = new PipelineArtifactServer(tracer);
Expand All @@ -141,7 +149,7 @@ protected override async Task ProcessCommandInternalAsync(
{
throw new ArgumentNullException(StringUtil.Loc("CannotBeNullOrEmpty"), "Project ID");
}

Guid projectId = Guid.Parse(projectIdStr);
ArgUtil.NotEmpty(projectId, nameof(projectId));

Expand Down Expand Up @@ -285,7 +293,10 @@ protected override async Task ProcessCommandInternalAsync(
}

string fullPath = this.CreateDirectoryIfDoesntExist(targetPath);

if (cleanDestinationFolderBool)
{
CleanDirectory(context, fullPath);
}
var downloadOption = downloadType == "single" ? DownloadOptions.SingleDownload : DownloadOptions.MultiDownload;

// Build artifacts always includes the artifact in the path name
Expand All @@ -307,6 +318,49 @@ private string CreateDirectoryIfDoesntExist(string targetPath)
return fullPath;
}

private void CleanDirectory(AgentTaskPluginExecutionContext context, string directoryPath)
{
FileAttributes dirAttributes;
context.Output(StringUtil.Loc("CleaningDestinationFolder", directoryPath));

try
{
dirAttributes = File.GetAttributes(directoryPath);
}
catch (Exception ex) when (ex is FileNotFoundException || ex is DirectoryNotFoundException)
{
context.Warning(StringUtil.Loc("NoFolderToClean", directoryPath));
return;
}

if (dirAttributes.HasFlag(FileAttributes.Directory))
{
bool isDirectoryEmpty = !Directory.EnumerateFileSystemEntries(directoryPath).Any();
if (isDirectoryEmpty)
{
context.Warning(StringUtil.Loc("NoFolderToClean", directoryPath));
return;
}

// delete the child items
DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
foreach (FileInfo file in directoryInfo.GetFiles())
{
file.Delete();
}

foreach (DirectoryInfo subDirectory in directoryInfo.GetDirectories())
{
subDirectory.Delete(true);
}
}
else
{
// specified folder is not a directory. Delete it.
File.Delete(directoryPath);
}
}

private async Task<int> GetPipelineIdAsync(AgentTaskPluginExecutionContext context, string pipelineDefinition, string buildVersionToDownload, string project, string[] tagFilters, BuildResult resultFilter = BuildResult.Succeeded, string branchName = null, CancellationToken cancellationToken = default(CancellationToken))
{
if(String.IsNullOrWhiteSpace(pipelineDefinition))
Expand Down
2 changes: 2 additions & 0 deletions src/Misc/layoutbin/en-US/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"CannotUploadFromCurrentEnvironment": "Cannot upload to a pipeline artifact from {0} environment.",
"CannotUploadSummary": "Cannot upload summary file, summary file location is not specified.",
"CheckoutTaskDisplayNameFormat": "Checkout {0}@{1} to {2}",
"CleaningDestinationFolder": "Cleaning destination folder: {0}",
"ClockSkewStopRetry": "Stopped retrying OAuth token request exception after {0} seconds.",
"CodeCoverageDataIsNull": "No coverage data found. Check the build errors/warnings for more details.",
"CodeCoveragePublishIsValidOnlyForBuild": "Publishing code coverage works only for 'build'.",
Expand Down Expand Up @@ -412,6 +413,7 @@
"NeedAdminForUnconfigWinServiceAgent": "Needs Administrator privileges for unconfiguring agent that running as windows service.",
"NetworkServiceNotFound": "Cannot find network service account",
"NoArtifactsFound": "No artifacts are available in the version '{0}'.",
"NoFolderToClean": "Specified cleaning folder was not found. Nothing to clean",
"NoRestart": "Restart the machine at a later time? (Y/N)",
"NoRestartSuggestion": "AutoLogon was enabled during agent configuration. It is recommended that the machine be restarted for AutoLogon settings to take effect.",
"NoResultFound": "No Result Found to Publish '{0}'.",
Expand Down

0 comments on commit d98b6d2

Please sign in to comment.