-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
72 lines (59 loc) · 2.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
const hoek = require('@hapi/hoek');
const { BookendInterface } = require('screwdriver-build-bookend');
/**
* Get cache commands that call the store-cli for approrpiate scope
* @method getCacheCommands
* @param {Array} cache Array of cacheKeys
* @param {String} scope Cache scope
* @param {String} action Set or Get
* @return {String} Commands to call store-cli
*/
function getCacheCommands(cache, scope, action) {
if (cache && cache.length > 0) {
const cmds = cache.map(item => `store-cli ${action} ${item} --type=cache --scope=${scope} || true`);
return cmds.join(' ; ');
}
return `echo skipping ${scope} cache`;
}
class CacheBookend extends BookendInterface {
/**
* Gives the commands needed for restoring build cache before the build starts.
* @method getSetupCommand
* @param {Object} o Information about the environment for setup
* @param {PipelineModel} o.pipeline Pipeline model for the build
* @param {JobModel} o.job Job model for the build
* @param {Object} o.build Build configuration for the build (before creation)
* @return {Promise} Resolves to a string that represents the command to execute
*/
getSetupCommand(o) {
const cache = hoek.reach(o.job, 'permutations.0.cache');
if (cache) {
const pipelineCache = getCacheCommands(cache.pipeline, 'pipeline', 'get');
const eventCache = getCacheCommands(cache.event, 'event', 'get');
const jobCache = getCacheCommands(cache.job, 'job', 'get');
return Promise.resolve(`${pipelineCache} ; ${eventCache} ; ${jobCache}`);
}
return Promise.resolve('echo skipping cache');
}
/**
* Gives the commands needed for publishing build cache after a build completes.
* @method getTeardownCommand
* @param {Object} o Information about the environment for setup
* @param {PipelineModel} o.pipeline Pipeline model for the build
* @param {JobModel} o.job Job model for the build
* @param {Object} o.build Build configuration for the build (before creation)
* @return {Promise} Resolves to a string that represents the commmand to execute
*/
getTeardownCommand(o) {
const cache = hoek.reach(o.job, 'permutations.0.cache');
if (cache) {
const pipelineCache = getCacheCommands(cache.pipeline, 'pipeline', 'set');
const eventCache = getCacheCommands(cache.event, 'event', 'set');
const jobCache = getCacheCommands(cache.job, 'job', 'set');
return Promise.resolve(`${pipelineCache} ; ${eventCache} ; ${jobCache}`);
}
return Promise.resolve('echo skipping cache');
}
}
module.exports = CacheBookend;