Skip to content

Commit bf06895

Browse files
committed
added code samples for 04 - cicd deployment
1 parent 012ef36 commit bf06895

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

ci-cd/04-deployment/Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Start with an official Python runtime as the base image
2+
FROM python:3.10-slim
3+
4+
# Set the working directory in the container
5+
WORKDIR /app
6+
7+
# Copy the current directory contents into the container
8+
COPY . /app
9+
10+
# Install any dependencies
11+
RUN pip install -r requirements.txt
12+
13+
# Run the application
14+
CMD ["python", "-m", "unittest", "discover"]

ci-cd/04-deployment/app.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def hello():
2+
return "Hello, World!"
3+
4+
if __name__ == "__main__":
5+
print(hello())

ci-cd/04-deployment/ci.yaml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: CI/CD - 04 - GitHub Actions Deployment
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build-and-deploy:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
# Step 1: Checkout code
14+
- name: Checkout code
15+
uses: actions/checkout@v3
16+
17+
# Step 2: Build and tag Docker image
18+
- name: Build and tag Docker image
19+
run: |
20+
docker build -t my-app:${{ github.sha }} .
21+
docker tag my-app:${{ github.sha }} \
22+
username/my-app:${{ github.sha }}
23+
24+
# Step 3: Log in to Docker Hub
25+
- name: Log in to Docker Hub
26+
run: |
27+
echo "${{ secrets.DOCKER_PASSWORD }}" | \
28+
docker login -u "${{ secrets.DOCKER_USERNAME }}" \
29+
--password-stdin
30+
31+
# Step 4: Push Docker image to Docker Hub
32+
- name: Push Docker image
33+
run: |
34+
docker push username/my-app:${{ github.sha }}
35+
36+
# Step 5: Pull Docker image on the server
37+
- name: Pull Docker image
38+
run: |
39+
echo "${{ secrets.SSH_KEY }}" > ssh_key
40+
chmod 600 ssh_key
41+
ssh -i ssh_key \
42+
-o StrictHostKeyChecking=no \
43+
user@your-server \
44+
"docker pull username/my-app:${{ github.sha }}"
45+
46+
# Step 6: Stop and remove the existing container
47+
- name: Stop and remove existing container
48+
run: |
49+
ssh -i ssh_key \
50+
-o StrictHostKeyChecking=no \
51+
user@your-server \
52+
"docker stop my-app || true"
53+
ssh -i ssh_key \
54+
-o StrictHostKeyChecking=no \
55+
user@your-server \
56+
"docker rm my-app || true"
57+
58+
# Step 7: Run the new container
59+
- name: Run the new container
60+
run: |
61+
ssh -i ssh_key \
62+
-o StrictHostKeyChecking=no \
63+
user@your-server \
64+
"docker run -d \
65+
--name my-app \
66+
-p 80:80 \
67+
username/my-app:${{ github.sha }}"

ci-cd/04-deployment/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
flask
2+
numpy

ci-cd/04-deployment/test_app.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import unittest
2+
from app import hello
3+
4+
class TestApp(unittest.TestCase):
5+
def test_hello(self):
6+
self.assertEqual(hello(), "Hello, World!")
7+
8+
if __name__ == "__main__":
9+
unittest.main()

0 commit comments

Comments
 (0)