将 .env 文件添加到 gitignore 后,Django elastic beanstalk CLI 部署失败

Django elastic beanstalk CLI deployment failure after adding .env files to gitignore

我是 Django/EB/Git 的新手,一直在做一个 django 项目,并成功地分离了我的设置并分离了用于开发和生产的 .env 文件,所有这些都按预期工作和部署 - 请参阅以下项目结构:

项目结构

project root
    myapp
        settings
            __init__
            base.py
            dev.py
            prod.py
.env.dev
.env.prod
.gitignore
manage.py
requiremnts.txt

然而,当我将我的 .env 文件添加到 .gitignore 文件时,我现在在 eb 日志 (cfn-init-cmd.log) 中收到以下部署错误:

.git忽略

# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
.env.dev
.env.prod

错误:eb 日志 (cfn-init-cmd.log) FileNotFoundError: [Errno 2] 没有这样的文件或目录: '.env.prod'

如果我从 .gitignore 文件中删除 .env.prod,那么我的项目部署成功。

此外,我在网上读到这可能是由于我 git 添加并提交了 .env.prod 文件到回购协议,但相信我也排除了 git add/commit 当我重新开始并使用以下命令重新创建 git 存储库时(本地项目上的命令 运行):

git add --all -- :!.env.dev :!.env.prod
git commit -m "Initial commit"

其次是:

eb deploy myproject-env

查看我的 .ebextensions 配置文件如下:

.ebextensions/django.config

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: myproject.wsgi:application
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: "myproject.settings.prod"
  aws:elasticbeanstalk:environment:proxy:staticfiles:
    "/static": "static/"
packages:
  yum:
    python3-devel: []
    mariadb-devel: []
container_commands:
  01_collectstatic:
    command: "source /var/app/venv/staging-LQM1lest/bin/activate && python manage.py collectstatic --noinput"
  02_migrate:
    command: "source /var/app/venv/staging-LQM1lest/bin/activate && python manage.py migrate --noinput"
    leader_only: true

我不确定是否应该将任何 git 命令添加到我的 .ebextensions 配置中,但假设它只是在本地 git 存储库上完成,然后推送到 github,我也尝试过使用和不使用 codecommit 进行部署,但对上述内容没有影响。

我花了大约一周的时间弄清楚这一切并最终能够部署,我认为这应该是将 .env 文件添加到 .gitignore 文件的最后一步,我'我只是不确定我遗漏了什么或是否正确地使用 git 存储库做了一些事情。

非常感谢help/guidance,提前致谢。

如果 .ebignore 文件不存在,Elastic beanstalk 使用 .gitignore 文件。所以您可以将两者都用于您的文件管理。

AWS doc 说:

You can tell the EB CLI to ignore certain files in your project directory by adding the file .ebignore to the directory. This file works like a .gitignore file.

...

If .ebignore isn't present, but .gitignore is, the EB CLI ignores files specified in .gitignore. If .ebignore is present, the EB CLI doesn't read .gitignore.

When .ebignore is present, the EB CLI doesn't use git commands to create your source bundle. This means that EB CLI ignores files specified in .ebignore, and includes all other files. In particular, it includes uncommitted source files.