.env 文件不在 Gitlab 的 Heroku CI/CD 中

.env file not in Heroku CI/CD with Gitlab

我正在尝试通过 GitLab 管道将 Node.js 应用程序部署到 Heroku。下面是我的管道代码。我在 GitLab 项目中设置了变量。 .env 文件似乎没有上传到 Heroku 应用程序并且应用程序崩溃了。

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

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

也不应该。

.env 文件是在开发 中设置环境变量的便捷机制。在 Heroku 上,你应该使用 config vars,这是 设置环境变量的便捷机制,例如

heroku config:set API_KEY=SOME_API_KEY

请注意,如果值包含 <| 之类的字符,这些值对您使用的 shell 有意义。

如果您在构建时需要这些变量,您可以set environment variables as part of your GitLab configuration (committed to your repository) or in group-level secrets(未提交,并且在某些方面更符合每个环境设置的概念)。

您 运行 您的应用程序所在的每个 环境 都是不同的,应该有自己的环境变量。他们不会跟着你的申请走来走去是正常的,也是预料之中的。这是 one of the fundamental principles Heroku 模型的设计基础。

有了这个 .yml 文件,我可以构建我的 docker 图像并同时部署到我的两个 Digital Ocean droplet 上,前面有一个负载均衡器。

# ssh-keyscan gitlab.com >> authorized_keys: use this command to add gitlab ssh keys to sever. Run on server terminal
# cat ~/.ssh/id_rsa.pub >> authorized_keys : Run this command on the sever on the terminal. 
# Both COMMANDS ABOVE ARE necessary.
# Have to put .env echo statments in Docker build stage because documents dont persist. Artifacts could be used.

stages:
  - build
  - deploy

variables:
  TAG_LATEST: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:latest
  TAG_COMMIT: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:$CI_COMMIT_SHA


build-App:
  image: docker:latest
  stage: build
  services:
    - docker:dind
  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
    - cat .env
    - docker build . -t $TAG_COMMIT -t $TAG_LATEST 
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
    - docker push $TAG_COMMIT
    - docker push $TAG_LATEST

deploy-1:
  image: ubuntu:latest
  stage: deploy
  tags:
    - deployment
  before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'
  - eval $(ssh-agent -s)
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
  - echo "$SSH_PUBLIC_KEY" | tr -d '\r' > ~/.ssh/id_rsa.pub
  - chmod 600 ~/.ssh/*
  - chmod 644 ~/.ssh/*.pub
  - ssh-add
  - ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts
  - ls -ld ~/.ssh/*
  script:
    - ssh   -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY"
    - ssh  -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker pull $TAG_COMMIT"
    - ssh  -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker container rm -f my-app || true"
    - ssh  -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker run -d -p 3000:3000 --name my-app $TAG_COMMIT"
  environment:
    name: production
    url: http://134.122.23.185
  only:
    - master

deploy-2:
  image: ubuntu:latest
  stage: deploy
  tags:
    - deployment-backup
  before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'
  - eval $(ssh-agent -s)
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - echo "$SSH_PRIVATE_KEY_BACKUP" | tr -d '\r' > ~/.ssh/id_rsa
  - echo "$SSH_PUBLIC_KEY_BACKUP" | tr -d '\r' > ~/.ssh/id_rsa.pub
  - chmod 600 ~/.ssh/*
  - chmod 644 ~/.ssh/*.pub
  - ssh-add
  - ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts
  - ls -ld ~/.ssh/*
  script:
    - ssh   -o StrictHostKeyChecking=no $SERVER_USER_BACKUP@$SERVER_IP_BACKUP "docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY"
    - ssh  -o StrictHostKeyChecking=no $SERVER_USER_BACKUP@$SERVER_IP_BACKUP "docker pull $TAG_COMMIT"
    - ssh  -o StrictHostKeyChecking=no $SERVER_USER_BACKUP@$SERVER_IP_BACKUP "docker container rm -f my-app || true"
    - ssh  -o StrictHostKeyChecking=no $SERVER_USER_BACKUP@$SERVER_IP_BACKUP "docker run -d -p 3000:3000 --name my-app $TAG_COMMIT"
  environment:
    name: production-backup
    url: http://161.35.123.72
  only:
    - master