From f9534d26a1de0682d5bac5ec2aa687cf4829cd74 Mon Sep 17 00:00:00 2001 From: Mincheol Shin <48898994+its-sky@users.noreply.github.com> Date: Sat, 30 Dec 2023 16:03:35 +0900 Subject: [PATCH 1/8] =?UTF-8?q?[Docs]=20#10=20-=20CI.yml=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/CI.yml | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 .github/workflows/CI.yml diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml new file mode 100644 index 00000000..2272f153 --- /dev/null +++ b/.github/workflows/CI.yml @@ -0,0 +1,34 @@ +name: CI + +on: + pull_request: + branches: [ "develop" ] + +jobs: + build: + runs-on: ubuntu-22.04 + env: + working-directory: moonshot + + steps: + - name: checkout + uses: actions/checkout@v3 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + distribution: 'corretto' + java-version: '17' + + - name: application.yaml 작성 + run: | + cd src/main/resources + echo "${{ secrets.APPLICATION }}" > ./application-deploy.yaml + working-directory: ${{ env.working-directory }} + + - name: build + run: | + chmod +x gradlew + ./gradlew clean build + working-directory: ${{ env.working-directory }} + shell: bash From 617a3287dd41e88068a119d9c9b8eb0d3857a6c8 Mon Sep 17 00:00:00 2001 From: its-sky Date: Sat, 30 Dec 2023 16:50:51 +0900 Subject: [PATCH 2/8] =?UTF-8?q?[Docs]=20#10=20:=20CD.yml=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/CD.yml | 64 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 .github/workflows/CD.yml diff --git a/.github/workflows/CD.yml b/.github/workflows/CD.yml new file mode 100644 index 00000000..560207b0 --- /dev/null +++ b/.github/workflows/CD.yml @@ -0,0 +1,64 @@ +name: CD + +on: + push: + branches: [ "develop" ] + +jobs: + deploy-ci: + runs-on: ubuntu-22.04 + env: + working-directory: moonshot + + steps: + - name: checkout + uses: actions/checkout@v3 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + distribution: 'corretto' + java-version: '17' + + - name: application.yaml 생성 + run: | + cd src/main/resources + echo "${{ secrets.APPLICATION }}" > ./application-deploy.yaml + working-directory: ${{ env.working-directory }} + + - name: build + run: | + chmod +x gradlew + ./gradlew clean build + working-directory: ${{ env.working-directory }} + shell: bash + + - name: docker build setting + uses: docker/setup-buildx-action@v2.9.1 + + - name: docker hub login + uses: docker/login-action@v2.2.0 + with: + username: ${{ secrets.DOCKERHUB_LOGIN_USERNAME }} + password: ${{ secrets.DOCKERHUB_LOGIN_ACCESSTOKEN }} + + - name: docker image build and push + run: | + docker build --platform linux/amd64 -t teammoonshot/server . + docker push teammoonshot/server + working-directory: ${{ env.working-directory }} + + deploy-cd: + needs: deploy-ci + runs-on: ubuntu-22.04 + + steps: + - name: docker container running + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.RELEASE_SERVER_IP }} + username: ${{ secrets.RELEASE_SERVER_USER }} + key: ${{ secrets.RELEASE_SERVER_KEY }} + script: | + cd ~ + ./deploy.sh \ No newline at end of file From 42e718c824e857e99e56f169308880f82d44d118 Mon Sep 17 00:00:00 2001 From: its-sky Date: Sat, 30 Dec 2023 16:55:45 +0900 Subject: [PATCH 3/8] =?UTF-8?q?[Feat]=20#10=20:=20Dockerfile=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..fb5088d6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM amd64/amazoncorretto:17 + +WORKDIR /app + +COPY ./build/libs/server-0.0.1-SNAPSHOT.jar /app/moonshot.jar + +CMD ["java", "-Duser.timezone=Asia/Seoul", "-jar", "-Dspring.profiles.active=deploy", "moonshot.jar"] \ No newline at end of file From 14a40b4a1af019da79ed04e952f5bbab4fc750f9 Mon Sep 17 00:00:00 2001 From: its-sky Date: Sat, 30 Dec 2023 17:01:24 +0900 Subject: [PATCH 4/8] =?UTF-8?q?[Chore]=20#10=20:=20Spring=20actuator=20gra?= =?UTF-8?q?dle=20dependency=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 +++ 1 file changed, 3 insertions(+) diff --git a/build.gradle b/build.gradle index 470e5014..dd126b8f 100644 --- a/build.gradle +++ b/build.gradle @@ -36,6 +36,9 @@ dependencies { annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.security:spring-security-test' + + // spring-actuator + implementation 'org.springframework.boot:spring-boot-starter-actuator' } dependencyManagement { From 674db31062b7d3a2d63adb8e75ff72000651a215 Mon Sep 17 00:00:00 2001 From: its-sky Date: Sat, 30 Dec 2023 17:01:39 +0900 Subject: [PATCH 5/8] =?UTF-8?q?[Docs]=20#10=20:=20deploy.sh=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy.sh | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 deploy.sh diff --git a/deploy.sh b/deploy.sh new file mode 100644 index 00000000..e370ce2c --- /dev/null +++ b/deploy.sh @@ -0,0 +1,99 @@ +#!/bin/bash + +nginx_config_path="/etc/nginx" +all_port=("8080" "8081") +available_port=() +server_name=server + +docker_ps_output=$(docker ps | grep $server_name) +running_container_name=$(echo "$docker_ps_output" | awk '{print $NF}') +blue_port=$(echo "$running_container_name" | awk -F'-' '{print $NF}') +web_health_check_url=/actuator/health + +if [ -z "$blue_port" ]; then + echo "> 실행 중인 서버의 포트: 없음" +else + echo "> 실행 중인 서버의 포트: $blue_port" +fi + +# 실행 가능한 포트 확인 ( all_port 중 blue_port를 제외한 port ) +for item in "${all_port[@]}"; do + if [ "$item" != "$blue_port" ]; then + available_port+=("$item") + fi +done + +# 실행 가능한 포트 없으면 끝내기 +if [ ${#available_port[@]} -eq 0 ]; then + echo "> 실행 가능한 포트가 없습니다." + exit 1 +fi + +green_port=${available_port[0]} + +echo "----------------------------------------------------------------------" +# docker image pull +echo "> 도커 이미지 pull 받기" +docker pull teammoonshot/${server_name} + +# green_port로 서버 실행 +echo "> ${green_port} 포트로 서버 실행" +echo "> docker run -d --name ${server_name}-${green_port} -p ${green_port}:8080 -e TZ=Asia/Seoul teammoonshot/${server_name}" +docker run -d --name ${server_name}-${green_port} -p ${green_port}:8080 -e TZ=Asia/Seoul teammoonshot/${server_name} +echo "----------------------------------------------------------------------" + +# green_port 서버 제대로 실행 중인지 확인 +sleep 10 +for retry_count in {1..10} +do + echo "> 서버 상태 체크" + echo "> curl -s http://localhost:${green_port}${web_health_check_url}" + # http://localhost:{그린포트}{health check 주소} -> nginx + response=$(curl -s http://localhost:${green_port}${web_health_check_url}) + up_count=$(echo $response | grep 'UP' | wc -l) + + if [ $up_count -ge 1 ] + then + echo "> 서버 실행 성공" + break + else + echo "> 아직 서버 실행 안됨" + echo "> 응답 결과: ${response}" + fi + if [ $retry_count -eq 10 ] + then + echo "> 서버 실행 실패" + docker rm -f ${server_name}-${green_port} + + exit 1 + fi + sleep 2 +done +echo "----------------------------------------------------------------------" + +# nginx switching +echo "> nginx 포트 스위칭" +echo "set \$service_url http://127.0.0.1:${green_port};" | sudo tee ${nginx_config_path}/conf.d/service-url.inc +sudo nginx -s reload + +sleep 1 + +echo "----------------------------------------------------------------------" +# nginx를 통해서 서버 접근 가능한지 확인 +response=$(curl -s http://localhost${web_health_check_url}) +up_count=$(echo $response | grep 'UP' | wc -l) +if [ $up_count -ge 1 ] +then + echo "> 서버 변경 성공" +else + echo "> 서버 변경 실패" + echo "> 서버 응답 결과: ${response}" + exit 1 +fi + +# blue_port 서버 있다면 중단 +if [ -n "$blue_port" ]; then + echo "> 기존 ${blue_port}포트 서버 중단" + echo "> docker rm -f ${server_name}-${blue_port}" + sudo docker rm -f ${server_name}-${blue_port} +fi \ No newline at end of file From 07d4b400c86e29577c99c058d2b215fec663c4c1 Mon Sep 17 00:00:00 2001 From: its-sky Date: Sat, 30 Dec 2023 17:02:25 +0900 Subject: [PATCH 6/8] =?UTF-8?q?[Add]=20#10=20:=20Security=20Config=20/actu?= =?UTF-8?q?ator/health=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/moonshot/server/global/config/SecurityConfig.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/moonshot/server/global/config/SecurityConfig.java b/src/main/java/org/moonshot/server/global/config/SecurityConfig.java index 8e815fe4..765e81e4 100644 --- a/src/main/java/org/moonshot/server/global/config/SecurityConfig.java +++ b/src/main/java/org/moonshot/server/global/config/SecurityConfig.java @@ -20,7 +20,8 @@ public class SecurityConfig { private static final String[] WHITELIST = { "/login/**", - "/" + "/", + "/actuator/health" }; private final MoonshotExceptionHandler moonshotExceptionHandler; From e315e2f55bd9e0e104b5a8edacf0f380328c98d7 Mon Sep 17 00:00:00 2001 From: its-sky Date: Sat, 30 Dec 2023 17:11:11 +0900 Subject: [PATCH 7/8] =?UTF-8?q?[Fix]=20#10=20:=20CI.yml,=20CD.yml=20workin?= =?UTF-8?q?g=20directory=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/CD.yml | 5 ----- .github/workflows/CI.yml | 4 ---- 2 files changed, 9 deletions(-) diff --git a/.github/workflows/CD.yml b/.github/workflows/CD.yml index 560207b0..ec9c1678 100644 --- a/.github/workflows/CD.yml +++ b/.github/workflows/CD.yml @@ -7,8 +7,6 @@ on: jobs: deploy-ci: runs-on: ubuntu-22.04 - env: - working-directory: moonshot steps: - name: checkout @@ -24,13 +22,11 @@ jobs: run: | cd src/main/resources echo "${{ secrets.APPLICATION }}" > ./application-deploy.yaml - working-directory: ${{ env.working-directory }} - name: build run: | chmod +x gradlew ./gradlew clean build - working-directory: ${{ env.working-directory }} shell: bash - name: docker build setting @@ -46,7 +42,6 @@ jobs: run: | docker build --platform linux/amd64 -t teammoonshot/server . docker push teammoonshot/server - working-directory: ${{ env.working-directory }} deploy-cd: needs: deploy-ci diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 2272f153..d3a25206 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -7,8 +7,6 @@ on: jobs: build: runs-on: ubuntu-22.04 - env: - working-directory: moonshot steps: - name: checkout @@ -24,11 +22,9 @@ jobs: run: | cd src/main/resources echo "${{ secrets.APPLICATION }}" > ./application-deploy.yaml - working-directory: ${{ env.working-directory }} - name: build run: | chmod +x gradlew ./gradlew clean build - working-directory: ${{ env.working-directory }} shell: bash From b5052b53791d2b884d2e2d890f4497230dcb3cf3 Mon Sep 17 00:00:00 2001 From: its-sky Date: Sat, 30 Dec 2023 17:28:55 +0900 Subject: [PATCH 8/8] =?UTF-8?q?[Fix]=20#10=20:=20CI.yml,=20CD.yml=20gradle?= =?UTF-8?q?=20=EB=B9=8C=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/CD.yml | 2 +- .github/workflows/CI.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/CD.yml b/.github/workflows/CD.yml index ec9c1678..9bdfa42a 100644 --- a/.github/workflows/CD.yml +++ b/.github/workflows/CD.yml @@ -26,7 +26,7 @@ jobs: - name: build run: | chmod +x gradlew - ./gradlew clean build + ./gradlew build -x test shell: bash - name: docker build setting diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index d3a25206..cd769de1 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -26,5 +26,5 @@ jobs: - name: build run: | chmod +x gradlew - ./gradlew clean build + ./gradlew build -x test shell: bash