1
0
mirror of https://github.com/yldio/copilot.git synced 2024-11-28 14:10:04 +02:00
copilot/scripts/deploy

134 lines
3.2 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env node
const execa = require('execa');
const { parse } = require('dotenv');
const main = require('apr-main');
const map = require('apr-map');
const series = require('apr-series');
const globby = require('globby');
const path = require('path');
const flatten = require('lodash.flatten');
const readPkg = require('read-pkg');
const figures = require('figures');
const NAMESPACE = 'quay.io/yldio';
const ROOT = path.join(__dirname, '..');
const LOG = console.log;
const createTritonProfile = async ({ SDC_ACCOUNT = '', SDC_KEY_ID = '' }) => {
2017-05-31 13:46:58 +03:00
LOG(
`${figures.arrowRight} create triton profile SDC_ACCOUNT=${SDC_ACCOUNT.length} SDC_KEY_ID=${SDC_KEY_ID.length}`
);
const payload = JSON.stringify({
url: 'https://eu-ams-1.api.joyent.com',
account: SDC_ACCOUNT,
keyId: SDC_KEY_ID,
name: 'eu-ams-1',
curr: true
});
2017-05-31 13:46:58 +03:00
const cp = execa('triton', ['profile', 'create', '-f', '-'], {
input: payload
});
2017-05-31 13:46:58 +03:00
cp.stdout.pipe(process.stdout);
cp.stderr.pipe(process.stderr);
return cp;
};
const getEnv = async () => {
await createTritonProfile(process.env);
const tritonEnv = await execa.stdout('triton', ['env']);
const dotEnv = tritonEnv.replace(/^export /gim, '');
return Object.assign({}, process.env, parse(dotEnv));
};
const login = ({
_DOCKER_LOGIN_USERNAME,
_DOCKER_LOGIN_PASSWORD,
_DOCKER_REGISTRY
}) => async () => {
LOG(`${figures.arrowRight} login`);
2017-05-31 13:46:58 +03:00
return execa(
'docker',
[
'login',
'--email="."',
`--username="${_DOCKER_LOGIN_USERNAME}"`,
`--password="${_DOCKER_LOGIN_PASSWORD}"`,
_DOCKER_REGISTRY
],
{
stdio: 'inherit'
}
);
};
const build = ({ CIRCLE_BRANCH }) => async () => {
LOG(`${figures.arrowRight} build`);
const dockerfiles = await globby(['packages/*/Dockerfile'], {
cwd: ROOT
});
LOG(`${figures.arrowRight} build.dockerfiles`);
LOG(JSON.stringify(dockerfiles, null, 2));
return map(dockerfiles, async dockerfile => {
const folder = path.resolve(ROOT, path.dirname(dockerfile));
const { name } = await readPkg(folder);
const tags = [`${name}:${CIRCLE_BRANCH}`, `${name}:latest`];
LOG(`${figures.arrowRight} build.name ${name}`);
LOG(JSON.stringify(tags, null, 2));
await execa(
'docker',
flatten([
'build',
flatten(tags.map(name => ['-t', `${NAMESPACE}/${name}`])),
'-f',
dockerfile,
'.'
]),
{
stdio: 'inherit'
}
);
LOG(`${figures.arrowRight} build.push ${NAMESPACE}/${name}`);
return execa('docker', ['push', `${NAMESPACE}/${name}`], {
stdio: 'inherit'
});
});
};
const logout = env => () => {
LOG(`${figures.arrowRight} logout`);
2017-05-31 12:38:19 +03:00
return execa('docker', ['logout'], {
stdio: 'inherit'
});
};
const deploy = env => () => {
LOG(`${figures.arrowRight} deploy`);
return execa('docker-compose', ['up', '-d', '--build'], {
2017-05-31 12:38:19 +03:00
stdio: 'inherit',
env
});
};
const run = async () => {
const env = await getEnv();
LOG(`${figures.arrowRight} .env`);
LOG(JSON.stringify(Object.keys(env), null, 2));
2017-05-31 17:33:45 +03:00
return series([login(env), build(env), logout(env), deploy(env)]);
};
LOG(`${figures.arrowRight} DEPLOY`);
main(run());