-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdocker-build.js
63 lines (58 loc) · 1.67 KB
/
docker-build.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
const shell = require('shelljs');
const dockerBuild = process.argv.slice(2).length && process.argv.slice(2)[0];
const dockerName = 'react-ssr-docker-' + dockerBuild;
const dockerImage = `vipgit/react-ssr-${dockerBuild}:${process.env.npm_package_version}`;
const dockerPortMapping = dockerBuild === 'frontend' ? '8080:80' : '8090:8090';
const dockerNetwork =
dockerBuild === 'frontend' ? '' : '--network postgres-network';
// docker network create --driver bridge postgres-network
shell.cd(`./docker/${dockerBuild}/prod`);
function getDockerContainerId(callback) {
shell.exec('docker rm ' + dockerName + ' --force', function(
code,
stdout,
stderr
) {
shell.exec(
`docker run -d -p ${dockerPortMapping} --name=${dockerName} ${dockerNetwork} ${dockerImage}`,
callback
);
});
}
function publishToDockerHub(commitHash, repository, callback) {
shell.exec('docker commit ' + commitHash + ' ' + repository, function(
code,
stdout,
stderr
) {
shell.exec('docker push ' + repository, callback);
});
}
if (
process.argv.slice(2).length &&
process.argv.slice(2).length === 2 &&
process.argv.slice(2)[1] === '--publish'
) {
shell.exec('docker build -t ' + dockerImage + ' .', function() {
getDockerContainerId(function(code, stdout, stderr) {
publishToDockerHub(stdout, dockerImage, function() {
console.log(
'Docker Build Successful - version deployed : ' + dockerImage
);
});
});
});
} else {
shell.exec('docker build -t ' + dockerImage + ' .', function(
code,
stdout,
stderr
) {
getDockerContainerId(function(code, stdout, stderr) {
console.log(
'Docker Build Successful - version running : ' + dockerImage,
stdout
);
});
});
}