Skip to content

Commit 638f8fe

Browse files
committed
setup cicd pipeline
1 parent 651ce07 commit 638f8fe

1 file changed

Lines changed: 67 additions & 5 deletions

File tree

.github/workflows/ci-cd.yml

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,52 @@
1-
name: CI/CD Pipeline
1+
name: CI/CD Application Deployment
22

33
on:
44
push:
5-
branches: [ "main" ]
5+
branches: ["main"]
66

77
jobs:
8-
docker:
8+
9+
# -------------------
10+
# CI JOB
11+
# -------------------
12+
ci:
13+
name: Continuous Integration
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Set up Node.js
21+
uses: actions/setup-node@v4
22+
with:
23+
node-version: 20
24+
25+
- name: Install dependencies
26+
run: npm install
27+
28+
- name: Run tests
29+
run: |
30+
if npm run | grep -q test; then
31+
npm test
32+
else
33+
echo "No tests found, skipping..."
34+
fi
35+
36+
- name: Build app
37+
run: npm run build
38+
39+
# -------------------
40+
# CD JOB
41+
# -------------------
42+
cd:
43+
name: Docker Build, Push & Deploy to EC2
944
runs-on: ubuntu-latest
45+
needs: ci
1046

1147
steps:
12-
- uses: actions/checkout@v4
48+
- name: Checkout code
49+
uses: actions/checkout@v4
1350

1451
- name: Login to Docker Hub
1552
uses: docker/login-action@v3
@@ -21,4 +58,29 @@ jobs:
2158
uses: docker/build-push-action@v5
2259
with:
2360
push: true
24-
tags: ${{ secrets.DOCKERHUB_USERNAME }}/alienui-react
61+
tags: |
62+
${{ secrets.DOCKERHUB_USERNAME }}/alienui-react:0.1
63+
cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/alienui-react:0.1
64+
cache-to: type=inline
65+
66+
- name: Setup SSH Key
67+
run: |
68+
echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > key.pem
69+
chmod 600 key.pem
70+
71+
- name: Deploy to EC2
72+
run: |
73+
ssh -o StrictHostKeyChecking=no -i key.pem ubuntu@${{ secrets.EC2_HOST }} << "EOF"
74+
75+
sudo docker stop alienui-react || true
76+
sudo docker rm alienui-react || true
77+
78+
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/alienui-react:0.1
79+
80+
sudo docker run -d \
81+
--name alienui \
82+
-p 80:3000 \
83+
--restart unless-stopped \
84+
${{ secrets.DOCKERHUB_USERNAME }}/alienui-react:0.1
85+
86+
EOF

0 commit comments

Comments
 (0)