Skip to content

Commit

Permalink
[DownloadBuildArtifactsV0] Add clean up logic of destination folder (#…
Browse files Browse the repository at this point in the history
…15025)

* Add clean logic

* Add comments to the code

* Bump version and fix interface

* Resolve comments
  • Loading branch information
Tatyana Kostromskaya authored Jul 12, 2021
1 parent a61b8be commit e6aab16
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 9 deletions.
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,7 @@
"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",
"loc.messages.NoFolderToClean": "Specified cleaning folder was not found. Nothing to clean"
}
38 changes: 37 additions & 1 deletion Tasks/DownloadBuildArtifactsV0/file_helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Stats, statSync as getFile } from 'fs';
import { Stats, statSync as getFile, readdirSync } from 'fs';
import * as path from 'path';

import * as tl from 'azure-pipelines-task-lib/task';
Expand Down Expand Up @@ -102,3 +102,39 @@ function extractTar(tarArchivePath: string, extractedFilesDir: string): void {
throw new Error(`Couldn't extract artifact files from a tar archive: ${tarExecResult.error}`);
}
}

/**
* Removes content from specified folder path
* @param {string} folderToClean - path to the folder to clean up content
* @throws File system exeptions
* @returns void
*/
export function cleanUpFolder(folderToClean: string): void {
console.log(tl.loc('CleaningDestinationFolder', folderToClean));

// stat the specified folder path
let destinationFolderStats: tl.FsStats;
try {
destinationFolderStats = tl.stats(folderToClean);
} catch (err) {
if (err.code != 'ENOENT') {
throw err;
} else {
tl.warning(tl.loc('NoFolderToClean'));
}
}

if (destinationFolderStats) {
if (destinationFolderStats.isDirectory()) {
// delete the child items
readdirSync(folderToClean)
.forEach((item: string) => {
let itemPath = path.join(folderToClean, item);
tl.rmRF(itemPath);
});
} else {
// specified folder is not a directory. delete it.
tl.rmRF(folderToClean);
}
}
}
8 changes: 7 additions & 1 deletion Tasks/DownloadBuildArtifactsV0/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { DownloadHandlerFilePath } from './DownloadHandlers/DownloadHandlerFileP

import { resolveParallelProcessingLimit } from './download_helper';

import { extractTarsIfPresent } from './file_helper';
import { extractTarsIfPresent, cleanUpFolder } from './file_helper';

var taskJson = require('./task.json');

Expand Down 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,11 @@ async function main(): Promise<void> {
});
var artifacts = [];

// Clean destination folder if requested
if (cleanDestinationFolder) {
cleanUpFolder(downloadPath);
}

if (isCurrentBuild) {
projectId = tl.getVariable("System.TeamProjectId");
definitionId = '';
Expand Down
15 changes: 12 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,9 @@
"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",
"NoFolderToClean": "Specified cleaning folder was not found. Nothing to clean"
},
"outputVariables": [
{
Expand Down
15 changes: 12 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,9 @@
"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",
"NoFolderToClean": "ms-resource:loc.messages.NoFolderToClean"
},
"outputVariables": [
{
Expand Down

0 comments on commit e6aab16

Please sign in to comment.