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

Internal: Handle release retries by not re-uploading artifact #1694

Merged
merged 6 commits into from
Oct 9, 2018
Merged
Changes from all 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
45 changes: 43 additions & 2 deletions scripts/azpipelines/upload-to-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ const fs = require("fs");
const path = require("path");
const azureStorage = require("azure-storage");
const { getManifest, getContainerName } = require("./utils");
const crypto = require("crypto");

const storageAccountName = process.env.AZURE_STORAGE_ACCOUNT;
const storageAccountKey = process.argv[2];
const attemptNumber = Number(process.env.Release_AttemptNumber);

console.log(`This is the ${attemptNumber} try to release`);

if (!storageAccountKey) {
console.error("No storage account key passed");
Expand All @@ -16,9 +20,24 @@ if (!storageAccountKey) {
console.log("Uploading to storage account:", storageAccountName);
const blobService = azureStorage.createBlobService(storageAccountName, storageAccountKey);

async function uploadToBlob(container, filename, blobName, override = false) {
console.log(`Uploading ${filename} ====> Container=${container}, Blobname=${blobName}`);
function computeFileMd5(filename) {
const data = fs.readFileSync(filename);
return crypto.createHash("md5").update(data).digest("base64");
}

async function getBlob(container, blobName) {
return new Promise((resolve, reject) => {
blobService.getBlobProperties(container, blobName, (error, result, response) => {
if (error) {
return reject(error);
}

resolve(result);
});
});
}

async function createBlobFromLocalFile(container, filename, blobName, override = false) {
const options = {};
if (!override) {
options.accessConditions = azureStorage.AccessCondition.generateIfNotExistsCondition();
Expand All @@ -27,8 +46,10 @@ async function uploadToBlob(container, filename, blobName, override = false) {
return new Promise((resolve, reject) => {
blobService.createBlockBlobFromLocalFile(container, blobName, filename, options,
(error, result, response) => {

if (error) {
reject(error);
return;
}

console.log("Uploaded", result, response);
Expand All @@ -37,6 +58,26 @@ async function uploadToBlob(container, filename, blobName, override = false) {
});
}

async function uploadToBlob(container, filename, blobName, override = false) {
console.log(`Uploading ${filename} ====> Container=${container}, Blobname=${blobName}`);
try {
return await createBlobFromLocalFile(container, filename, blobName, override);
} catch (error) {
if (error.code === "BlobAlreadyExists") {
const blob = await getBlob(container, blobName);
const md5 = computeFileMd5(filename);
const blobMd5 = blob.contentSettings && blob.contentSettings.contentMD5;
if (md5 === blobMd5) {
console.log(`Already uploaded ${filename} skipping(Md5 hash matched)`);
} else {
throw new Error(`Error blob already exists but doesn't match the local file. ${md5} != ${blobMd5}`);
}
} else {
throw error;
}
}
}

async function uploadFiles(os) {
const manifest = getManifest(os);
console.log(`Uploading ${manifest.files.length} files for os: ${os}`);
Expand Down