-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathCI_CD_Deployment_Step_Using_Docker_AWS.txt
207 lines (148 loc) · 6.33 KB
/
CI_CD_Deployment_Step_Using_Docker_AWS.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
Step 1: Signup in AWS Account for Free Tier
Step 1A: Create Docker Account Keep save your username and password for future use
Step 2: Create EC2 Instance using Ubuntu Image
Step 3: Create RDS for Database -> Select MySQL -> Security Group allow Inbound and Outbound Rules
Step 4: Login to EC2 using Connect
Step 5: Install Docker
Step 6: Download mySql Workbench
Step 7: Add gunicorn==22.0.0 in requirements.txt
-----------------------------------Install Docker Code for EC2-----------------------------
sudo apt update
sudo apt install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker
# Install Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# Add your user to the docker group
sudo usermod -aG docker $USER
-------------------------Then Open Port 80 and 8000 for Django in EC2 -------------------------
sudo ufw allow 8000
sudo ufw allow 80
-----------Install MySQL CLient Like Workbench or In VScode then Connect Database Server-------
Create Database From Explore
----------------------------Project Settings and Configurations-------------------------------
.env -> Update Database Setting in .env File
-------------------------------------------------
settings.py
ALLOWED_HOSTS = ["*"]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') # Directory for collectstatic
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'), # Directory for static files used in development
]
TEMPLATES = [
{ .......
'DIRS': ['EcommerceInventory/templates'], #Add this Template Directory For React
}
--------------------------------------------------
urls.py
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
# Add the catch-all pattern at the end
urlpatterns += [
re_path(r'^(?:.*)/?$', index),
]
def index(request):
return render(request, 'index.html')
Create templates Directory in Project Folder
--------------------Update API Url in React .env file---------------------------
REACT_APP_API_URL='API_URL'
-------------------Updating index.html Content of React------------------------
-----------------Create Dockerfile in Parent Directory------------------------
# Stage 1: Build frontend
FROM node:18 as build-stage
WORKDIR /code
# # Set working directory for frontend
# WORKDIR /app/frontend
# # Copy frontend package files
COPY ./Frontend/ecommerce_inventory/ /code/Frontend/ecommerce_inventory/
WORKDIR /code/Frontend/ecommerce_inventory/
# # Install frontend dependencies
RUN npm install
# # Copy frontend source code
# COPY ./Frontend/ecommerce_inventory ./
# # Build frontend (adjust this based on your React build process)
RUN npm run build
# Stage 2: Build Django backend
FROM python:3.11.0
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /code
# Set working directory for backend
# Copy backend requirements file
# COPY ./Backend/EcommerceInventory/requirements.txt /code/
# Copy built frontend to Django static files directory
# Copy Django project files
COPY ./Backend/EcommerceInventory /code/Backend/EcommerceInventory/
RUN pip install -r ./Backend/EcommerceInventory/requirements.txt
COPY --from=build-stage ./code/Frontend/ecommerce_inventory/build ./Backend/EcommerceInventory/static/
COPY --from=build-stage ./code/Frontend/ecommerce_inventory/build/static ./Backend/EcommerceInventory/static/
COPY --from=build-stage ./code/Frontend/ecommerce_inventory/build/index.html ./Backend/EcommerceInventory/EcommerceInventory/templates/index.html
# Collect static files
RUN python ./Backend/EcommerceInventory/manage.py migrate
RUN python ./Backend/EcommerceInventory/manage.py collectstatic --no-input
# Expose port 80 (adjust as necessary)
EXPOSE 80
WORKDIR /code/Backend/EcommerceInventory
# Command to run Django server
CMD ["gunicorn", "EcommerceInventory.wsgi:application", "--bind", "0.0.0.0:8000"]
-------------Create .github/workflows/deploy.yml file-----------------------------
name: Deploy to AWS
on:
push:
branches:
- TEST
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Log in to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker images
run: |
docker build -t ${{ secrets.DOCKER_USERNAME }}/ecommerce-backend:latest .
docker push ${{ secrets.DOCKER_USERNAME }}/ecommerce-backend:latest
- name: Set up SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.AWS_EC2_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H ${{ secrets.AWS_EC2_IP }} >> ~/.ssh/known_hosts
# - name: Copy docker-compose.yml to EC2
# run: |
# scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa docker-compose.yml ubuntu@${{ secrets.AWS_EC2_IP }}:/home/ubuntu/docker-compose.yml
# - name: Copy Dockerfile to EC2
# run: |
# scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa Dockerfile ubuntu@${{ secrets.AWS_EC2_IP }}:/home/ubuntu/Dockerfile
- name: Deploy to EC2
run: |
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ubuntu@${{ secrets.AWS_EC2_IP }} << 'EOF'
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} ${{ secrets.DOCKER_USERNAME }}
docker pull ${{ secrets.DOCKER_USERNAME }}/ecommerce-backend:latest
docker stop ecommerce-backend || true
docker rm ecommerce-backend || true
docker run -d --name ecommerce-backend -p 80:8000 ${{ secrets.DOCKER_USERNAME }}/ecommerce-backend:latest
EOF
-----------------Add Setting variables in Github---------------------------------
Repo -> Settings-> Security -> Secrets and variables -> Actions -> New Repository Secret
DOCKER_USERNAME
DOCKER_PASSWORD
AWS_EC2_IP
AWS_EC2_KEY -> key Pair Contents
--------------------Docker Commands ----------------------------------------
List all Running Container :
docker ps
List all Container:
docker ps -a
Enter Docker Image Shell
docker run -it DOCKER_USENAME/REPO_NAME:latest /bin/bash
Check Docker Logs
docker logs CONTAINER_ID