-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (43 loc) · 1.54 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
#!/usr/bin/env node
var path = require('path');
var spawn = require('child_process').spawn;
var options = {
directory: process.cwd()
}
var pkg = require(path.resolve(options.directory, 'package.json'))
var imageNameAndVersion = getImageNameAndVersion(pkg)
console.log('Docker build and push: ', imageNameAndVersion)
if (!validatePackage(pkg)) {
console.error('Error package.json is not valid. Check the config.docker.imageName key exists');
process.exit(1);
}
var build = handleSpawnStream(spawn('docker', ['build', '-t', imageNameAndVersion, '.']))
build.on('close', function(code) {
if (code !== 0) {
console.log('docker build process exited with code ', code);
} else {
var push = handleSpawnStream(spawn('docker', ['push', imageNameAndVersion]))
push.on('close', function(code) {
if (code !== 0) {
console.log('docker push process exited with code ', code);
} else {
console.log('Docker build and push successful.');
}
})
}
});
function handleSpawnStream(spawnStream) {
spawnStream.stdout.pipe(process.stdout)
spawnStream.stderr.pipe(process.stderr)
spawnStream.on('error', function(err) {
console.error('Failed to start child process. Is Docker installed and available on PATH?');
process.exit(1)
})
return spawnStream
}
function validatePackage(pkg) {
return pkg && pkg.config && pkg.config.docker && pkg.config.docker.imageName
}
function getImageNameAndVersion(pkg) {
return pkg.config.docker.imageName + ':' + pkg.version
}