Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

node.js - .env file not in Heroku CI/CD with Gitlab

I am trying to deploy a Node.js application to Heroku via a GitLab pipeline. The below is my pipeline code. I have the variables set in the GitLab project. It seems as though the .env file is not uploaded to the Heroku app and the app crashes.

image: node:latest

before_script:
    - apt-get update -qy
    - apt-get install -y ruby-dev
    - gem install dpl
   # - npm link @angular/cli

stages:
  #  - test
    - production


# unit-test:
   # stage: test
   # image: trion/ng-cli-karma:latest
   # script:
     #   - npm install 
    #    - ng test 
  #  only:
 #       - master

production:
    type: deploy
    stage: production
    image: ruby:latest
    script:
        - echo "ACCOUNT_SID=$ACCOUNT_SID" >> .env
        - echo "AUTH_TOKEN=$AUTH_TOKEN" >> .env
        - echo "API_KEY=$API_KEY" >> .env
        - echo "API_SECRET=$API_SECRET" >> .env
        - echo "PHONE_NUMBER=$PHONE_NUMBER" >> .env
        - echo "sengrid_api=$sengrid_api" >> .env
        - dpl --provider=heroku --app=$HEROKU_APP_PRODUCTION --api-key=$HEROKU_API_KEY --skip_cleanup
    only:
        - master

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

It seems as though the .env file is not uploaded to the Heroku app

Nor should it be.

.env files are a convenient mechanism for setting environment variables in development. On Heroku, you should use config vars, which are its convenient mechanism for setting environment variables, e.g.

heroku config:set API_KEY=SOME_API_KEY

Note that you may need to quote values if they contain characters like < or | which are meaningful to whatever shell you are using.

If you need these variables at build time, you can set environment variables as part of your GitLab configuration (committed to your repository) or in group-level secrets (not committed, and in some ways more aligned with the concept of per-environment settings).

Each environment in which you run your application is different and should have its own environment variables. It is normal and expected that they don't follow your application around. This is one of the fundamental principles upon which Heroku's model was designed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...