Access env variables in node js app when using gitlab ci cd pipeline
Asked Answered
K

1

9

I am using gitlab ci cd pipe line to deploy my application to ubuntu server. I have different .env file for local and for dev env and its not a part of git repo (included in gitignore) how to get env variables in my app when deployed to ubuntu server

my gitlab-ci.yml

stages:
     - deploy
cache:
  paths:
    - node_modules/
deploy:
    stage: deploy
    script:
      - npm install
      - sudo pm2 delete lknodeapi || true
      - sudo pm2 start server.js --name lknodeapi
  
Kerns answered 17/1, 2021 at 14:44 Comment(1)
Does this answer your question? How do I add a .env file in gitlab ci during deployment stage?Turbulent
H
13

I guess you are looking for this -Create Variables Gitlab.You can create your environment variables in the ui and then change your gitlab-ci.yml like below

stages:
     - deploy
cache:
  paths:
    - node_modules/
deploy:
    stage: deploy
    script:
      - echo "NGINX_REPO_KEY"=$NGINX_REPO_KEY >> ".env"
      - npm install
      - sudo pm2 delete lknodeapi || true
      - sudo pm2 start server.js --name lknodeapi

This will create a .env file in root folder and put your variables in it.

Heavyladen answered 17/1, 2021 at 16:4 Comment(7)
Hi, I really need help with pm2. So i did everything as every tutorial suggests. my pm2 status is active and online, i also did the pm2 startup part but still my app is not working. When i stop pm2 and run node index.js my app works. Why is pm2 not working?Licht
pls check for logs ,sudo pm2 logs @LichtHeavyladen
@Shubh i am not getting any errors, PM2 | 2021-01-27T06:47:04: PM2 log: --- New PM2 Daemon started ---------------------------------------------------- PM2 | 2021-01-27T06:47:04: PM2 log: Time : Wed Jan 27 2021 06:47:04 GMT+0000 (Coordinated Universal Time) PM2 | 2021-01-27T06:47:04: PM2 log: PM2 version : 4.5.1 PM2 | 2021-01-27T06:47:04: PM2 log: Node.js version : 12.20.1Licht
can't say anything about it thenHeavyladen
My data from my database is not getting loaded for some reason, its working with when i start the app with node but not with pm2Licht
Please post a separate question with all the details and tag me @LichtHeavyladen
ok thank you, what files should i include in my question default.conf and nginx.conf?Licht

© 2022 - 2024 — McMap. All rights reserved.