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
  
Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46
gANDALF
  • 360
  • 4
  • 21
  • Does this answer your question? [How do I add a .env file in gitlab ci during deployment stage?](https://stackoverflow.com/questions/52540316/how-do-i-add-a-env-file-in-gitlab-ci-during-deployment-stage) – Miguel Mar 03 '23 at 11:17

1 Answers1

12

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.

Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46
  • 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? – kd12345 Jan 27 '21 at 05:00
  • pls check for logs ,sudo pm2 logs @kd12345 – Shubham Dixit Jan 27 '21 at 06:42
  • @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.1 – kd12345 Jan 27 '21 at 06:48
  • can't say anything about it then – Shubham Dixit Jan 27 '21 at 06:48
  • 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 pm2 – kd12345 Jan 27 '21 at 06:58
  • Please post a separate question with all the details and tag me @kd12345 – Shubham Dixit Jan 27 '21 at 07:14
  • ok thank you, what files should i include in my question default.conf and nginx.conf? – kd12345 Jan 27 '21 at 07:15