1

I connect the database:

import { Pool } from 'pg';
import config from './../config';

export default new Pool({
    connectionString: config.db_prod.connectionString,
    ssl: true,
});

My configuration file:

export default {
    db_dev: {
        connectionString: 'postgres://sfp...',
    },
    db_prod: {
        connectionString: 'postgres://woy...',
    },
...
}

My .gitlab-ci.yml file:

image: node:latest

stages:
  - production
  - development

cache:
    paths:
      - node_modules/

production:
  type: deploy
  stage: production
  image: ruby:latest
  script:
    - apt-get update -qy
    - apt-get install -y ruby-dev
    - gem install dpl
    - dpl --provider=heroku --app=deploy-node-aspp-with-gitlab --api-key=$HEROKU_PRODUCTION_API_KEY
  artifacts:
    paths:
      - node_modules/
  only:
    - master

development:
  type: deploy
  stage: development
  image: ruby:latest
  script:
    - apt-get update -qy
    - apt-get install -y ruby-dev
    - gem install dpl
    - dpl --provider=heroku --app=deploy-node-aspp-with-gitlab-d --api-key=$HEROKU_STAGING_API_KEY
  artifacts:
    paths:
      - node_modules/
  only:
    - dev

My package.json file:

{
...
  "main": "index.js",
  "scripts": {
    "start": "nodemon --exec babel-node src/index.js"
  },
  "devDependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.2.2",
    "@babel/node": "^7.2.2",
    "@babel/preset-env": "^7.3.1",
    "chai": "^4.2.0",
    "chai-http": "^4.2.1",
    "faker": "^4.1.0",
    "mocha": "^6.1.3",
    "nodemon": "^1.18.10"
  },
  "dependencies": {
    "aws-sdk": "^2.398.0",
    "bcrypt": "^3.0.3",
    "body-parser": "^1.18.3",
    "cron": "^1.7.0",
    "express": "^4.16.4",
    "express-validator": "^5.3.1",
    "firebase-admin": "^7.2.0",
    "generate-password": "^1.4.1",
    "handlebars": "^4.1.2",
    "jsonwebtoken": "^8.4.0",
    "moment": "^2.24.0",
    "morgan": "^1.9.1",
    "multer": "^1.4.1",
    "node-gcm": "^1.0.2",
    "nodemailer": "^6.1.0",
    "pg": "^7.8.0",
    "uuid": "^3.3.2"
  }
}

I need when I load into the repository in the dev branch so that the connection string is db_dev, when I load into the repository in the master branch so that the connection string is db_prod.

To me not me manually with db_dev on db_prod and back.

How do i implement this?

1 Answer 1

2

Maybe have other solutions, but this is my solution.

  • Use environment variable to switch connection strings - variable name: DB_CONNECTION_STRING

  • Use Heroku platform-api to update Config Vars: Update DB_CONNECTION_STRING value.

The first, edit your code to select the db string dynamically:

import { Pool } from 'pg';
import config from './../config';

export default new Pool({
    connectionString: config[process.env.DB_CONNECTION_STRING || "db_dev"].connectionString,
    ssl: true,
});

Next, update your .gitlab-ci.yml file. We have to update Heroku's Config Vars via http api:

image: node:latest

stages:
  - production
  - development

cache:
    paths:
      - node_modules/

production:
  type: deploy
  stage: production
  image: ruby:latest
  script:
    - apt-get update -qy
    - apt-get install -y ruby-dev
    - gem install dpl
    - >-
      curl -X PATCH https://api.heroku.com/apps/deploy-node-aspp-with-gitlab/config-vars -H "Accept: application/vnd.heroku+json; version=3" -H "Authorization: Bearer $HEROKU_PRODUCTION_API_KEY" -H "Content-Type: application/json" -d "{'DB_CONNECTION_STRING': 'db_prod'}"
    - dpl --provider=heroku --app=deploy-node-aspp-with-gitlab --api-key=$HEROKU_PRODUCTION_API_KEY
  artifacts:
    paths:
      - node_modules/
  only:
    - master

development:
  type: deploy
  stage: development
  image: ruby:latest
  script:
    - apt-get update -qy
    - apt-get install -y ruby-dev
    - gem install dpl
    - >-
      curl -X PATCH https://api.heroku.com/apps/deploy-node-aspp-with-gitlab/config-vars -H "Accept: application/vnd.heroku+json; version=3" -H "Authorization: Bearer $HEROKU_STAGING_API_KEY" -H "Content-Type: application/json" -d "{'DB_CONNECTION_STRING': 'db_dev'}"
    - dpl --provider=heroku --app=deploy-node-aspp-with-gitlab-d --api-key=$HEROKU_STAGING_API_KEY
  artifacts:
    paths:
      - node_modules/
  only:
    - dev
Sign up to request clarification or add additional context in comments.

3 Comments

This is what I was looking for!
Your code works, but variable DB_CONNECTION_STRING contains undefined
Let check the variable on heroku dashboard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.