From 9bb72d32529fc6ff24cc157816efee630e003bb9 Mon Sep 17 00:00:00 2001 From: Mingyu Park Date: Sat, 17 Aug 2024 23:59:38 +0900 Subject: [PATCH] Add Dockerfile & Modify github action file --- .github/workflows/django.yml | 25 ++++++++++++++++++++++--- backend/Dockerfile | 19 +++++++++++++++++++ backend/categories/urls.py | 6 ++++++ backend/categories/views.py | 13 +++++++++++-- backend/config/settings.py | 14 +++++++++++--- backend/config/urls.py | 3 ++- 6 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 backend/Dockerfile create mode 100644 backend/categories/urls.py diff --git a/.github/workflows/django.yml b/.github/workflows/django.yml index c9b9ace..7625bfe 100644 --- a/.github/workflows/django.yml +++ b/.github/workflows/django.yml @@ -1,4 +1,4 @@ -name: Django CI +name: Django CI/CD on: push: @@ -8,13 +8,11 @@ on: jobs: build: - runs-on: ubuntu-latest strategy: max-parallel: 4 matrix: python-version: ['3.9', '3.10', '3.11', '3.12'] - steps: - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} @@ -30,3 +28,24 @@ jobs: run: | cd backend python manage.py test + + docker: + needs: build + runs-on: ubuntu-latest + if: github.event_name == 'pull_request' && github.event.pull_request.base.ref == 'main' + steps: + - uses: actions/checkout@v4 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Build and push + uses: docker/build-push-action@v2 + with: + context: ./backend + file: ./backend/Dockerfile + push: true + tags: ${{ secrets.DOCKERHUB_USERNAME }}/leafy-backend:${{ github.sha }} \ No newline at end of file diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..2cc132b --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,19 @@ +# Use an official Python runtime as a parent image +FROM python:3.9-slim + +# Set environment variables +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +# Set work directory +WORKDIR /app + +# Install dependencies +COPY requirements.txt /app/ +RUN pip install --upgrade pip && pip install -r requirements.txt + +# Copy project +COPY . /app/ + +# Run the application +CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"] \ No newline at end of file diff --git a/backend/categories/urls.py b/backend/categories/urls.py new file mode 100644 index 0000000..4bfdf2d --- /dev/null +++ b/backend/categories/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path("", views.categories), +] \ No newline at end of file diff --git a/backend/categories/views.py b/backend/categories/views.py index 91ea44a..c1c7e75 100644 --- a/backend/categories/views.py +++ b/backend/categories/views.py @@ -1,3 +1,12 @@ -from django.shortcuts import render +from rest_framework.decorators import api_view +from rest_framework.response import Response +from .models import Category -# Create your views here. +@api_view() +def categories(request): + return Response( + { + "ok": True, + "categories": Category.objects.all() + } + ) \ No newline at end of file diff --git a/backend/config/settings.py b/backend/config/settings.py index 07d1f43..4b394ef 100644 --- a/backend/config/settings.py +++ b/backend/config/settings.py @@ -29,14 +29,20 @@ # Application definition - -INSTALLED_APPS = [ +SYSTEM_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', +] + +THIRD_PARTY_APPS = [ + 'rest_framework', +] + +CUSTOM_APPS = [ 'users.apps.UsersConfig', 'rooms.apps.RoomsConfig', 'common.apps.CommonConfig', @@ -46,9 +52,11 @@ 'wishlists.apps.WishlistsConfig', 'bookings.apps.BookingsConfig', 'medias.apps.MediasConfig', - 'direct_messages.apps.DirectMessagesConfig' + 'direct_messages.apps.DirectMessagesConfig', ] +INSTALLED_APPS = SYSTEM_APPS + THIRD_PARTY_APPS + CUSTOM_APPS + MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', diff --git a/backend/config/urls.py b/backend/config/urls.py index 5006338..e5eac28 100644 --- a/backend/config/urls.py +++ b/backend/config/urls.py @@ -15,8 +15,9 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import include, path urlpatterns = [ path('admin/', admin.site.urls), + path("categories/", include("categories.urls")), ]