From 4116edc1159211cb4da4e00c4aa7c4e8ecc1d7c8 Mon Sep 17 00:00:00 2001 From: Omesh Pandita Date: Fri, 5 Jan 2018 16:25:57 +0530 Subject: [PATCH] download jenkins artifact as zip if downloading all files. --- .../jenkinsdownloadartifacts.ts | 62 +++++++++++++++++-- Tasks/JenkinsDownloadArtifacts/package.json | 6 +- Tasks/JenkinsDownloadArtifacts/task.json | 4 +- Tasks/JenkinsDownloadArtifacts/task.loc.json | 4 +- 4 files changed, 65 insertions(+), 11 deletions(-) diff --git a/Tasks/JenkinsDownloadArtifacts/jenkinsdownloadartifacts.ts b/Tasks/JenkinsDownloadArtifacts/jenkinsdownloadartifacts.ts index b52bc1724458..c04536da45fb 100644 --- a/Tasks/JenkinsDownloadArtifacts/jenkinsdownloadartifacts.ts +++ b/Tasks/JenkinsDownloadArtifacts/jenkinsdownloadartifacts.ts @@ -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'; @@ -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) || "**"; @@ -78,6 +91,24 @@ function publishEvent(feature, properties: any): void { } } +export async function unzip(zipLocation: string, unzipLocation: string): Promise { + await new Promise(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')); @@ -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)); @@ -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)); diff --git a/Tasks/JenkinsDownloadArtifacts/package.json b/Tasks/JenkinsDownloadArtifacts/package.json index e0c504a64646..503b87127e35 100644 --- a/Tasks/JenkinsDownloadArtifacts/package.json +++ b/Tasks/JenkinsDownloadArtifacts/package.json @@ -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" } } diff --git a/Tasks/JenkinsDownloadArtifacts/task.json b/Tasks/JenkinsDownloadArtifacts/task.json index 44eb15de71dc..bc754b1ed9ee 100644 --- a/Tasks/JenkinsDownloadArtifacts/task.json +++ b/Tasks/JenkinsDownloadArtifacts/task.json @@ -17,8 +17,8 @@ "demands": [], "version": { "Major": 1, - "Minor": 128, - "Patch": 4 + "Minor": 129, + "Patch": 1 }, "groups": [ { diff --git a/Tasks/JenkinsDownloadArtifacts/task.loc.json b/Tasks/JenkinsDownloadArtifacts/task.loc.json index dba74f4820a8..3417b7651bf1 100644 --- a/Tasks/JenkinsDownloadArtifacts/task.loc.json +++ b/Tasks/JenkinsDownloadArtifacts/task.loc.json @@ -17,8 +17,8 @@ "demands": [], "version": { "Major": 1, - "Minor": 128, - "Patch": 4 + "Minor": 129, + "Patch": 1 }, "groups": [ {