Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DownloadBuildArtifactsV0] Add clean up logic of destination folder #15025

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"loc.input.help.itemPattern": "Specify files to be downloaded as multi line minimatch pattern. [More Information](https://aka.ms/minimatchexamples) <p>The default pattern (\\*\\*) will download all files across all artifacts in the build if \"Specific files\" option is selected. To download all files within artifact drop use drop/**.</p>",
"loc.input.label.downloadPath": "Destination directory",
"loc.input.help.downloadPath": "Path on the agent machine where the artifacts will be downloaded",
"loc.input.label.cleanDestinationFolder": "Clean destination folder",
"loc.input.help.cleanDestinationFolder": "Delete all existing files in destination folder before artifact download",
"loc.input.label.parallelizationLimit": "Parallelization limit",
"loc.input.help.parallelizationLimit": "Number of files to download simultaneously",
"loc.input.label.checkDownloadedFiles": "Check downloaded files",
Expand Down Expand Up @@ -63,5 +65,6 @@
"loc.messages.IntegrityCheckNotPassed": "Artifact items integrity check failed",
"loc.messages.IntegrityCheckPassed": "Artifact items integrity check successfully finished",
"loc.messages.TarExtractionNotSupportedInWindows": "Tar extraction is not supported on Windows",
"loc.messages.NoTarsFound": "No tar archives were found to extract"
"loc.messages.NoTarsFound": "No tar archives were found to extract",
"loc.messages.CleaningDestinationFolder": "Cleaning destination folder: %s"
}
32 changes: 32 additions & 0 deletions Tasks/DownloadBuildArtifactsV0/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ async function main(): Promise<void> {
var allowPartiallySucceededBuilds: boolean = tl.getBoolInput("allowPartiallySucceededBuilds", false);
var branchName: string = tl.getInput("branchName", false);
var downloadPath: string = path.normalize(tl.getInput("downloadPath", true));
var cleanDestinationFolder: boolean = tl.getBoolInput("cleanDestinationFolder", false);
var downloadType: string = tl.getInput("downloadType", true);
var tagFiltersInput: string = tl.getInput("tags", false);
var tagFilters = [];
Expand Down Expand Up @@ -109,6 +110,37 @@ async function main(): Promise<void> {
});
var artifacts = [];

// Clean destination folder if requested
if (cleanDestinationFolder) {
This conversation was marked as resolved.
Show resolved Hide resolved
console.log(tl.loc('CleaningDestinationFolder', downloadPath));

// stat the destination folder (downloadPath) path
let destinationFolderStats: tl.FsStats;
try {
destinationFolderStats = tl.stats(downloadPath);
}
catch (err) {
This conversation was marked as resolved.
Show resolved Hide resolved
if (err.code != 'ENOENT') {
This conversation was marked as resolved.
Show resolved Hide resolved
throw err;
}
}

if (destinationFolderStats) {
if (destinationFolderStats.isDirectory()) {
// delete the child items
fs.readdirSync(downloadPath)
.forEach((item: string) => {
let itemPath = path.join(downloadPath, item);
tl.rmRF(itemPath);
});
}
else {
// destination folder is not a directory. delete it.
tl.rmRF(downloadPath);
}
}
}

if (isCurrentBuild) {
projectId = tl.getVariable("System.TeamProjectId");
definitionId = '';
Expand Down
14 changes: 11 additions & 3 deletions Tasks/DownloadBuildArtifactsV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 188,
"Patch": 3
"Minor": 190,
"Patch": 0
},
"groups": [
{
Expand Down Expand Up @@ -170,6 +170,13 @@
"required": true,
"helpMarkDown": "Path on the agent machine where the artifacts will be downloaded"
},
{
"name": "cleanDestinationFolder",
"type":"boolean",
"label": "Clean destination folder",
"defaultValue": false,
"helpMarkDown": "Delete all existing files in destination folder before artifact download"
},
{
"name": "parallelizationLimit",
"type": "string",
Expand Down Expand Up @@ -288,7 +295,8 @@
"IntegrityCheckNotPassed": "Artifact items integrity check failed",
"IntegrityCheckPassed": "Artifact items integrity check successfully finished",
"TarExtractionNotSupportedInWindows": "Tar extraction is not supported on Windows",
"NoTarsFound": "No tar archives were found to extract"
"NoTarsFound": "No tar archives were found to extract",
"CleaningDestinationFolder": "Cleaning destination folder: %s"
},
"outputVariables": [
{
Expand Down
14 changes: 11 additions & 3 deletions Tasks/DownloadBuildArtifactsV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 188,
"Patch": 3
"Minor": 190,
"Patch": 0
},
"groups": [
{
Expand Down Expand Up @@ -170,6 +170,13 @@
"required": true,
"helpMarkDown": "ms-resource:loc.input.help.downloadPath"
},
{
"name": "cleanDestinationFolder",
"type": "boolean",
"label": "ms-resource:loc.input.label.cleanDestinationFolder",
"defaultValue": false,
"helpMarkDown": "ms-resource:loc.input.help.cleanDestinationFolder"
},
{
"name": "parallelizationLimit",
"type": "string",
Expand Down Expand Up @@ -288,7 +295,8 @@
"IntegrityCheckNotPassed": "ms-resource:loc.messages.IntegrityCheckNotPassed",
"IntegrityCheckPassed": "ms-resource:loc.messages.IntegrityCheckPassed",
"TarExtractionNotSupportedInWindows": "ms-resource:loc.messages.TarExtractionNotSupportedInWindows",
"NoTarsFound": "ms-resource:loc.messages.NoTarsFound"
"NoTarsFound": "ms-resource:loc.messages.NoTarsFound",
"CleaningDestinationFolder": "ms-resource:loc.messages.CleaningDestinationFolder"
},
"outputVariables": [
{
Expand Down