From c8f328335df2f7443a2db2f7bfd3e05ad5f7d859 Mon Sep 17 00:00:00 2001 From: AlexandraPoturaeva Date: Sun, 19 Nov 2023 10:30:08 +0300 Subject: [PATCH 1/6] add all env files to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 8afda7e..20f95c1 100644 --- a/.gitignore +++ b/.gitignore @@ -122,6 +122,7 @@ celerybeat.pid # Environments .env +.env.* .venv env/ venv/ From 86ec23f9c31c54f6e461d3bbaf26ff842750e8f6 Mon Sep 17 00:00:00 2001 From: AlexandraPoturaeva Date: Sun, 19 Nov 2023 15:35:38 +0300 Subject: [PATCH 2/6] set settings for deployment to render.com --- build.sh | 8 ++++++ ...ation_options_alter_participant_options.py | 21 +++++++++++++++ requirements.txt | 4 ++- sports_events/settings.py | 27 ++++++++++--------- 4 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 build.sh create mode 100644 events/migrations/0004_alter_application_options_alter_participant_options.py diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..78112c6 --- /dev/null +++ b/build.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -o errexit # exit on error + +pip install -r requirements.txt + +python manage.py collectstatic --no-input +python manage.py migrate \ No newline at end of file diff --git a/events/migrations/0004_alter_application_options_alter_participant_options.py b/events/migrations/0004_alter_application_options_alter_participant_options.py new file mode 100644 index 0000000..48653b0 --- /dev/null +++ b/events/migrations/0004_alter_application_options_alter_participant_options.py @@ -0,0 +1,21 @@ +# Generated by Django 4.2.6 on 2023-11-19 11:38 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('events', '0003_application_participant'), + ] + + operations = [ + migrations.AlterModelOptions( + name='application', + options={'get_latest_by': 'created_at', 'ordering': ['-created_at']}, + ), + migrations.AlterModelOptions( + name='participant', + options={'get_latest_by': 'created_at', 'ordering': ['last_name']}, + ), + ] diff --git a/requirements.txt b/requirements.txt index a95b0fa..e497d8a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,11 @@ add-trailing-comma==3.1.0 +dj-database-url==2.1.0 Django==4.2.6 django-extensions==3.2.3 flake8==6.1.0 -Pillow==10.1.0 +gunicorn==21.2.0 pre-commit==3.5.0 +psycopg2-binary==2.9.9 pytest-django==4.6.0 python-dotenv==1.0.0 social-auth-app-django==5.4.0 diff --git a/sports_events/settings.py b/sports_events/settings.py index 0fd2677..24c30b6 100644 --- a/sports_events/settings.py +++ b/sports_events/settings.py @@ -13,7 +13,13 @@ import os from pathlib import Path from dotenv import load_dotenv -load_dotenv() +import dj_database_url + +ENVIRONMENT = os.getenv('ENV') +if ENVIRONMENT == 'prod': + load_dotenv('.env.prod') +else: + load_dotenv() # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -22,28 +28,28 @@ # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-0rqld6-pd0(50pe=tqw_b3fwd*2b4lv4s%ygz1@q=xvez#6#12' +SECRET_KEY = os.getenv('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = os.getenv('DEBUG') ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ - 'users', - 'core', - 'events', - + 'django.contrib.sessions', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', - 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'social_django', 'django_extensions', + + 'core', + 'users', + 'events', ] MIDDLEWARE = [ @@ -87,10 +93,7 @@ # https://docs.djangoproject.com/en/4.2/ref/settings/#databases DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', - }, + 'default': dj_database_url.config(conn_max_age=600, conn_health_checks=True), } # Password validation From 0b8f52ba0cc2cf6159dc5ca86debca9d50758c7f Mon Sep 17 00:00:00 2001 From: AlexandraPoturaeva Date: Sun, 19 Nov 2023 16:15:50 +0300 Subject: [PATCH 3/6] fix locale.Error --- events/services.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/events/services.py b/events/services.py index f3e0e58..5bf84d6 100644 --- a/events/services.py +++ b/events/services.py @@ -1,7 +1,6 @@ from events.models import Event from core.models import Region import time -import locale import calendar @@ -18,7 +17,6 @@ def generate_region_choices(): def generate_month_choices(): - locale.setlocale(locale.LC_ALL, 'ru_RU') now = time.localtime() year_month_raw = [ time.localtime( From 8ca16025a1a312f5553a7ec7146a0e5eed57abd7 Mon Sep 17 00:00:00 2001 From: AlexandraPoturaeva Date: Sun, 19 Nov 2023 16:19:34 +0300 Subject: [PATCH 4/6] add pillow to requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index e497d8a..a1715b7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,7 @@ Django==4.2.6 django-extensions==3.2.3 flake8==6.1.0 gunicorn==21.2.0 +Pillow==10.1.0 pre-commit==3.5.0 psycopg2-binary==2.9.9 pytest-django==4.6.0 From af1e7dbd4ba023292cd4212c7a94d9dfeeaf3555 Mon Sep 17 00:00:00 2001 From: AlexandraPoturaeva Date: Sun, 19 Nov 2023 16:45:39 +0300 Subject: [PATCH 5/6] change settings for allowed hosts --- sports_events/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sports_events/settings.py b/sports_events/settings.py index 24c30b6..256e980 100644 --- a/sports_events/settings.py +++ b/sports_events/settings.py @@ -33,7 +33,7 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = os.getenv('DEBUG') -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS').split(' ') # Application definition From 834a8ef6dd3054c41ce55c1180cd27a158fcd1b0 Mon Sep 17 00:00:00 2001 From: AlexandraPoturaeva Date: Tue, 21 Nov 2023 10:39:02 +0300 Subject: [PATCH 6/6] create and publish a Docker image in the Container registry on GitHub --- .dockerignore | 40 +++++++++++++++++++++++++ .github/workflows/deploy-image.yml | 48 ++++++++++++++++++++++++++++++ Dockerfile | 17 +++++++++++ build.sh | 8 ----- events/services.py | 2 ++ 5 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/deploy-image.yml create mode 100644 Dockerfile delete mode 100644 build.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..ac7306d --- /dev/null +++ b/.dockerignore @@ -0,0 +1,40 @@ +# Include any files or directories that you don't want to be copied to your +# container here (e.g., local build artifacts, temporary files, etc.). +# +# For more help, visit the .dockerignore file reference guide at +# https://docs.docker.com/engine/reference/builder/#dockerignore-file + +**/.DS_Store +**/__pycache__ +**/.venv +**/.classpath +**/.dockerignore +**/.env +**/.env.* +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/bin +**/charts +**/docker-compose* +**/compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md +**/*.bat +**/*.sample +venv/ +.idea/ +.pytest_cache/ diff --git a/.github/workflows/deploy-image.yml b/.github/workflows/deploy-image.yml new file mode 100644 index 0000000..ad3b003 --- /dev/null +++ b/.github/workflows/deploy-image.yml @@ -0,0 +1,48 @@ +# +name: Create and publish a Docker image + +# Configures this workflow to run every time a change is pushed to the branch called `release`. +on: + push: + branches: ['release'] + +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. +jobs: + build-and-push-image: + runs-on: ubuntu-latest + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + # + steps: + - name: Checkout repository + uses: actions/checkout@v4 + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. + - name: Build and push Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..35008b0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM python:3.11-alpine + +EXPOSE 8000 + +WORKDIR /app + +RUN python -m pip install --upgrade pip + +COPY requirements.txt /app + +RUN python -m pip install -r requirements.txt + +COPY . /app + +ENTRYPOINT ["python"] + +CMD ["manage.py", "runserver", "0.0.0.0:8000"] \ No newline at end of file diff --git a/build.sh b/build.sh deleted file mode 100644 index 78112c6..0000000 --- a/build.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash - -set -o errexit # exit on error - -pip install -r requirements.txt - -python manage.py collectstatic --no-input -python manage.py migrate \ No newline at end of file diff --git a/events/services.py b/events/services.py index 5bf84d6..f3e0e58 100644 --- a/events/services.py +++ b/events/services.py @@ -1,6 +1,7 @@ from events.models import Event from core.models import Region import time +import locale import calendar @@ -17,6 +18,7 @@ def generate_region_choices(): def generate_month_choices(): + locale.setlocale(locale.LC_ALL, 'ru_RU') now = time.localtime() year_month_raw = [ time.localtime(