Skip to content

Commit 764cd60

Browse files
committed
Added Docker and CI CD Pipeline with AWS Integration
1 parent 5dbdd5d commit 764cd60

File tree

8 files changed

+116
-11
lines changed

8 files changed

+116
-11
lines changed

.github/workflows/deploy.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Deploy on AWS
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
build-and-deploy:
10+
runs-on:ubuntu-latest
11+
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
16+
- name: Setup Docker Buildx
17+
uses: docker/setup-buildx-action@v1
18+
19+
- name: Login to Dockerhub
20+
uses: docker/login-action@v1
21+
with:
22+
username: ${{ secrets.DOCKER_USERNAME }}
23+
password: ${{ secrets.DOCKER_PASSWORD }}
24+
25+
- name: Build and push Docker image
26+
run : |
27+
docker build -t ${{ secrets.DOCKER_USERNAME }}/ecommerce-backend:latest .
28+
docker push ${{ secrets.DOCKER_USERNAME }}/ecommerce-backend:latest
29+
30+
- name: Setup SSH
31+
run: |
32+
mkdir -p ~/.ssh/
33+
echo "${{ secrets.AWS_EC2_KEY }}" > ~/.ssh/id_rsa
34+
chmod 600 ~/.ssh/id_rsa
35+
ssh-keyscan -H ${{ secrets.AWS_EC2_IP }} >> ~/.ssh/known_hosts
36+
37+
- name: Deploy on AWS EC2
38+
run: |
39+
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ubuntu@${{ secrets.AWS_EC2_IP }} << 'EOF'
40+
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
41+
docker pull ${{ secrets.DOCKER_USERNAME }}/ecommerce-backend:latest
42+
docker stop ecommerce-backend || true
43+
docker rm ecommerce-backend || true
44+
docker run -d --name ecommerce-backend -p 80:8000 ${{ secrets.DOCKER_USERNAME }}/ecommerce-backend:latest
45+
EOF

Backend/EcommerceInventory/.env

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
DEBUG='True'
22
SECRET_KEY='django-insecure-n==@#u@5goj0o27k8isfo%6!k3fs(2u@3r7+n!ovu-uu1#p=gd'
3-
DATABASE_NAME='ecommerceproject'
4-
DATABASE_USER='ecommerceproject'
5-
DATABASE_PASSWORD='ecommerceproject'
6-
DATABASE_HOST='localhost'
3+
DATABASE_NAME='ecommerce'
4+
DATABASE_USER='ecommerce'
5+
DATABASE_PASSWORD='ecommerce'
6+
DATABASE_HOST='ecommerce.cfamkce00xa3.ap-south-1.rds.amazonaws.com'
77
DATABASE_PORT='3306'

Backend/EcommerceInventory/EcommerceInventory/settings.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# SECURITY WARNING: don't run with debug turned on in production!
3030
DEBUG = os.getenv('DEBUG','True') == 'True'
3131

32-
ALLOWED_HOSTS = []
32+
ALLOWED_HOSTS = ["*"]
3333

3434

3535
# Application definition
@@ -76,7 +76,7 @@
7676
TEMPLATES = [
7777
{
7878
'BACKEND': 'django.template.backends.django.DjangoTemplates',
79-
'DIRS': [],
79+
'DIRS': ['EcommerceInventory/templates'],
8080
'APP_DIRS': True,
8181
'OPTIONS': {
8282
'context_processors': [
@@ -142,6 +142,10 @@
142142
# https://docs.djangoproject.com/en/5.0/howto/static-files/
143143

144144
STATIC_URL = '/static/'
145+
STATIC_ROOT = os.path.join(BASE_DIR,'staticfiles')
146+
STATICFILES_DIRS=[
147+
os.path.join(BASE_DIR,'static'),
148+
]
145149

146150
# Default primary key field type
147151
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

Backend/EcommerceInventory/EcommerceInventory/urls.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
from django.contrib import admin
1818
from django.urls import include, path, re_path
1919

20+
from EcommerceInventory.views import index
21+
from EcommerceInventory import settings
2022
from UserServices.Controller.DynamicFormController import DynamicFormController
2123
from UserServices.Controller.SuperAdminDynamicFormController import SuperAdminDynamicFormController
2224
from UserServices.Controller.SidebarController import ModuleView
25+
from django.conf.urls.static import static
2326

2427
urlpatterns = [
2528
path('admin/', admin.site.urls),
@@ -28,6 +31,11 @@
2831
path('api/superAdminForm/<str:modelName>/',SuperAdminDynamicFormController.as_view(),name='superadmindynamicForm'),
2932
path('api/getMenus/',ModuleView.as_view(),name='sidebarmenu'),
3033
path('api/products/',include('ProductServices.urls')),
31-
re_path(r'^(?:.*)/?$', index),
32-
3334
]
35+
36+
if settings.DEBUG:
37+
urlpatterns+=static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
38+
39+
urlpatterns+=[
40+
re_path(r'^(?:.*)/?$',index,name='index')
41+
]
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
from django.shortcuts import render
2-
32
def index(request):
4-
return render(request, 'index.html')
3+
return render(request, 'index.html')
32 Bytes
Binary file not shown.

Dockerfile

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#Stage 1:Build Frontend
2+
FROM node:18 as build-stage
3+
4+
WORKDIR /code
5+
6+
COPY ./Frontend/ecommerce_inventory/ /code/Frontend/ecommerce_inventory/
7+
8+
WORKDIR /code/Frontend/ecommerce_inventory
9+
10+
#Installing packages
11+
RUN npm install
12+
13+
#Building the frontend
14+
RUN npm run build
15+
16+
17+
#Stage 2:Build Backend
18+
FROM python:3:11:0
19+
20+
#Set Environment Variables
21+
ENV PYTHONDONTWRITEBYTECODE 1
22+
ENV PYTHONUNBUFFERED 1
23+
24+
WORKDIR /code
25+
26+
#Copy Django Project to the container
27+
COPY ./Backend/EcommerceInventory /code/Backend/EcommerceInventory/
28+
29+
#Install the required packages
30+
RUN pip install -r ./Backend/EcommerceInventory/requirements.txt
31+
32+
#Copy the frontend build to the Django project
33+
COPY --from=build-stage ./code/Frontend/ecommerce_inventory/build /code/Backend/EcommerceInventory/static/
34+
COPY --from=build-stage ./code/Frontend/ecommerce_inventory/build/static /code/Backend/EcommerceInventory/static/
35+
COPY --from=build-stage ./code/Frontend/ecommerce_inventory/build/index.html /code/Backend/EcommerceInventory/EcommerceInventory/templates/index.html
36+
37+
#Run Django Migration Command
38+
RUN python ./Backend/EcommerceInventory/manage.py migrate
39+
40+
#Run Django Collectstatic Command
41+
RUN python ./Backend/EcommerceInventory/manage.py collectstatic --no-input
42+
43+
#Expose the port
44+
EXPOSE 80
45+
46+
WORKDIR /code/Backend/EcommerceInventory
47+
48+
#Run the Django Server
49+
CMD ["gunicorn","EcommerceInventory.wsgi.application","--bind","0.0.0.0:8000"]

Frontend/ecommerce_inventory/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
REACT_APP_API_URL='http://localhost:8000/api/'
1+
REACT_APP_API_URL='http://ec2-3-110-164-200.ap-south-1.compute.amazonaws.com/api/'

0 commit comments

Comments
 (0)