如何更改开发和生产变量

时间:2019-04-16 10:42:23

标签: node.js heroku

我连接数据库:

with t as (
      select v.*
      from (values ('0001', 'A'),
                   ('0001', 'B'),
                   ('0001', 'C'),
                   ('0002', 'A'),
                   ('0002', 'C'),
                   ('0003', 'D'),
                   ('0004', 'A')
            ) v(orderid, article)
     ),
     a as (
      select article, count(*) as cnt,
             row_number() over (order by count(*) desc, article) as seqnum
      from t
      group by article
     ),
     aa as (
      select t.*, a.seqnum, min(seqnum) over (partition by orderid) as order_seqnum
      from t join
           a
           on t.article = a.article
     )
select article, count(*) as cnt,
       sum(sum(case when order_seqnum = seqnum then 1 else 0 end)) over (order by count(*) desc, article)
from aa
group by article;

我的配置文件:

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

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

我的export default { db_dev: { connectionString: 'postgres://sfp...', }, db_prod: { connectionString: 'postgres://woy...', }, ... } 文件:

.gitlab-ci.yml

我的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 文件:

package.json

当我加载到{ ... "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" } } 分支中的存储库中以使连接字符串为dev时,我需要,当我加载到db_dev分支中的存储库中以使连接字符串时,我需要是master

对我来说,不是我自己手动db_prod上的db_dev再返回。

我该如何实现?

1 个答案:

答案 0 :(得分:2)

也许还有其他解决方案,但这是我的解决方案。

  • 使用environment variable切换连接字符串-变量名:DB_CONNECTION_STRING

  • 使用Heroku platform-api更新Config Vars:更新DB_CONNECTION_STRING值。

首先,编辑代码以动态选择数据库字符串:

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

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

接下来,更新您的.gitlab-ci.yml文件。我们必须通过http api更新Heroku的Config Vars

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