-
Notifications
You must be signed in to change notification settings - Fork 0
134 lines (120 loc) · 4.34 KB
/
stagging-cicd.yml
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
name: Staging CI/CD Pipeline
on:
push:
branches:
- stagging
env:
IMAGE_NAME: daoudhussaindev/next-js-app
NODE_VERSION: 18.x
AWS_REGION: ap-south-1
CONTAINER_NAME: next-js-app-staging
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Build Next.js application
run: npm run build
- name: Cache build output
uses: actions/cache@v3
with:
path: |
.next
node_modules
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
restore-keys: |
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-
test:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Restore cache
uses: actions/cache@v3
with:
path: |
.next
node_modules
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm run test
deploy:
needs: [build, test]
runs-on: ubuntu-latest
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Set up SSH
run: |
mkdir -p ~/.ssh
echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts
echo "Testing SSH connection..."
ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} 'echo "SSH connection successful"'
- name: Deploy to EC2
run: |
echo "Starting deployment..."
ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} "\
set -e
echo 'Connected to EC2 instance'
echo 'Checking Docker status...'
docker ps
echo 'Stopping existing container...'
if docker ps -a --filter name=${{ env.CONTAINER_NAME }} | grep -q ${{ env.CONTAINER_NAME }}; then
echo 'Stopping and removing existing container...'
docker stop ${{ env.CONTAINER_NAME }} || true
docker rm ${{ env.CONTAINER_NAME }} || true
fi
echo 'Pulling new image...'
docker pull ${{ env.IMAGE_NAME }}:dev
echo 'Starting new container...'
docker run -d \
--name ${{ env.CONTAINER_NAME }} \
-p 3000:3000 \
--restart unless-stopped \
${{ env.IMAGE_NAME }}:dev
echo 'Cleaning up old images...'
docker image prune -f
echo 'Verifying deployment...'
if docker ps | grep -q ${{ env.CONTAINER_NAME }}; then
echo 'Container is running successfully'
else
echo 'Container failed to start'
exit 1
fi
echo 'Deployment completed successfully'
"
- name: Verify Deployment
run: |
echo "Waiting for application to start..."
sleep 15
ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} "\
if curl -s http://localhost:3000 > /dev/null; then
echo 'Application is responding'
exit 0
else
echo 'Application is not responding'
exit 1
fi
"