Skip to content

Commit

Permalink
download jenkins artifact as zip if downloading all files.
Browse files Browse the repository at this point in the history
  • Loading branch information
omeshp committed Jan 17, 2018
1 parent cb24bb3 commit 4116edc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
62 changes: 57 additions & 5 deletions Tasks/JenkinsDownloadArtifacts/jenkinsdownloadartifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { AzureStorageArtifactDownloader } from "./AzureStorageArtifacts/AzureSto
import { ArtifactDetailsDownloader } from "./ArtifactDetails/ArtifactDetailsDownloader";
import { JenkinsRestClient, JenkinsJobDetails } from "./ArtifactDetails/JenkinsRestClient"

var DecompressZip = require('decompress-zip');
var fsExtra = require('fs-extra');
var taskJson = require('./task.json');

const area: string = 'JenkinsDownloadArtifacts';
Expand All @@ -32,6 +34,17 @@ async function getArtifactsFromUrl(artifactQueryUrl: string, strictSSL: boolean,
await downloader.processItems(webProvider, localFileProvider, downloaderOptions);
}

async function getZipFromUrl(artifactArchiveUrl: string, strictSSL: boolean, localPathRoot: string, handler: handlers.BasicCredentialHandler) {
console.log(tl.loc('ArtifactDownloadUrl', artifactArchiveUrl));

var downloaderOptions = configureDownloaderOptions();
var downloader = new engine.ArtifactEngine();
var zipProvider = new providers.ZipProvider(artifactArchiveUrl, handler, { ignoreSslError: false });
var filesystemProvider = new providers.FilesystemProvider(localPathRoot);

await downloader.processItems(zipProvider, filesystemProvider, downloaderOptions)
}

function configureDownloaderOptions(): engine.ArtifactEngineOptions {
var downloaderOptions = new engine.ArtifactEngineOptions();
downloaderOptions.itemPattern = tl.getInput('itemPattern', false) || "**";
Expand Down Expand Up @@ -78,6 +91,24 @@ function publishEvent(feature, properties: any): void {
}
}

export async function unzip(zipLocation: string, unzipLocation: string): Promise<void> {
await new Promise<void>(function (resolve, reject) {
tl.debug('Extracting ' + zipLocation + ' to ' + unzipLocation);

var unzipper = new DecompressZip(zipLocation);
unzipper.on('error', err => {
return reject(tl.loc("ExtractionFailed", err))
});
unzipper.on('extract', log => {
tl.debug('Extracted ' + zipLocation + ' to ' + unzipLocation + ' successfully');
return resolve();
});
unzipper.extract({
path: unzipLocation
});
});
}

async function doWork() {
try {
tl.setResourcePath(path.join(__dirname, 'task.json'));
Expand All @@ -100,10 +131,10 @@ async function doWork() {
switch (artifactProvider.toLowerCase()) {
case "azurestorage":
let azureDownloader = new AzureStorageArtifactDownloader(tl.getInput('ConnectedServiceNameARM', true),
tl.getInput('storageAccountName', true),
tl.getInput('containerName', true),
tl.getInput('storageAccountName', true),
tl.getInput('containerName', true),
tl.getInput('commonVirtualPath', false));
await azureDownloader.downloadArtifacts(localPathRoot, tl.getInput('itemPattern', false) || "**");
await azureDownloader.downloadArtifacts(localPathRoot, tl.getInput('itemPattern', false) || "**");
break;
default:
throw Error(tl.loc('ArtifactProviderNotSupported', artifactProvider));
Expand All @@ -124,8 +155,29 @@ async function doWork() {
};

var handler = new handlers.BasicCredentialHandler(username, password);

await getArtifactsFromUrl(artifactQueryUrl, strictSSL, localPathRoot, itemPattern, handler, variables);
if (!itemPattern || itemPattern === '**') {
const archiveUrl: string = `${serverEndpointUrl}/${jenkinsJobDetails.jobUrlInfix}/${jenkinsJobDetails.multiBranchPipelineUrlInfix}/${jenkinsJobDetails.buildId}/artifact/*zip*/`
var zipLocation = path.join(localPathRoot, "archive.zip");
await getZipFromUrl(archiveUrl, strictSSL, zipLocation, handler);

var unzipPromise = unzip(zipLocation, localPathRoot);
unzipPromise.catch((error) => {
throw error;
});

await unzipPromise;

if (tl.exist(zipLocation)) {
tl.rmRF(zipLocation);
}

fsExtra.move(path.join(localPathRoot, "archive"), localPathRoot).catch((error) => {
throw error;
});
}
else {
await getArtifactsFromUrl(artifactQueryUrl, strictSSL, localPathRoot, itemPattern, handler, variables);
}
}

console.log(tl.loc('ArtifactSuccessfullyDownloaded', localPathRoot));
Expand Down
6 changes: 4 additions & 2 deletions Tasks/JenkinsDownloadArtifacts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
"author": "Microsoft Corporation",
"license": "MIT",
"dependencies": {
"artifact-engine": "0.1.15",
"decompress-zip": "0.3.0",
"fs-extra": "5.0.0",
"request": "2.74.0",
"vsts-task-lib": "2.1.0",
"artifact-engine": "0.1.14"
"vsts-task-lib": "2.1.0"
}
}
4 changes: 2 additions & 2 deletions Tasks/JenkinsDownloadArtifacts/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"demands": [],
"version": {
"Major": 1,
"Minor": 128,
"Patch": 4
"Minor": 129,
"Patch": 1
},
"groups": [
{
Expand Down
4 changes: 2 additions & 2 deletions Tasks/JenkinsDownloadArtifacts/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"demands": [],
"version": {
"Major": 1,
"Minor": 128,
"Patch": 4
"Minor": 129,
"Patch": 1
},
"groups": [
{
Expand Down

0 comments on commit 4116edc

Please sign in to comment.