Search code examples
node.jscontinuous-integrationgitlabpm2

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
  

Solution

  • 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.