Skip to content

Commit 8535e80

Browse files
committed
updated ci cd pipeline for ec2
1 parent 9bf298f commit 8535e80

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

.github/workflows/build-ci-cd.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: CI/CD Docker Deployment to AWS EC2
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
workflow_dispatch:
7+
8+
jobs:
9+
# Job 1: Build and push the Docker images to Docker Hub
10+
publish_docker_images:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
16+
- name: Log in to Docker Hub
17+
uses: docker/login-action@v3
18+
with:
19+
username: temadeveloper
20+
password: ${{ secrets.DOCKER_HUB_TOKEN }}
21+
22+
- name: Build and push backend image
23+
uses: docker/build-push-action@v5
24+
with:
25+
context: ./backend/library/
26+
push: true
27+
tags: temadeveloper/library_proj:backend-latest
28+
29+
- name: Build and push frontend image
30+
uses: docker/build-push-action@v5
31+
with:
32+
context: ./frontend/
33+
push: true
34+
tags: temadeveloper/library_proj:frontend-latest
35+
36+
# Job 2: Deploy the Docker images to your EC2 instance
37+
deploy_to_ec2:
38+
runs-on: ubuntu-latest
39+
needs: publish_docker_images # This job depends on the successful completion of the build job
40+
steps:
41+
- name: Deploy to Server via SSH
42+
uses: appleboy/ssh-action@v1.0.3
43+
with:
44+
host: ${{ secrets.SSH_HOST }}
45+
username: ${{ secrets.SSH_USERNAME }}
46+
key: ${{ secrets.SSH_PRIVATE_KEY }}
47+
script: |
48+
# Pull the latest backend and frontend images
49+
docker pull temadeveloper/library_proj:backend-latest
50+
docker pull temadeveloper/library_proj:frontend-latest
51+
52+
# Stop and remove old containers
53+
docker stop library-backend-container || true
54+
docker rm library-backend-container || true
55+
docker stop library-frontend-container || true
56+
docker rm library-frontend-container || true
57+
58+
# Run the new containers
59+
docker run -d --name library-backend-container -p 3001:3001 temadeveloper/library_proj:backend-latest
60+
docker run -d --name library-frontend-container -p 80:3000 temadeveloper/library_proj:frontend-latest

0 commit comments

Comments
 (0)