-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Chore #10 개발서버 cicd 구축
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
name: GachTaxi-BE dev CI/CD | ||
|
||
on: | ||
push: | ||
branches: [ "dev" ] # develop 브랜치에 push 시 트리거 | ||
pull_request: | ||
branches: [ "dev" ] # develop 브랜치에 대한 PR 시 트리거 | ||
types: [opened, synchronize, reopened] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v4 | ||
with: | ||
java-version: '17' | ||
distribution: 'temurin' | ||
|
||
# Gradle 캐시 설정 | ||
- name: Gradle Caching | ||
uses: actions/cache@v3 | ||
with: | ||
path: | | ||
~/.gradle/caches | ||
~/.gradle/wrapper | ||
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*gradle*', '**/gradle-wrapper.properties') }} | ||
restore-keys: | | ||
${{ runner.os }}-gradle- | ||
- name: Build with Gradle Wrapper | ||
run: ./gradlew build -x test | ||
|
||
# 빌드된 JAR 파일 확인 | ||
- name: List JAR files | ||
run: ls build/libs | ||
|
||
# Docker 이미지 빌드 및 푸시 | ||
- name: Docker build & push | ||
run: | | ||
docker login -u ${{ secrets.DEV_DOCKER_USER_NAME }} -p ${{ secrets.DEV_DOCKER_USER_TOKEN }} | ||
docker buildx create --use | ||
docker buildx build --platform linux/amd64,linux/arm64 -f Dockerfile-dev -t ${{ secrets.DEV_DOCKER_USER_NAME }}/gachtaxi:latest --push . | ||
deploy: | ||
runs-on: ubuntu-latest | ||
needs: build | ||
if: github.event_name == 'push' | ||
|
||
steps: | ||
# SSH를 사용하여 원격 서버에 배포 | ||
- name: Deploy to server | ||
uses: appleboy/ssh-action@master | ||
with: | ||
host: ${{ secrets.DEV_SSH_SECRET_HOST }} | ||
username: ${{ secrets.DEV_SSH_SECRET_USER }} | ||
port: 30922 | ||
key: ${{ secrets.DEV_SSH_SECRET_PRIVATE_KEY }} | ||
script: | | ||
# Blue-Green Deployment 포트 및 컨테이너 이름 설정 | ||
BLUE_PORT=8080 | ||
GREEN_PORT=8081 | ||
BLUE_NAME="blue" | ||
GREEN_NAME="green" | ||
# 현재 실행 중인 컨테이너 확인 | ||
IS_BLUE_ON=$(sudo docker ps --filter "name=$BLUE_NAME" --filter "status=running" -q) | ||
if [ -n "$IS_BLUE_ON" ]; then | ||
echo "** ${GREEN_PORT} 포트에서 GREEN 컨테이너 실행" | ||
sudo docker run --name $GREEN_NAME -d -p $GREEN_PORT:$GREEN_PORT \ | ||
--env-file ./gachtaxi-dev.env -e TZ=Asia/Seoul ${{ secrets.DEV_DOCKER_USER_NAME }}/gachtaxi | ||
BEFORE_NAME=$BLUE_NAME | ||
AFTER_NAME=$GREEN_NAME | ||
BEFORE_PORT=$BLUE_PORT | ||
AFTER_PORT=$GREEN_PORT | ||
else | ||
echo "** ${BLUE_PORT} 포트에서 BLUE 컨테이너 실행" | ||
sudo docker run --name $BLUE_NAME -d -p $BLUE_PORT:$BLUE_PORT \ | ||
--env-file ./gachtaxi-dev.env -e TZ=Asia/Seoul ${{ secrets.DEV_DOCKER_USER_NAME }}/gachtaxi | ||
BEFORE_NAME=$GREEN_NAME | ||
AFTER_NAME=$BLUE_NAME | ||
BEFORE_PORT=$GREEN_PORT | ||
AFTER_PORT=$BLUE_PORT | ||
fi | ||
echo "** ${AFTER_NAME} 컨테이너 실행 완료 (포트: ${AFTER_PORT})" | ||
# 이전 컨테이너 중지 및 삭제 | ||
echo "** 이전 컨테이너(${BEFORE_NAME}) 종료 및 삭제" | ||
sudo docker stop $BEFORE_NAME || true | ||
sudo docker rm $BEFORE_NAME || true | ||
# 사용하지 않는 이미지 정리 | ||
echo "** 사용하지 않는 Docker 이미지 정리" | ||
sudo docker image prune -a -f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM eclipse-temurin:17-jre-focal | ||
|
||
ARG JAR_FILE=build/libs/*.jar | ||
|
||
COPY ${JAR_FILE} docker-springboot.jar | ||
|
||
ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=dev", "/docker-springboot.jar"] |