다른 브랜치를 사용하여 Heroku에 Django 배포

문제



Django News 프로젝트를 Heroku에 배포하려고 했습니다. 그러나 나는 그것을 배포할 수 없었습니다. 매번 실패했습니다. 빌드 실패를 제공합니다.

투쟁



나는 cookiecutter-django 지시를 따랐다:

$ heroku create --buildpack https://github.com/heroku/heroku-buildpack-python

$ heroku addons:create heroku-postgresql:hobby-dev
# On Windows use double quotes for the time zone, e.g.
# heroku pg:backups schedule --at "02:00 America/Los_Angeles" DATABASE_URL
$ heroku pg:backups schedule --at '02:00 America/Los_Angeles' DATABASE_URL
$ heroku pg:promote DATABASE_URL

$ heroku addons:create heroku-redis:hobby-dev

$ heroku addons:create mailgun:starter

$ heroku config:set PYTHONHASHSEED=random

$ heroku config:set WEB_CONCURRENCY=4

$ heroku config:set DJANGO_DEBUG=False
$ heroku config:set DJANGO_SETTINGS_MODULE=config.settings.production
$ heroku config:set DJANGO_SECRET_KEY="$(openssl rand -base64 64)"

# Generating a 32 character-long random string without any of the visually similar characters "IOl01":
$ heroku config:set DJANGO_ADMIN_URL="$(openssl rand -base64 4096 | tr -dc 'A-HJ-NP-Za-km-z2-9' | head -c 32)/"

# Set this to your Heroku app url, e.g. 'bionic-beaver-28392.herokuapp.com'
$ heroku config:set DJANGO_ALLOWED_HOSTS=

# Assign with AWS_ACCESS_KEY_ID
$ heroku config:set DJANGO_AWS_ACCESS_KEY_ID=

# Assign with AWS_SECRET_ACCESS_KEY
$ heroku config:set DJANGO_AWS_SECRET_ACCESS_KEY=

# Assign with AWS_STORAGE_BUCKET_NAME
$ heroku config:set DJANGO_AWS_STORAGE_BUCKET_NAME=

$ git push heroku master

$ heroku run python manage.py createsuperuser

$ heroku run python manage.py check --deploy

$ heroku open

push 단계, 37행까지 모든 것이 정상이었습니다. 이 행은 Build Failed 오류를 제공합니다. Heroku의 단락 출력은 다음과 같습니다.

-----> Python app detected
 !     Python has released a security update! Please consider upgrading to python-3.8.5
       Learn More: https://devcenter.heroku.com/articles/python-runtimes
-----> Installing python-3.8.3
-----> Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2
-----> Installing SQLite3
-----> Installing requirements with pip
       Collecting pytz==2020.1
         Downloading pytz-2020.1-py2.py3-none-any.whl (510 kB)
       Collecting python-slugify==4.0.1
         Downloading python-slugify-4.0.1.tar.gz (11 kB)

         ...
         BLA BLA BLA
         ...

           Running setup.py install for psycopg2: started
           Running setup.py install for psycopg2: finished with status 'done'
       Successfully installed Collectfast-2.2.0 Pillow-7.2.0 amqp-2.6.1 argon2-cffi-20.1.0 asgiref-3.2.10 billiard-3.6.3.0 cachetools-4.1.1 celery-4.4.6 certifi-2020.6.20 cffi-1.14.2 chardet-3.0.4 defusedxml-0.6.0 django-3.0.8 django-allauth-0.42.0 django-anymail-7.1.0 django-celery-beat-2.0.0 django-crispy-forms-1.9.1 django-environ-0.4.5 django-js-asset-1.2.2 django-model-utils-4.0.0 django-mptt-0.10.0 django-ratelimit-2.0.0 django-redis-4.12.1 django-storages-1.9.1 django-timezone-field-4.0 djangorestframework-3.11.0 flower-0.9.5 future-0.18.2 google-api-core-1.22.1 google-auth-1.20.1 google-cloud-core-1.4.1 google-cloud-storage-1.31.0 google-crc32c-1.0.0 google-resumable-media-1.0.0 googleapis-common-protos-1.52.0 gunicorn-20.0.4 hiredis-1.0.1 humanize-2.6.0 idna-2.10 kombu-4.6.11 mistune-0.8.4 oauthlib-3.1.0 prometheus-client-0.8.0 protobuf-3.13.0 psycopg2-2.8.5 pyasn1-0.4.8 pyasn1-modules-0.2.8 pycparser-2.20 python-crontab-2.5.1 python-dateutil-2.8.1 python-slugify-4.0.1 python3-openid-3.2.0 pytz-2020.1 redis-3.5.3 requests-2.24.0 requests-oauthlib-1.3.0 rsa-4.6 sentry-sdk-0.16.1 six-1.15.0 sqlparse-0.3.1 text-unidecode-1.3 tornado-6.0.4 typing-extensions-3.7.4.3 urllib3-1.25.10 vine-1.3.0
-----> $ python manage.py collectstatic --noinput
       Traceback (most recent call last):
         File "/app/.heroku/python/lib/python3.8/site-packages/environ/environ.py", line 273, in get_value
           value = self.ENVIRON[var]
         File "/app/.heroku/python/lib/python3.8/os.py", line 675, in __getitem__
           raise KeyError(key) from None
       KeyError: 'DJANGO_GCP_STORAGE_BUCKET_NAME'
       During handling of the above exception, another exception occurred:
       Traceback (most recent call last):
         File "manage.py", line 31, in <module>
           execute_from_command_line(sys.argv)
         File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
           utility.execute()
         File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
           self.fetch_command(subcommand).run_from_argv(self.argv)
         File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 231, in fetch_command
           settings.INSTALLED_APPS
         File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 76, in __getattr__
           self._setup(name)
         File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 63, in _setup
           self._wrapped = Settings(settings_module)
         File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 142, in __init__
           mod = importlib.import_module(self.SETTINGS_MODULE)
         File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
           return _bootstrap._gcd_import(name[level:], package, level)
         File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
         File "<frozen importlib._bootstrap>", line 991, in _find_and_load
         File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
         File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
         File "<frozen importlib._bootstrap_external>", line 783, in exec_module
         File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
         File "/tmp/build_9bb85fa2/config/settings/production.py", line 69, in <module>
           GS_BUCKET_NAME = env("DJANGO_GCP_STORAGE_BUCKET_NAME")
         File "/app/.heroku/python/lib/python3.8/site-packages/environ/environ.py", line 123, in __call__
           return self.get_value(var, cast=cast, default=default, parse_default=parse_default)
         File "/app/.heroku/python/lib/python3.8/site-packages/environ/environ.py", line 277, in get_value
           raise ImproperlyConfigured(error_msg)
       django.core.exceptions.ImproperlyConfigured: Set the DJANGO_GCP_STORAGE_BUCKET_NAME environment variable
 !     Error while running '$ python manage.py collectstatic --noinput'.
       See traceback above for details.
       You may need to update application code to resolve this error.
       Or, you can disable collectstatic for this application:
          $ heroku config:set DISABLE_COLLECTSTATIC=1
       https://devcenter.heroku.com/articles/django-assets
 !     Push rejected, failed to compile Python app.
 !     Push failed


다음과 같은 오류 로그를 볼 수 있습니다. 변수DJANGO_GCP_STORAGE_BUCKET_NAME가 설정되지 않았습니다. 예, 설정하지 않아야 합니다.

프로젝트 초기에 저는 Google Cloud Platform을 "클라우드 공급자"로 선택했습니다. 그러나 단순하고 사용하기 쉽도록 정적 파일을 제공하기 위해 WhiteNoise으로 전환했습니다.

그러나 "생산 설정"에 설정된 variable를 이미 삭제했습니다.

# configs/settings/production.py
GS_BUCKET_NAME = env("DJANGO_GCP_STORAGE_BUCKET_NAME")


그렇다면 Heroku의 문제는 무엇입니까? 프로젝트의 이전 커밋을 사용하는 이유는 무엇입니까?

나는 해결책을 찾기 위해 거의 2시간 동안 고군분투했다.

해결책



해결책은 Heroku에 branch you push를 지정하는 것이 었습니다. 기본 명령입니다.

$ git push heroku master

master 분기를 배포의 master 분기로 푸시합니다. 그러나 나는 master 가 아니라 heroku 분기에 있습니다. 이 분기는 수십 개의 커밋이 있는 현재 분기입니다.

doc Deploying from a branch besides master:Heroku 에서 언급했듯이 푸시하도록 분기를 변경했습니다.

$ git push heroku heroku:master


그리고 이제 성공이었습니다. 고통이 풀렸습니다.

모두 완료되었습니다!

좋은 웹페이지 즐겨찾기