Skip to content

Instantly share code, notes, and snippets.

@kurtzilla
Last active June 27, 2018 07:07
Show Gist options
  • Save kurtzilla/6a3ca5964dc1470deacf9612bda4640e to your computer and use it in GitHub Desktop.
Save kurtzilla/6a3ca5964dc1470deacf9612bda4640e to your computer and use it in GitHub Desktop.

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"

yarn remove dotenv-safe

change import in startServer.ts to import dotenv not dotenv-safe

remove exceptions from the dockerignore for .env and .env.prod

comment out or remove the copying of the .env files in Dockerfile

...
COPY ./packages/server/dist ./packages/server/dist
COPY ./packages/common/dist ./packages/common/dist
# COPY ./packages/server/.env.prod ./packages/server/.env
# COPY ./packages/server/.env.example ./packages/server/
COPY ./ormconfig.json .
...

create the file /packages/server/src/scripts/generate_do.ts

import * as fs from 'fs';
import * as path from 'path';

const txt = fs.readFileSync(path.join(__dirname, '../../.env.prod'), 'utf-8');
const vars = txt.replace(/(?:\r\n|\r|\n)/g, ' ');

const namespace = 'kurtzilla';
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@167.99.167.255 "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 => {
  console.log(err);
});

create the file /packages/server/src/scripts/generate_heroku.ts

import * as fs from 'fs';
import * as path from 'path';

const txt = fs.readFileSync(path.join(__dirname, '../../.env.prod'), 'utf-8');
const vars = txt.replace(/(?:\r\n|\r|\n)/g, ' ');

const herokuApp = 'infinite-cove-14572';

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);
  }
);

remove the deploy_server_xxx.sh files

in ./package.json - do this for scripts

"scripts": {
    "build:server": "lerna run build --scope={@abb/common,@abb/server}",
    "deploy:do": "yarn build:server && ts-node ./packages/server/src/scripts/generate_do.ts && chmod +x ./deploy_do.sh && ./deploy_do.sh",
    "deploy:heroku": "yarn build:server && ts-node ./packages/server/src/scripts/generate_heroku.ts && chmod +x ./deploy_heroku.sh && ./deploy_heroku.sh"
  }

new gitignore

.idea/
.vscode/
node_modules/
build/
temp/
OBS/
*.log
dump.rdb
.env
.env.prod
dist
deploy_do.sh
deploy_heroku.sh

// TODO: don't copy over server/src/script files - or move directory to root? Can get exclude to work in copyfiles yet // "build": "rimraf ./dist && tsc && copyfiles -u 1 "src/**/*.graphql" --e "./src/scripts/" dist", // SOLUTION - these files are being added by tsc not copyfiles // add scripts folder to exclude list in /server/tsconfig.json

"exclude": ["node_modules", "./src/scripts/"],
"include": ["./src/**/*.tsx", "./src/**/*.ts"]

// TODO: if the key values have spaces in the string - this will all fall apart - escape strings

// TODO: create a method/workflow to remove any obsolete keys // DOCS: http://dokku.viewdocs.io/dokku/configuration/environment-variables/ // DOCS: https://devcenter.heroku.com/articles/config-vars

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment