Skip to content

Commit

Permalink
feat(*): synchronized contents of archive build with contents of dock…
Browse files Browse the repository at this point in the history
…er build
  • Loading branch information
Sergey Sokolov committed Apr 16, 2019
1 parent 542df64 commit 97a1fa2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
48 changes: 44 additions & 4 deletions commands/archive-build/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
const shell = require('shelljs');
const tar = require('tar');
const path = require('path');
const fs = require('fs-extra');
const configs = require('../../configs/app-configs');
const exec = require('../util/exec');
const nginxConfTemplate = require('../../templates/nginx.conf.template');
const startScript = require('../../templates/start.template');

const tempDirName = '.archive-build';
const nginxConfFileName = 'nginx.conf';
const startScriptFileName = 'start.sh';
const nodeModulesDirName = 'node_modules';
const packageJsonFileName = 'package.json';
const pathToTempDir = path.join(configs.cwd, tempDirName);
const nginxConfPath = path.join(pathToTempDir, nginxConfFileName);
const startScriptPath = path.join(pathToTempDir, startScriptFileName);
const nodeModulesPath = path.join(configs.cwd, nodeModulesDirName);
const packageJsonPath = path.join(configs.cwd, packageJsonFileName);

(async () => {
try {
console.time('Total time');
console.time('Setting up time');

await fs.emptyDir(pathToTempDir);

const nginxConf = configs.localNginxConf
? await fs.readFile(configs.localNginxConf, 'utf8')
: nginxConfTemplate;

await Promise.all([
fs.writeFile(nginxConfPath, nginxConf, 'utf8'),
fs.writeFile(startScriptPath, startScript, { encoding: 'utf8', mode: 0o555 }),
fs.remove(configs.buildPath),
]);

console.timeEnd('Setting up time');
console.time('Build application time');
// run build script
await exec('npm run build');

console.timeEnd('Build application time');

console.time('Remove build dependencies time');
// if yarn is available prune dev dependencies with yarn, otherwise use npm
if (configs.useYarn && shell.which('yarn')) {
Expand All @@ -22,15 +50,27 @@ const exec = require('../util/exec');
}

console.timeEnd('Remove build dependencies time');

console.time('Archive build time');
await Promise.all([
fs.copy(configs.buildPath, path.join(pathToTempDir, configs.buildPath)),
fs.copy(nodeModulesPath, path.join(pathToTempDir, nodeModulesDirName)),
fs.copy(packageJsonPath, path.join(pathToTempDir, packageJsonFileName))
]);
await tar.c(
{ file: configs.archiveName },
[configs.buildPath, ...configs.additionalBuildPath, 'node_modules', 'package.json']
{
file: configs.archiveName,
cwd: pathToTempDir
},
fs.readdirSync(pathToTempDir)
);

console.timeEnd('Archive build time');
console.time('Cleanup time');

// remove temp directory
await fs.remove(pathToTempDir);

console.timeEnd('Cleanup time');
console.timeEnd('Total time');
} catch (err) {
console.error('Error during archive-build.');
Expand Down
12 changes: 7 additions & 5 deletions commands/docker-build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const path = require('path');
const shell = require('shelljs');
const fs = require('fs-extra');
const configs = require('../../configs/app-configs');
const dockerfileTemplate = require('./dockerfile.template');
const nginxConfTemplate = require('./nginx.conf.template');
const startScript = require('./start.template');
const dockerfileTemplate = require('../../templates/dockerfile.template');
const nginxConfTemplate = require('../../templates/nginx.conf.template');
const startScript = require('../../templates/start.template');
const exec = require('../util/exec');

let imageVersion = configs.version;
Expand Down Expand Up @@ -36,6 +36,7 @@ const imageFullName = `${dockerRegistry ? `${dockerRegistry}/` : ''}${imageName}
try {
console.log(`Build docker image ${imageFullName}`);
console.time('Total time');
console.time('Setting up time');
// create tmp directory for docker related files
// We need to copy it because we will remove this directory during build process

Expand All @@ -56,12 +57,12 @@ const imageFullName = `${dockerRegistry ? `${dockerRegistry}/` : ''}${imageName}
fs.remove(configs.buildPath),
]);

console.timeEnd('Setting up time');
console.time('Build application time');
// run build script
await exec('npm run build');

console.timeEnd('Build application time');

console.time('Remove dev dependencies time');
// if yarn is available prune dev dependencies with yarn, otherwise use npm
if (configs.useYarn && shell.which('yarn')) {
Expand All @@ -71,13 +72,13 @@ const imageFullName = `${dockerRegistry ? `${dockerRegistry}/` : ''}${imageName}
}

console.timeEnd('Remove dev dependencies time');

console.time('Build docker image time');
await exec(`docker build -f "./${tempDirName}/Dockerfile" \\
--build-arg START_SH_LOCATION="./${tempDirName}/start.sh" \\
--build-arg NGINX_CONF_LOCATION="./${tempDirName}/nginx.conf" -t ${imageFullName} .`);

console.timeEnd('Build docker image time');
console.time('Cleanup time');

// remove temp directory
await fs.remove(pathToTempDir);
Expand All @@ -87,6 +88,7 @@ const imageFullName = `${dockerRegistry ? `${dockerRegistry}/` : ''}${imageName}
await exec(`docker push ${imageFullName}`);
}

console.timeEnd('Cleanup time');
console.timeEnd('Total time');
} catch (err) {
await fs.remove(pathToTempDir);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const configs = require('../../configs/app-configs');
const configs = require('../configs/app-configs');

module.exports = `
FROM ${configs.baseDockerImage}
Expand All @@ -7,6 +7,6 @@ ARG NGINX_CONF_LOCATION
WORKDIR /src
ADD $START_SH_LOCATION /src/start.sh
ADD $NGINX_CONF_LOCATION /etc/nginx/conf.d/default.conf
ADD $NGINX_CONF_LOCATION /src/nginx.conf
ADD . /src
`;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const configs = require('../../configs/app-configs');
const configs = require('../configs/app-configs');

module.exports = `server {
listen ${configs.clientServerPort};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const configs = require('../../configs/app-configs');
const configs = require('../configs/app-configs');

module.exports = `#!/bin/sh
# Move nginx config
mv ./nginx.conf /etc/nginx/conf.d/default.conf
# Start the nginx process in background
nginx &
# Start nodejs process
node ./${configs.buildPath}/${configs.serverOutput}
`;
`;

0 comments on commit 97a1fa2

Please sign in to comment.