Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
  
like image 317
gANDALF Avatar asked May 18 '26 14:05

gANDALF


1 Answers

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.

like image 142
Shubh Avatar answered May 20 '26 04:05

Shubh