in addition to below...
// TODO still having issues getting env vars with spaces to work on dokku instance // I think the best course of action is to remove all spaces for now (replacing with // dashes was a close second choice, but easy enough to implement with the following file
create /server/src/utils/reduceEnvVars.ts
// current default setting is remove spaces
export const reduceEnvVars = (fileText: string, replaceSpace: string = '') => {
const lines = fileText.split(/(?:\r\n|\r|\n)/g);
const vars = lines.reduce((prev: string, curr: string): string => {
const pair = curr.split('=');
if (pair.length === 2) {
const key = pair[0];
// TODO make the value work with spaces on dokku!!
const value = pair[1].replace(/\s+/g, replaceSpace).replace(/\"/g, '');
// surround values with spaces with double quotes
// const value = pair[1].replace(/\s+/g, '\\ ').replace(/\"/g, '');// yields a backslash space - but dokku no likey
// value = value.indexOf('\\ ') !== -1 ? `"${value}"` : value;
// be sure to add a space at the end to separate values
return `${prev}${key}="${value}" `;
}
return prev;
}, '');
return vars.trim();
};
change deploy scripts generate_do.ts
import * as fs from 'fs';
import * as path from 'path';
import { reduceEnvVars } from '../utils/reduceEnvVars';
const txt = fs.readFileSync(path.join(__dirname, '../../.env.prod'), 'utf-8');
const vars = reduceEnvVars(txt);
// console.log('VARS', vars);
const namespace = '<namespace>';
const appName = 'abb';
const version = 'latest';
const appVersion = `${appName}:${version}`;
const deploy = `
#! /bin/bash
docker build -t ${namespace}/${appVersion} .
docker push ${namespace}/${appVersion}
ssh root@<ip-address> "dokku config:set --no-restart ${appName} ${vars} && docker pull ${namespace}/${appVersion} && docker tag ${namespace}/${appVersion} dokku/${appVersion} && dokku tags:deploy ${appName} ${version}"
`;
fs.writeFile(path.join(__dirname, '../../../../deploy_do.sh'), deploy, err => {
if (err) {
console.log(err);
}
});
generate_heroku.ts
import * as fs from 'fs';
import * as path from 'path';
import { reduceEnvVars } from '../utils/reduceEnvVars';
const txt = fs.readFileSync(path.join(__dirname, '../../.env.prod'), 'utf-8');
const vars = reduceEnvVars(txt);
const herokuApp = '<heroku-app>';
const deploy = `
#! /bin/bash
heroku container:push --app=${herokuApp} web
heroku config:set ${vars}
heroku container:release --app=${herokuApp} web
`;
fs.writeFile(
path.join(__dirname, '../../../../deploy_heroku.sh'),
deploy,
err => {
console.log(err);
}
);
added a new script to ./package.json for help with testing
"testdo": "ts-node ./packages/server/src/scripts/generate_do.ts"