Skip to content

Commit 321d309

Browse files
authored
[DEPLOY] dev환경 docker를 이용한 무중단 배포 방식으로 변경 (#495)
2 parents 6eb179a + 81d3789 commit 321d309

File tree

15 files changed

+312
-154
lines changed

15 files changed

+312
-154
lines changed
File renamed without changes.

.github/workflows/app-cd-dev.yml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: ⚙️ MAKERS-DEV-APP-DEPLOY
2+
3+
on:
4+
push:
5+
branches: [ dev ]
6+
7+
env:
8+
SPRING_PROFILES_ACTIVE: dev
9+
ECR_APP_NAME: ${{ secrets.ECR_APP_NAME }}-dev
10+
APP_ECR_REPO: ${{ secrets.ECR_HOST }}/${{ secrets.ECR_APP_NAME }}-dev
11+
ECR_HOST: ${{ secrets.ECR_HOST }}
12+
13+
jobs:
14+
build:
15+
name: CD Pipeline
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: checkout
20+
uses: actions/checkout@v3
21+
22+
- name: set up JDK 21
23+
uses: actions/setup-java@v3
24+
with:
25+
distribution: 'corretto'
26+
java-version: '21'
27+
28+
- name: touch yml files
29+
run: |
30+
touch ./src/main/resources/application-dev.yml
31+
touch ./src/test/resources/application-test.yml
32+
33+
shell: bash
34+
35+
- name: copy application.yml files
36+
run: |
37+
echo "${{ secrets.APPLICATION_DEV_YML }}" > ./src/main/resources/application-dev.yml
38+
echo "${{ secrets.APPLICATION_TEST_YML }}" > ./src/test/resources/application-test.yml
39+
40+
- name: Configure AWS credentials
41+
uses: aws-actions/configure-aws-credentials@v2
42+
with:
43+
aws-access-key-id: ${{ secrets.DEV_AWS_ACCESS_ID }}
44+
aws-secret-access-key: ${{ secrets.DEV_AWS_SECRET_KEY }}
45+
aws-region: ap-northeast-2
46+
47+
- name: 🐘Gradle Build
48+
run: |
49+
chmod +x ./gradlew
50+
./gradlew clean build -x test
51+
52+
- name: Login to ECR
53+
run: |
54+
aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin $ECR_HOST
55+
56+
- name: 🐳Docker Image Build & Push
57+
run: |
58+
docker build \
59+
--build-arg SPRING_PROFILES_ACTIVE=$SPRING_PROFILES_ACTIVE \
60+
-t $ECR_APP_NAME .
61+
docker tag $ECR_APP_NAME:latest $APP_ECR_REPO
62+
docker push $APP_ECR_REPO
63+
64+
- name: Copy docker-compose.yml
65+
run: echo "${{ secrets.DOCKER_COMPOSE_DEV_YML }}" > ./docker-compose.yml
66+
67+
- name: Send docker-compose.yml to EC2 Instance
68+
uses: appleboy/scp-action@master
69+
with:
70+
username: ec2-user
71+
host: ${{ secrets.DEV_SERVER_IP }}
72+
key: ${{ secrets.DEV_PEM_KEY }}
73+
source: "./docker-compose.yml"
74+
target: "/home/ec2-user/app/"
75+
76+
- name: Send deploy script to EC2 Instance
77+
uses: appleboy/scp-action@master
78+
with:
79+
username: ec2-user
80+
host: ${{ secrets.DEV_SERVER_IP }}
81+
key: ${{ secrets.DEV_PEM_KEY }}
82+
source: "./scripts/"
83+
target: "/home/ec2-user/app/"
84+
85+
- name: Docker Container Run
86+
uses: appleboy/ssh-action@master
87+
with:
88+
username: ec2-user
89+
host: ${{ secrets.DEV_SERVER_IP }}
90+
key: ${{ secrets.DEV_PEM_KEY }}
91+
script: |
92+
cd ~
93+
sudo chmod +x ./app/scripts/*.sh
94+
./app/scripts/deploy.sh
95+
docker image prune -f

.github/workflows/app-ci.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: 🏭 Makers Dev App CI
2+
3+
on:
4+
pull_request:
5+
branches: [ "dev" ]
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: checkout
13+
uses: actions/checkout@v3
14+
15+
- name: Set up JDK 21
16+
uses: actions/setup-java@v3
17+
with:
18+
java-version: '21'
19+
distribution: 'corretto'
20+
21+
- name: Copy yml
22+
run: |
23+
touch ./src/main/resources/application-dev.yml
24+
touch ./src/test/resources/application-test.yml
25+
26+
echo "${{ secrets.APPLICATION_DEV_YML }}" >> ./src/main/resources/application-dev.yml
27+
echo "${{ secrets.APPLICATION_TEST_YML }}" >> ./src/test/resources/application-test.yml
28+
29+
shell: bash
30+
31+
- name: Grant execute permission for gradlew
32+
run: chmod +x gradlew
33+
34+
- name: Build with Gradle
35+
run: ./gradlew build

.github/workflows/build-dev.yml

Lines changed: 0 additions & 56 deletions
This file was deleted.

.github/workflows/release-dev-code_deploy.yml

Lines changed: 0 additions & 68 deletions
This file was deleted.

.github/workflows/release-prod.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ jobs:
4848
- name: 📂 Make zip file
4949
run: zip -r ./$GITHUB_SHA.zip .
5050

51-
- name: 🪣 S3 upload
52-
uses: aws-actions/configure-aws-credentials@v1
51+
- name: Configure AWS credentials
52+
uses: aws-actions/configure-aws-credentials@v2
5353
with:
5454
aws-access-key-id: ${{ secrets.AWS_ACCESS_ID }}
5555
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }}

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,17 @@ out/
3939
### macOS ###
4040
.DS_Store
4141

42-
### application.yml 파일들에 대한 gitignore 처리
42+
### yml 파일 관련
4343
application-local.yml
4444
application-prod.yml
4545
application-dev.yml
4646
application-test.yml
47+
docker-compose.yml
4748

4849
### HTTP 관련
4950
http-client.private.env.json
5051
http-client.env.json
5152
**/resources/http/**/env/
5253
**/resources/http/**/data/
5354
**/resources/http
54-
*.http
55+
*.http

Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM amazoncorretto:21
2+
WORKDIR /app
3+
COPY ./build/libs/app-server-0.0.1-SNAPSHOT.jar /app/app-application.jar
4+
5+
ARG SPRING_PROFILES_ACTIVE
6+
ENV SPRING_PROFILES_ACTIVE=$SPRING_PROFILES_ACTIVE
7+
8+
CMD ["java", "-jar", "-Dspring.profiles.active=${SPRING_PROFILES_ACTIVE}", "app-application.jar"]

script/deploy.sh

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/bin/bash
2+
cd /home/ec2-user/app
3+
4+
source /home/ec2-user/app/scripts/health_check.sh
5+
source /home/ec2-user/app/scripts/deploy_container.sh
6+
source /home/ec2-user/app/scripts/nginx_reload.sh
7+
source /home/ec2-user/app/scripts/stop_container.sh
8+
9+
BLUE_CONTAINER_NAME="app-blue"
10+
BLUE_PORT=9090
11+
GREEN_CONTAINER_NAME="app-green"
12+
GREEN_PORT=9091
13+
14+
DOCKER_PS_OUTPUT=$(docker ps --format "{{.Names}}" | grep -E "(${BLUE_CONTAINER_NAME}|${GREEN_CONTAINER_NAME})")
15+
RUNNING_CONTAINER_NAME="${DOCKER_PS_OUTPUT:-}"
16+
17+
ALL_PORTS=("${BLUE_PORT}" "${GREEN_PORT}")
18+
AVAILABLE_PORT=()
19+
RUNNING_SERVER_PORT=""
20+
21+
check_running_container() {
22+
if [[ "$RUNNING_CONTAINER_NAME" == "$BLUE_CONTAINER_NAME" ]]; then
23+
echo "Running Port: $BLUE_CONTAINER_NAME (:$BLUE_PORT)"
24+
RUNNING_SERVER_PORT=$BLUE_PORT
25+
elif [[ "$RUNNING_CONTAINER_NAME" == "$GREEN_CONTAINER_NAME" ]]; then
26+
echo "Running Port: $GREEN_CONTAINER_NAME (:$GREEN_PORT)"
27+
RUNNING_SERVER_PORT=$GREEN_PORT
28+
else
29+
echo "Running Port: None"
30+
fi
31+
}
32+
33+
check_available_ports() {
34+
for item in "${ALL_PORTS[@]}"; do
35+
if [ "$item" != "$RUNNING_SERVER_PORT" ]; then
36+
AVAILABLE_PORT+=("$item")
37+
fi
38+
done
39+
40+
if [[ ${#AVAILABLE_PORT[@]} -eq 0 ]]; then
41+
echo "❌ No available ports."
42+
exit 1
43+
fi
44+
}
45+
46+
### --
47+
48+
check_running_container
49+
check_available_ports
50+
51+
DEPLOY_TARGET_CONTAINER_NAME=""
52+
DEPLOY_TARGET_PORT=""
53+
54+
if [ "$RUNNING_SERVER_PORT" == "$BLUE_PORT" ]; then # Green Up
55+
DEPLOY_TARGET_CONTAINER_NAME=${GREEN_CONTAINER_NAME}
56+
DEPLOY_TARGET_PORT=${GREEN_PORT}
57+
elif [ "$RUNNING_SERVER_PORT" == "$GREEN_PORT" ]; then # Blue Up
58+
DEPLOY_TARGET_CONTAINER_NAME=${BLUE_CONTAINER_NAME}
59+
DEPLOY_TARGET_PORT=${BLUE_PORT}
60+
else
61+
echo "❌ No running container found. Defaulting to $BLUE_CONTAINER_NAME."
62+
DEPLOY_TARGET_CONTAINER_NAME=${BLUE_CONTAINER_NAME}
63+
DEPLOY_TARGET_PORT=${BLUE_PORT}
64+
fi
65+
66+
deploy_container "${DEPLOY_TARGET_CONTAINER_NAME}" ${DEPLOY_TARGET_PORT}
67+
68+
if ! health_check ${DEPLOY_TARGET_PORT}; then
69+
echo "❌ Health Check failed ..."
70+
stop_container "${DEPLOY_TARGET_CONTAINER_NAME}"
71+
exit 1
72+
fi
73+
74+
reload_nginx ${DEPLOY_TARGET_PORT}
75+
76+
if [[ -n "$RUNNING_CONTAINER_NAME" ]]; then
77+
stop_container ${RUNNING_CONTAINER_NAME}
78+
fi
79+
80+
echo "▶️ Final health check applied nginx port switching ..."
81+
if ! health_check ${DEPLOY_TARGET_PORT}; then
82+
echo "❌ Server change failed ..."
83+
stop_container "${DEPLOY_TARGET_CONTAINER_NAME}"
84+
exit 1
85+
fi
86+
87+
echo "✅ Server change successful 👍"

0 commit comments

Comments
 (0)