Skip to content

Commit 3825a8a

Browse files
author
Mark Molinaro
authored
Merge pull request #11 from microsoft/mamolin/try-again
Fall back to onCacheMiss on failure
2 parents e6e903f + 8c7a22f commit 3825a8a

1 file changed

Lines changed: 74 additions & 71 deletions

File tree

  • src/vsts/buildAndReleaseTask

src/vsts/buildAndReleaseTask/task.js

Lines changed: 74 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ var outputFiles = tl.getInput('outputFiles') || '';
2121
var outputIgnore = tl.getInput('outputIgnore') || '';
2222

2323
var options = {
24-
sourcePath: tl.getPathInput('sourcePath', true, true),
25-
sourceFiles: tl.getInput('sourceFiles', true).split(/\r?\n/),
26-
sourceIgnore: sourceIgnore.split(/\r?\n/),
27-
hashSuffix: tl.getInput('hashSuffix'),
28-
execWorkingDirectory: tl.getPathInput('execWorkingDirectory'),
29-
execCommand: tl.getInput('execCommand'),
30-
storageAccount: tl.getInput('storageAccount'),
31-
storageContainer: tl.getInput('storageContainer'),
32-
storageKey: tl.getInput('storageKey'),
33-
outputPath: tl.getInput('outputPath'),
34-
outputFiles: outputFiles.split(/\r?\n/),
35-
outputIgnore: outputIgnore.split(/\r?\n/),
36-
uploadCacheOnMiss: tl.getBoolInput('uploadCacheOnMiss'),
37-
downloadCacheOnHit: tl.getBoolInput('downloadCacheOnHit')
24+
sourcePath: tl.getPathInput('sourcePath', true, true),
25+
sourceFiles: tl.getInput('sourceFiles', true).split(/\r?\n/),
26+
sourceIgnore: sourceIgnore.split(/\r?\n/),
27+
hashSuffix: tl.getInput('hashSuffix'),
28+
execWorkingDirectory: tl.getPathInput('execWorkingDirectory'),
29+
execCommand: tl.getInput('execCommand'),
30+
storageAccount: tl.getInput('storageAccount'),
31+
storageContainer: tl.getInput('storageContainer'),
32+
storageKey: tl.getInput('storageKey'),
33+
outputPath: tl.getInput('outputPath'),
34+
outputFiles: outputFiles.split(/\r?\n/),
35+
outputIgnore: outputIgnore.split(/\r?\n/),
36+
uploadCacheOnMiss: tl.getBoolInput('uploadCacheOnMiss'),
37+
downloadCacheOnHit: tl.getBoolInput('downloadCacheOnHit')
3838
}
3939

4040
// calling this function prints all the variables if System.Debug == true
@@ -60,65 +60,68 @@ var hashAndCache = function (options) {
6060

6161
var hash = generateHash(options.sourcePath, options.sourceFiles, options.sourceIgnore, options.hashSuffix, options.execCommand);
6262

63-
doesCacheExist(hash, options.storageAccount, options.storageContainer, options.storageKey).then(function(result) {
64-
if (result) {
65-
console.log("CACHE HIT!");
63+
doesCacheExist(hash, options.storageAccount, options.storageContainer, options.storageKey).then(function (result) {
64+
if (result) {
65+
console.log("CACHE HIT!");
6666

67-
if (options.downloadCacheOnHit) {
68-
downloadCache(hash, options.storageAccount, options.storageContainer, options.storageKey, options.outputPath).then(function() {
69-
extractCache(options.outputPath, hash);
70-
deleteCache(options.outputPath, hash);
71-
});
72-
}
73-
} else {
74-
console.log("CACHE MISS!");
75-
76-
if (options.execCommand) {
77-
console.log("Running Command " + options.execCommand);
78-
execSync(options.execCommand, { cwd: options.execWorkingDirectory, stdio: 'inherit' });
79-
} else {
80-
console.log("No command specified - skipping");
81-
}
82-
83-
if (options.uploadCacheOnMiss) {
84-
var files = getFileList(options.outputPath, options.outputFiles, options.outputIgnore);
85-
86-
if (!files || files.length == 0) {
87-
console.log("No output files found - skipping cache update");
88-
return;
89-
}
90-
91-
var tarFile = hash + ".tgz";
92-
var tarPath = path.join(options.outputPath, tarFile);
93-
// the tar library doesn't like paths that start with @ - need to add ./ to the start
94-
files = files.map(function(value) { return value.startsWith('@') ? './' + value : value });
95-
96-
console.log("Creating tarball " + tarPath);
97-
98-
var tarOptions = {
99-
sync: true,
100-
file: tarPath,
101-
strict: true,
102-
gzip: true,
103-
portable: true,
104-
noMtime: true,
105-
cwd: options.outputPath
106-
}
107-
108-
tar.create(tarOptions, files);
109-
uploadCache(tarPath, tarFile, options.storageAccount, options.storageContainer, options.storageKey)
110-
.then(function() {
111-
fs.unlinkSync(tarPath);
112-
})
113-
.catch(function(err) {
114-
console.warn("Uploading of cache failed. This may happen when attempting to upload in parallel.")
115-
console.warn(err);
116-
});
117-
}
67+
if (options.downloadCacheOnHit) {
68+
downloadCache(hash, options.storageAccount, options.storageContainer, options.storageKey, options.outputPath).then(function () {
69+
extractCache(options.outputPath, hash);
70+
deleteCache(options.outputPath, hash);
71+
}).catch(function () { onCacheMiss(e) });
11872
}
73+
} else {
74+
console.log("CACHE MISS!");
75+
return onCacheMiss(options);
76+
}
11977
});
12078
}
12179

80+
var onCacheMiss = function (options) {
81+
if (options.execCommand) {
82+
console.log("Running Command " + options.execCommand);
83+
execSync(options.execCommand, { cwd: options.execWorkingDirectory, stdio: 'inherit' });
84+
} else {
85+
console.log("No command specified - skipping");
86+
}
87+
88+
if (options.uploadCacheOnMiss) {
89+
var files = getFileList(options.outputPath, options.outputFiles, options.outputIgnore);
90+
91+
if (!files || files.length == 0) {
92+
console.log("No output files found - skipping cache update");
93+
return;
94+
}
95+
96+
var tarFile = hash + ".tgz";
97+
var tarPath = path.join(options.outputPath, tarFile);
98+
// the tar library doesn't like paths that start with @ - need to add ./ to the start
99+
files = files.map(function (value) { return value.startsWith('@') ? './' + value : value });
100+
101+
console.log("Creating tarball " + tarPath);
102+
103+
var tarOptions = {
104+
sync: true,
105+
file: tarPath,
106+
strict: true,
107+
gzip: true,
108+
portable: true,
109+
noMtime: true,
110+
cwd: options.outputPath
111+
}
112+
113+
tar.create(tarOptions, files);
114+
uploadCache(tarPath, tarFile, options.storageAccount, options.storageContainer, options.storageKey)
115+
.then(function () {
116+
fs.unlinkSync(tarPath);
117+
})
118+
.catch(function (err) {
119+
console.warn("Uploading of cache failed. This may happen when attempting to upload in parallel.")
120+
console.warn(err);
121+
});
122+
}
123+
}
124+
122125
var generateHash = function (sourcePath, sourceFiles, sourceIgnore, hashSuffix, execCommand) {
123126
console.log("Generating Hash...");
124127
console.log("sourcePath: " + sourcePath);
@@ -168,7 +171,7 @@ var getFileList = function (workingDirectory, globs, ignoreGlob) {
168171
files = files.concat(glob.sync(g, globOptions));
169172
}
170173

171-
var filesUnique = files.sort().filter(function(item, pos, ary) {
174+
var filesUnique = files.sort().filter(function (item, pos, ary) {
172175
return !pos || item != ary[pos - 1];
173176
});
174177

@@ -209,7 +212,7 @@ var downloadCache = function (hash, storageAccount, storageContainer, storageKey
209212
console.log("storageAccount: " + storageAccount);
210213
console.log("storageContainer: " + storageContainer);
211214
console.log("targetPath: " + targetPath);
212-
215+
213216
if (storageAccount && storageContainer && storageKey) {
214217
var blobName = hash + ".tgz";
215218
var downloadFile = path.join(targetPath, blobName);
@@ -247,7 +250,7 @@ var uploadCache = function (blobPath, blobName, storageAccount, storageContainer
247250
console.log("blobName: " + blobName);
248251
console.log("storageAccount: " + storageAccount);
249252
console.log("storageContainer: " + storageContainer);
250-
253+
251254
if (storageAccount && storageContainer && storageKey) {
252255
var blobService = azureStorage.createBlobService(storageAccount, storageKey);
253256

0 commit comments

Comments
 (0)