-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdockerize.js
99 lines (80 loc) · 2.77 KB
/
dockerize.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const { exec } = require('child_process');
const getConfig = pluginConfig => ({
registry: pluginConfig.registry || process.env.DOCKER_REGISTRY,
image: pluginConfig.image || process.env.DOCKER_IMAGE,
dockerfile: pluginConfig.dockerfile || process.env.DOCKER_FILE,
argVersion: pluginConfig.argVersion,
target: pluginConfig.target,
});
const runDocker = cmd =>
new Promise((resolve, reject) => {
const options = {
cwd: process.cwd(),
env: process.env,
};
exec(`docker ${cmd}`, options, error => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
const getImageName = (tag, config) => {
const { image, registry } = config;
if (!image) {
throw new Error('DOCKER_IMAGE is required');
}
if (!registry) {
return `${image}:${tag}`;
}
return `${registry}/${image}:${tag}`;
};
const prepare = async (pluginConfig, context) => {
const { argVersion, dockerfile, target, ...config } = getConfig(pluginConfig);
const { nextRelease, lastRelease, logger } = context;
const { version, channel } = nextRelease;
if (lastRelease) {
const { version: lastVersion } = lastRelease;
const lastTag = getImageName(lastVersion, config);
const pullCommand = ['pull', lastTag].join(' ');
logger.log('Docker pulling for %s', lastTag);
await runDocker(pullCommand);
}
// build a versioned image
const versionTag = getImageName(version, config);
const command = [
'build',
target && `--target ${target}`,
`-t ${versionTag}`,
dockerfile && `-f ${dockerfile}`,
argVersion && `--build-arg ${argVersion}=${version}`,
'.',
]
.filter(Boolean)
.join(' ');
logger.log('Docker building for %s', versionTag);
await runDocker(command);
if (channel) {
// tag the image for the channel
const channelTag = getImageName(channel, config);
logger.log('Docker tagging for %s', channelTag);
await runDocker(`tag ${versionTag} ${channelTag}`);
}
};
const publish = async (pluginConfig, context) => {
const config = getConfig(pluginConfig);
const { nextRelease, logger } = context;
const { version, channel } = nextRelease;
// push the versioned image
const versionTag = getImageName(version, config);
logger.log('Docker pushing for %s', versionTag);
await runDocker(`push ${versionTag}`);
if (channel) {
// push the image on the channel
const channelTag = getImageName(channel, config);
logger.log('Docker pushing for %s', channelTag);
await runDocker(`push ${channelTag}`);
}
};
module.exports = { prepare, publish };