Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
143 commits
Select commit Hold shift + click to select a range
fda1620
[refactor/#351] Dockerfile에 멀티 스테이지 빌드 적용
yongjun0511 Apr 26, 2025
03eac9c
[refactor/#351] buildx를 이용해 캐시를 이용하도록 수정
yongjun0511 Apr 26, 2025
a9b84f8
[refactor/#351] buildx driver 수정
yongjun0511 Apr 26, 2025
c3c6734
[refactor/#351] buildx driver 수정
yongjun0511 Apr 26, 2025
46c65b4
[refactor/#351] buildx default 드라이버 해제
yongjun0511 Apr 26, 2025
b48b102
[refactor/#351] dependency 차이를 판단해서 분기
yongjun0511 Apr 26, 2025
bd4eff7
[refactor/#351] .dockerignore 작성
yongjun0511 Apr 26, 2025
46b5a78
[refactor/#351] application-secret yml적용 방식 변경
yongjun0511 Apr 27, 2025
c634f3b
[refactor/#351] application-secret yml적용 방식 변경
yongjun0511 Apr 27, 2025
2d13b4e
[refactor/#351] gradle 캐시 적용
yongjun0511 Apr 27, 2025
8f68bd8
[refactor/#351] gradle 캐시 적용 테스트
yongjun0511 Apr 27, 2025
fcd15f3
[refactor/#351] gradle 캐시 save 과정 추가
yongjun0511 Apr 27, 2025
ca5b7e4
[refactor/#351] gradle build 캐시 추가
yongjun0511 Apr 27, 2025
aca35ee
[refactor/#351] gradle build 캐시 테스트
yongjun0511 Apr 27, 2025
f9b5d79
[refactor/#351] gradle cache 적용
yongjun0511 Apr 27, 2025
cf82aed
[refactor/#351] src 경로 파일 수정 테스트
yongjun0511 Apr 27, 2025
ad1ca6d
[refactor/#351] src 경로 파일 수정 테스트
yongjun0511 Apr 27, 2025
9bf8037
[refactor/#351] github action cache mount
yongjun0511 Apr 27, 2025
c3e714b
[refactor/#351] Dockerfile 수정
yongjun0511 Apr 27, 2025
c037b73
[refactor/#351] Dockerfile 수정
yongjun0511 Apr 27, 2025
996e834
[refactor/#351] Dockerfile 수정
yongjun0511 Apr 27, 2025
2694c8a
[refactor/#351] Dockerfile 수정
yongjun0511 Apr 27, 2025
2353331
[refactor/#351] Dockerfile 수정
yongjun0511 Apr 27, 2025
143ab17
[refactor/#351] Dockerfile 수정..
yongjun0511 Apr 27, 2025
d8dd1c0
[refactor/#351] 로컬 캐시 제외
yongjun0511 Apr 27, 2025
f5eeca8
[refactor/#351] 로컬 캐시 제외
yongjun0511 Apr 27, 2025
42cd1a3
[refactor/#351] 인라인 캐시로 변경
yongjun0511 Apr 27, 2025
8aec3d7
[refactor/#351] 도커 로그인 과정 추가
yongjun0511 Apr 27, 2025
81c1756
[refactor/#351] 도커 로그인 과정 추가
yongjun0511 Apr 27, 2025
d02f8c9
[refactor/#351] mount 추가
yongjun0511 Apr 27, 2025
aa5c015
[refactor/#351] src 디렉토리 변경 테스트
yongjun0511 Apr 27, 2025
3a228a5
[refactor/#351] CI/CD test
yongjun0511 Apr 27, 2025
49c3c25
[refactor/#351] configuration cache 적용 test
yongjun0511 Apr 27, 2025
fcd4c7f
[refactor/#351] src 변경 테스트
yongjun0511 Apr 27, 2025
d246e9d
[refactor/#351] src 변경 테스트
yongjun0511 Apr 27, 2025
7ece0f8
[refactor/#351] src 변경 테스트
yongjun0511 Apr 27, 2025
acce225
[refactor/#351] src 변경 테스트
yongjun0511 Apr 27, 2025
5ae566a
[refactor/#351] src 변경 테스트
yongjun0511 Apr 27, 2025
0c222fc
[refactor/#351] CI/CD 변경
yongjun0511 Apr 27, 2025
86aa23d
[refactor/#351] CI/CD 변경
yongjun0511 Apr 27, 2025
d5577aa
[refactor/#351] CI/CD 변경
yongjun0511 Apr 27, 2025
3b8beab
[refactor/#351] CI/CD 변경
yongjun0511 Apr 27, 2025
c945e88
[refactor/#351] CI/CD 변경
yongjun0511 Apr 27, 2025
98af1b7
[refactor/#351] CI/CD 변경
yongjun0511 Apr 27, 2025
f0ef85d
[refactor/#351] CI/CD 변경
yongjun0511 Apr 27, 2025
c567ae4
[refactor/#351] CI/CD 변경
yongjun0511 Apr 27, 2025
43c9eef
[refactor/#351] CI/CD 변경
yongjun0511 Apr 27, 2025
d7dcbde
[refactor/#351] Dockerfile test
yongjun0511 Apr 27, 2025
eaf235a
[refactor/#351] src 변경 test
yongjun0511 Apr 27, 2025
5372745
[refactor/#351] gradle 캐싱 적용
yongjun0511 Apr 27, 2025
1d3c1ab
[refactor/#351] 최종 적용 완료
yongjun0511 Apr 27, 2025
2538693
[refactor/#351] Docker 파일 스테이지별로 분리
yongjun0511 Apr 27, 2025
1484a81
[refactor/#351] external 이미지 pull
yongjun0511 Apr 27, 2025
34478f1
[refactor/#351] 최종 수정 완료
yongjun0511 Apr 27, 2025
6392d3b
[refactor/#351] 최종 수정 완료
yongjun0511 Apr 27, 2025
7163793
[refactor/#351] src 변경 test
yongjun0511 Apr 27, 2025
a1dd451
[refactor/#351] Check Dependency 조건문 수정
yongjun0511 Apr 27, 2025
bac85ec
[refactor/#351] Check Dependency 로그 코드 추가
yongjun0511 Apr 27, 2025
5647ce9
[refactor/#351] Check Dependency 로그 코드 추가
yongjun0511 Apr 27, 2025
7948cba
[refactor/#351] build 부분 조건문 추가
yongjun0511 Apr 27, 2025
635feb7
[refactor/#351] src 변경 테스트
yongjun0511 Apr 27, 2025
b2040ca
[refactor/#351] src 변경 테스트
yongjun0511 Apr 28, 2025
3a06b00
[refactor/#351] gradle 캐시 관련 디버깅 옵션 추가
yongjun0511 Apr 28, 2025
da8b72b
[refactor/#351] src 변경 테스트
yongjun0511 Apr 28, 2025
53f7675
[refactor/#351] Dockerfile 로그 찍어보기
yongjun0511 Apr 28, 2025
4ee506b
[refactor/#351] 캐시 빌드시 secret 추가
yongjun0511 Apr 28, 2025
f3974b2
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
6f65f5e
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
236d3c2
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
030aad2
[refactor/#351] 문법 오류 해결
yongjun0511 Apr 28, 2025
262b767
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
9d4f32a
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
202b05c
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
e72bdac
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
a1fdb40
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
ea31a99
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
a5f780b
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
a503eec
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
65796b3
[refactor/#351] 캐싱 on
yongjun0511 Apr 28, 2025
bc5bb6f
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
43a8993
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
ca30718
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
9375ff2
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
b16cfd9
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
2baad0f
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
1484137
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
bcaf61b
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
c25a03e
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
76dbf04
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
5059888
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
4c2a0aa
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
4ad8128
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
b633cf2
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
dfa2894
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
7cc456d
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
96abe56
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
678781a
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
438e524
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
9c248ab
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
a6d970b
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
bc1f19c
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
95e9de5
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
ad5c343
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
c2fd9ab
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
a807fe0
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
6be7545
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
5ff8027
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
1975105
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
280d4e4
[refactor/#351] 디버깅
yongjun0511 Apr 28, 2025
3b5695c
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
12915ca
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
7ed53da
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
9581f60
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
7b6552a
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
a74e8fb
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
c82bb2f
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
a55ea70
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
b1befc2
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
bcda7b6
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
094c125
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
e7b5526
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
3115904
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
876c6c8
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
5e3e24d
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
65bf175
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
5ae4ef7
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
51da89e
[refactor/#351] 디버깅
yongjun0511 Apr 29, 2025
4000d6c
[refactor/#351] src 변경 테스트
yongjun0511 Apr 29, 2025
4b1c028
[refactor/#351] src 변경 테스트
yongjun0511 Apr 29, 2025
1c7190e
[refactor/#351] src 변경 테스트
yongjun0511 Apr 29, 2025
cbf9f4a
[refactor/#351] src 변경 테스트
yongjun0511 Apr 29, 2025
72ca7e5
[refactor/#351] src 변경 테스트
yongjun0511 Apr 29, 2025
b758718
[refactor/#351] src 변경 테스트
yongjun0511 Apr 29, 2025
6551497
[refactor/#351] src 변경 테스트
yongjun0511 Apr 29, 2025
7a48722
[refactor/#351] src 변경 테스트
yongjun0511 Apr 29, 2025
42e573f
[refactor/#351] src 변경 테스트
yongjun0511 Apr 29, 2025
c14807c
[refactor/#351] src 변경 테스트
yongjun0511 Apr 29, 2025
282b3bb
[refactor/#351] src 변경 테스트
yongjun0511 Apr 29, 2025
a72c7cb
[refactor/#351] 최종 수정
yongjun0511 Apr 29, 2025
533dc60
[refactor/#351] deploy.sh 확인
yongjun0511 Apr 29, 2025
e8d229b
[refactor/#351] deploy.sh 확인
yongjun0511 Apr 29, 2025
5734b0e
[refactor/#351] 구분해서 전송하기
yongjun0511 Apr 29, 2025
1e641c6
[refactor/#351] 변화 사항 체크하기
yongjun0511 Apr 29, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 기본적으로 무시할 것들
.git
.idea
.vscode
*.log
*.iml

# 빌드 결과물 무시
build/
target/

# 민감 파일 무시
src/main/resources/application-secret.yml

#도커 관련 파일
docker-compose.yml
Dockerfile

# 깃허브 파일
.github/
124 changes: 105 additions & 19 deletions .github/workflows/dev_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ name: CI/CD Pipeline
on:
push:
branches: [ develop ]
pull_request:
branches: [ develop ]

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
env:
GRADLE_BUILD_CACHE_URL: ${{ secrets.GRADLE_BUILD_CACHE_URL }}
GRADLE_BUILD_CACHE_USERNAME: ${{ secrets.GRADLE_BUILD_CACHE_USERNAME }}
GRADLE_BUILD_CACHE_PASSWORD: ${{ secrets.GRADLE_BUILD_CACHE_PASSWORD }}

steps:
- uses: actions/checkout@v4
Expand All @@ -22,36 +28,118 @@ jobs:
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew

- name: make application-secret.yml
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
install: true

- name: Create and use buildx builder
run: |
touch ./src/main/resources/application-secret.yml
echo "${{ secrets.APPLICATION_SECRET }}" > ./src/main/resources/application-secret.yml
shell: bash
docker buildx create --use --name mybuilder
docker buildx inspect --bootstrap

- name: Log in to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Check Dockerfile or Dependency Changes
id: check_changes
run: |
git fetch origin ${{ github.event.before }}
echo "Changed files between commits:"
changed_files=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }})
echo "$changed_files"

if echo "$changed_files" | grep -qE 'Dockerfile|build\.gradle|settings\.gradle|gradle\.properties|gradlew|gradle/wrapper/gradle-wrapper\.properties'; then
echo "changed=true" >> $GITHUB_ENV
else
echo "changed=false" >> $GITHUB_ENV
fi

- name: Build & Push Dependency Cache
if: env.changed == 'true'
run: |
docker buildx build \
--builder mybuilder \
--platform linux/amd64 \
--push \
--file Dockerfile \
--tag ${{ secrets.DOCKERHUB_USERNAME }}/clokey-docker:dependency-cache \
--cache-to type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/clokey-docker:dependency-cache,mode=max \
--secret type=env,id=GRADLE_BUILD_CACHE_URL,env=GRADLE_BUILD_CACHE_URL \
--secret type=env,id=GRADLE_BUILD_CACHE_USERNAME,env=GRADLE_BUILD_CACHE_USERNAME \
--secret type=env,id=GRADLE_BUILD_CACHE_PASSWORD,env=GRADLE_BUILD_CACHE_PASSWORD \
.

- name: Build & Push Application Image
run: |
docker buildx build \
--builder mybuilder \
--platform linux/amd64 \
--push \
--file Dockerfile \
--tag ${{ secrets.DOCKERHUB_USERNAME }}/clokey-docker:1.0.0 \
--build-arg DEPENDENCY_IMAGE=${{ secrets.DOCKERHUB_USERNAME }}/clokey-docker:dependency-cache \
--cache-from type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/clokey-docker:dependency-cache \
--secret type=env,id=GRADLE_BUILD_CACHE_URL,env=GRADLE_BUILD_CACHE_URL \
--secret type=env,id=GRADLE_BUILD_CACHE_USERNAME,env=GRADLE_BUILD_CACHE_USERNAME \
--secret type=env,id=GRADLE_BUILD_CACHE_PASSWORD,env=GRADLE_BUILD_CACHE_PASSWORD \
.

- name: Build with Gradle Wrapper
- name: Create application-secret.yml from Secret
run: |
./gradlew --stop && ./gradlew clean --refresh-dependencies
./gradlew clean build
ls -l build/libs
mkdir -p ./temp_secret
echo "${{ secrets.APPLICATION_SECRET }}" > ./temp_secret/application-secret.yml
shell: bash

- name: Copy application-secret.yml to remote
uses: appleboy/[email protected]
with:
username: ubuntu
host: ${{ secrets.EC2_HOST }}
key: ${{ secrets.EC2_SSH_KEY }}
source: ./temp_secret/application-secret.yml
target: /home/ubuntu/secret/

- name: Build Docker Image
- name: Check if docker-compose.yml changed
id: docker_compose_changed
run: |
docker build -t yongjun0511/clokey-docker:1.0.0 .
if git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep -q "docker-compose.yml"; then
echo "changed=true" >> $GITHUB_ENV
else
echo "changed=false" >> $GITHUB_ENV
fi

- name: Push Docker Image
- name: Copy docker-compose.yml to remote
if: env.changed == 'true'
uses: appleboy/[email protected]
with:
username: ubuntu
host: ${{ secrets.EC2_HOST }}
key: ${{ secrets.EC2_SSH_KEY }}
source: ./docker-compose.yml
target: /home/ubuntu/cicd/

- name: Check if deploy.sh changed
id: deploy_sh_changed
run: |
echo ${{ secrets.DOCKERHUB_TOKEN }} | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
docker push yongjun0511/clokey-docker:1.0.0
if git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep -q "deploy.sh"; then
echo "deploy_changed=true" >> $GITHUB_ENV
else
echo "deploy_changed=false" >> $GITHUB_ENV
fi

- name: Copy docker-compose file to remote
- name: Copy deploy.sh to remote
if: env.deploy_changed == 'true'
uses: appleboy/[email protected]
with:
username: ubuntu
host: ${{ secrets.EC2_HOST }}
key: ${{ secrets.EC2_SSH_KEY }}
source: "./docker-compose.yml"
target: "/home/ubuntu/cicd/"
source: ./deploy.sh
target: /home/ubuntu/cicd/

- name: Deploy Blue-Green
uses: appleboy/ssh-action@master
Expand All @@ -61,8 +149,6 @@ jobs:
key: ${{ secrets.EC2_SSH_KEY }}
script: |
echo ${{ secrets.DOCKERHUB_TOKEN }} | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
docker pull yongjun0511/clokey-docker:1.0.0
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/clokey-docker:1.0.0
sudo chmod +x /home/ubuntu/cicd/deploy.sh
sudo /home/ubuntu/cicd/deploy.sh


42 changes: 33 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
# syntax=docker/dockerfile:1.4

FROM gradle:8.5-jdk17 AS dependencies
WORKDIR /build

COPY gradlew .
COPY gradle.properties /root/.gradle/gradle.properties
COPY gradle/wrapper/gradle-wrapper.jar gradle/wrapper/
COPY gradle/wrapper/gradle-wrapper.properties gradle/wrapper/
COPY build.gradle settings.gradle ./

RUN ./gradlew dependencies --no-daemon

FROM gradle:8.5-jdk17 AS builder
WORKDIR /build

COPY --from=dependencies /build /build
COPY src src

RUN --mount=type=secret,id=GRADLE_BUILD_CACHE_URL \
--mount=type=secret,id=GRADLE_BUILD_CACHE_USERNAME \
--mount=type=secret,id=GRADLE_BUILD_CACHE_PASSWORD \
bash -euxo pipefail -c " \
echo '==== Mounted Secrets ====' && \
ls -l /run/secrets && \
export GRADLE_BUILD_CACHE_URL=\$(cat /run/secrets/GRADLE_BUILD_CACHE_URL | tr -d '\r\n') && \
export GRADLE_BUILD_CACHE_USERNAME=\$(cat /run/secrets/GRADLE_BUILD_CACHE_USERNAME | tr -d '\r\n') && \
export GRADLE_BUILD_CACHE_PASSWORD=\$(cat /run/secrets/GRADLE_BUILD_CACHE_PASSWORD | tr -d '\r\n') && \
./gradlew bootJar -x test --build-cache \
"

FROM openjdk:17-jdk-slim

# Set the timezone to Asia/Seoul
ENV TZ=Asia/Seoul
RUN ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ >/etc/timezone

# Set the working directory
WORKDIR /app

# Copy the built JAR file into the container
COPY ./build/libs/*.jar app.jar

# Expose the port your application will run on
EXPOSE 8080
COPY --from=builder /build/build/libs/*.jar app.jar

# Command to run the JAR file
ENTRYPOINT ["java", "-jar", "/app/app.jar"]
119 changes: 119 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/bin/bash

cd /home/ubuntu/cicd

APP_NAME="clokey"

# NGINX 설정 관련
NGINX_CONF_PATH="/etc/nginx"
BLUE_CONF="blue.conf"
GREEN_CONF="green.conf"
DEFAULT_CONF="nginx.conf"
MAX_RETRIES=3

# 활성화된 서비스 확인 및 스위칭 대상 결정
determine_target() {
if docker-compose -f docker-compose.yml ps | grep -q "app_blue.*Up"; then
TARGET="green"
OLD="blue"
elif docker-compose -f docker-compose.yml ps | grep -q "app_green.*Up"; then
TARGET="blue"
OLD="green"
else
TARGET="blue" # 첫 실행 시 기본값
OLD="none"
fi

echo "TARGET: $TARGET"
echo "OLD: $OLD"
}

# 헬스체크 실패 시 롤백 처리
health_check() {
local URL=$1
local RETRIES=0
local ORIGINAL_TARGET=$TARGET # 원래 TARGET 값을 저장

while [ $RETRIES -lt $MAX_RETRIES ]; do
echo "Checking service at $URL... (attempt: $((RETRIES + 1)))"
sleep 3

# 현재 실행 중인 컨테이너 확인
CONTAINER_RUNNING=$(docker ps --filter "name=app_$TARGET" --format '{{.Names}}')

if [ "$CONTAINER_RUNNING" = "app_$TARGET" ]; then
echo "$TARGET container is running."
return 0 # 컨테이너가 실행 중이라면 헬스체크 성공
else
echo "$TARGET container is not running."
fi

RETRIES=$((RETRIES + 1))
done

# 헬스체크 실패 시 롤백 처리
echo "Health check failed after $MAX_RETRIES attempts."
echo "Rolling back to the original target: $ORIGINAL_TARGET"

# TARGET을 원래 값으로 롤백
TARGET=$ORIGINAL_TARGET
echo "Rolled back TARGET: $TARGET"

# 로그 파일에 실패 기록
echo "Failed health check for $TARGET container" > /home/ubuntu/cicd/health_check_failure.log

# docker-compose down 대신 실패 기록 후 종료
exit 1
}

# NGINX 설정 스위칭 함수
switch_nginx_conf() {
if [ "$TARGET" = "blue" ]; then
sudo cp "${NGINX_CONF_PATH}/${BLUE_CONF}" "${NGINX_CONF_PATH}/${DEFAULT_CONF}"
else
sudo cp "${NGINX_CONF_PATH}/${GREEN_CONF}" "${NGINX_CONF_PATH}/${DEFAULT_CONF}"
fi

echo "Reloading NGINX configuration..."
nginx -s reload
}

# 이전 컨테이너 종료 함수
down_old_container() {
if [ "$OLD" != "none" ]; then
echo "Stopping old container: $OLD"
sudo docker stop "app_$OLD"

fi
}

# 메인 실행 로직
main() {
determine_target


# 대상 컨테이너 실행
echo "Starting $TARGET container..."
docker-compose -f docker-compose.yml up -d "app_$TARGET"

# 헬스체크
if [ "$TARGET" = "blue" ]; then
health_check "http://127.0.0.1:8080/actuator/health"
else
health_check "http://127.0.0.1:8081/actuator/health"
fi

# NGINX 설정 스위칭
switch_nginx_conf

# 이전 컨테이너 종료
down_old_container

echo "Deployment to $TARGET completed successfully!"

echo "Cleaning up dangling Docker images..."
docker image prune -f
}

# 스크립트 실행
main
12 changes: 9 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.8'
name: clokey_project

services:
app_blue:
Expand All @@ -7,7 +7,10 @@ services:
ports:
- "8080:8080"
environment:
- SPRING_PROFILES_ACTIVE=blue # 애플리케이션에서 'blue' 프로필로 설정
- SPRING_PROFILES_ACTIVE=blue
- SPRING_CONFIG_ADDITIONAL_LOCATION=file:/app/config/
volumes:
- /home/ubuntu/secret/application-secret.yml:/app/config/application-secret.yml:ro
networks:
- app_network

Expand All @@ -17,7 +20,10 @@ services:
ports:
- "8081:8080"
environment:
- SPRING_PROFILES_ACTIVE=green # 애플리케이션에서 'green' 프로필로 설정
- SPRING_PROFILES_ACTIVE=green
- SPRING_CONFIG_ADDITIONAL_LOCATION=file:/app/config/
volumes:
- /home/ubuntu/secret/application-secret.yml:/app/config/application-secret.yml:ro
networks:
- app_network

Expand Down
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
org.gradle.caching=true # ?? + ?? ?? ???
org.gradle.configureondemand=true
org.gradle.terms.of.use=true
Loading