-
Notifications
You must be signed in to change notification settings - Fork 0
136 lines (120 loc) · 3.82 KB
/
deploy.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
135
136
name: BlackFriday CI/CD
on:
push:
branches:
- 'feature/**'
- 'main'
paths-ignore:
- '**.md'
- 'docs/**'
- '.gitignore'
pull_request:
branches: [ main ]
paths-ignore:
- '**.md'
- 'docs/**'
- '.gitignore'
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
cache: 'gradle'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Test with Gradle
run: ./gradlew test
build-and-push:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
cache: 'gradle'
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_TOKEN }}
- name: Build and Push with Jib
run: |
./gradlew jib \
-Djib.to.image=docker.io/${{ secrets.DOCKER_HUB_USERNAME }}/blackfriday-app \
-Djib.to.tags=latest,${{ github.sha }}
deploy-blue:
needs: build-and-push
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Copy docker-compose files
uses: appleboy/scp-action@master
with:
host: ${{ secrets.NCP_BLUE_HOST }}
username: root
key: ${{ secrets.NCP_SSH_PRIVATE_KEY }}
source: "docker-compose.yml"
target: "/app"
- name: Deploy to Blue Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.NCP_BLUE_HOST }}
username: root
key: ${{ secrets.NCP_SSH_PRIVATE_KEY }}
script: |
cd /app
docker compose down
docker pull docker.io/${{ secrets.DOCKER_HUB_USERNAME }}/blackfriday-app:${{ github.sha }}
DOCKER_IMAGE=${{ secrets.DOCKER_HUB_USERNAME }}/blackfriday-app:${{ github.sha }} \
MYSQL_DATABASE=${{ secrets.MYSQL_DATABASE }} \
MYSQL_USER=${{ secrets.MYSQL_USER }} \
MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD }} \
docker compose up -d
- name: Health Check Blue
run: |
for i in {1..10}; do
if curl -s -f http://${{ secrets.NCP_BLUE_HOST }}:8080/actuator/health; then
echo "Health check passed"
exit 0
fi
echo "Attempt $i failed. Retrying..."
sleep 30
done
echo "Health check failed after 10 attempts"
exit 1
deploy-green:
needs: deploy-blue
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Copy docker-compose files
uses: appleboy/scp-action@master
with:
username: root
key: ${{ secrets.NCP_SSH_PRIVATE_KEY }}
source: "docker-compose.yml"
target: "/app"
- name: Deploy to Green Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.NCP_GREEN_HOST }}
username: root
key: ${{ secrets.NCP_SSH_PRIVATE_KEY }}
script: |
cd /app
docker compose down
docker pull docker.io/${{ secrets.DOCKER_HUB_USERNAME }}/blackfriday-app:${{ github.sha }}
DOCKER_IMAGE=${{ secrets.DOCKER_HUB_USERNAME }}/blackfriday-app:${{ github.sha }} \
MYSQL_DATABASE=${{ secrets.MYSQL_DATABASE }} \
MYSQL_USER=${{ secrets.MYSQL_USER }} \
MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD }} \
docker compose up -d