-
Notifications
You must be signed in to change notification settings - Fork 3
153 lines (128 loc) · 5.1 KB
/
be_cd-test.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
name: Coduo Backend Test Server CD
on:
push:
branches: [ "BE/test" ]
jobs:
build:
environment: test
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./backend
steps:
- name: checkout
uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'corretto'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
with:
cache-write-only: true
- name: Grant Execute Permission For Gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew bootJar
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_HUB_ID }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
- name: Image build and push
run: |
docker build --build-arg PROFILE=test --build-arg DEPENDENCY=build/dependency -t ${{ secrets.DOCKER_REPO_NAME }}/springboot-app:test-latest --platform linux/amd64 .
docker push ${{ secrets.DOCKER_REPO_NAME }}/springboot-app:test-latest
- name: Upload docker-compose yaml script to artifact
uses: actions/upload-artifact@v4
with:
name: docker-compose
path: ${{ github.workspace }}/backend/be_app-docker-compose.yml
deploy:
environment: test
runs-on: ${{ matrix.instance }}
needs: build
strategy:
matrix:
instance: [ "test-ec2-runner-a", "test-ec2-runner-b" ]
defaults:
run:
working-directory: ./backend
steps:
- name: Set docker-compose YAML script to runner
uses: actions/download-artifact@v4
with:
name: docker-compose
path: ${{ github.workspace }}/backend
- name: Move docker-compose YAML
run: |
sudo mv be_app-docker-compose.yml ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/
- name: Run deploy script
run: |
chmod +x ./deploy.sh
source ./deploy.sh
echo "BLUE_PORT=$BLUE_PORT" >> $GITHUB_ENV
echo "GREEN_PORT=$GREEN_PORT" >> $GITHUB_ENV
- name: Extract secrets as .be_app-env file
run: |
if [ "${{ matrix.instance }}" = "test-ec2-runner-a" ]; then
INSTANCE_NAME=${{ secrets.INSTANCE_A_NAME }}
elif [ "${{ matrix.instance }}" = "test-ec2-runner-b" ]; then
INSTANCE_NAME=${{ secrets.INSTANCE_B_NAME }}
else
echo "Unknown instance: ${{ matrix.instance }}"
exit 1
fi
cat <<EOF > ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/.be_app-env
# Docker Hub info from Github Secrets
DOCKER_REPO_NAME=${{ secrets.DOCKER_REPO_NAME }}
DOCKER_IMAGE_VERSION=test-latest
# DB Configuration secrets info from Github Secrets
MYSQL_TIME_ZONE=${{ secrets.MYSQL_TIME_ZONE }}
DB_BINDING_PORT=${{ secrets.DB_BINDING_PORT }}
MASTER_DB_URL=${{ secrets.TEST_SERVER_MASTER_DB_URL }}
MASTER_DB_USERNAME=${{ secrets.TEST_SERVER_DB_USERNAME }}
MASTER_DB_PASSWORD=${{ secrets.TEST_SERVER_DB_PASSWORD }}
SLAVE_DB_URL=${{ secrets.TEST_SERVER_SLAVE_DB_URL }}
SLAVE_DB_USERNAME=${{ secrets.TEST_SERVER_DB_USERNAME }}
SLAVE_DB_PASSWORD=${{ secrets.TEST_SERVER_DB_PASSWORD }}
DDL_AUTO=${{ secrets.TEST_DDL_AUTO }}
# OAUTH & JWT
CLIENT_ID=${{ secrets.TEST_CLIENT_ID }}
CLIENT_SECRET=${{ secrets.TEST_CLIENT_SECRET }}
CLIENT_REDIRECT_URI=${{ secrets.TEST_CLIENT_REDIRECT_URI }}
JWT_KEY=${{ secrets.JWT_KEY}}
# INSTANCE NAME
INSTANCE_NAME=$INSTANCE_NAME
# Server App
BLUE_SERVER_BINDING_PORT=${BLUE_PORT}
GREEN_SERVER_BINDING_PORT=${GREEN_PORT}
SERVER_LOGS_PATH=${{ secrets.SERVER_LOGS_PATH }}
EOF
- name: remove old spring-app image
run: |
sudo docker image rm -f ${{ secrets.DOCKER_REPO_NAME }}/springboot-app:test-latest;
- name: Start the new (green/blue) environment
run: |
sudo docker-compose --env-file ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/.be_app-env -f ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/be_app-docker-compose.yml -p $GREEN_PORT up -d
- name: Wait for new environment to be healthy
run: |
timeout=30
while ! curl -sf http://localhost:${GREEN_PORT}/api/actuator/health | grep "UP"; do
((timeout--))
if [ $timeout -le 0 ]; then
echo "Health check timed out" >&2
exit 1
fi
sleep 5
done
- name: Reload NGINX
run: |
sudo systemctl reload nginx
- name: Stop and remove the old environment
run: |
sudo docker-compose --env-file ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/.be_app-env -f ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/be_app-docker-compose.yml -p $BLUE_PORT down
- name: Delete Dangling docker images
run: |
sudo docker image prune -a -f