Merge pull request #107 from freeMates/20250602_#99_기능추가_코스_추천코스_enti… #52
This file contains hidden or 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
| name: daye200-LAB-BE-CICD | |
| on: | |
| push: | |
| branches: | |
| - main | |
| - test | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: 코드 체크아웃 | |
| uses: actions/checkout@v4 | |
| - name: Java 설정 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '21' | |
| distribution: 'temurin' | |
| cache: 'gradle' | |
| - name: Gradle Wrapper 실행권한 부여 | |
| run: chmod +x gradlew | |
| - name: application-prod.yml 생성 | |
| run: | | |
| mkdir -p src/main/resources | |
| echo "${{ secrets.APPLICATION_PROD_YML }}" > ./src/main/resources/application-prod.yml | |
| # prod 프로파일을 활성화하여 빌드 (테스트 코드 제외) | |
| - name: Build with Gradle | |
| run: ./gradlew clean build -x test -Dspring.profiles.active=prod | |
| - name: Docker 빌드환경 설정 | |
| uses: docker/setup-buildx-action@v3 | |
| - name: DockerHub 로그인 | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_HUB_TOKEN }} | |
| - name: Cache Docker layers | |
| uses: actions/cache@v4 | |
| with: | |
| path: /tmp/.buildx-cache | |
| key: ${{ runner.os }}-buildx-${{ hashFiles('Dockerfile') }} | |
| restore-keys: | | |
| ${{ runner.os }}-buildx- | |
| - name: Docker 이미지 빌드 및 푸시 | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: ./Dockerfile | |
| push: true | |
| tags: ${{ secrets.DOCKER_USERNAME }}/daye200-lab-back:${{ github.ref_name }} | |
| cache-from: type=local,src=/tmp/.buildx-cache | |
| cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max | |
| - name: Move Docker cache | |
| run: | | |
| rm -rf /tmp/.buildx-cache | |
| mv /tmp/.buildx-cache-new /tmp/.buildx-cache | |
| deploy: | |
| needs: build | |
| runs-on: ubuntu-latest | |
| steps: | |
| # SSH 디버깅을 위해 임시 단계 추가 (문제 해결 후 제거 가능) | |
| - name: Test SSH Connection | |
| run: | | |
| echo "$SERVER_KEY" > key.pem | |
| chmod 400 key.pem | |
| ssh -v -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i key.pem ubuntu@$SERVER_HOSTS "echo SSH 연결 성공" | |
| env: | |
| SERVER_KEY: ${{ secrets.SERVER_KEY }} | |
| SERVER_HOSTS: ${{ secrets.SERVER_HOSTS }} | |
| - name: Deploy to EC2 | |
| uses: appleboy/[email protected] | |
| with: | |
| host: ${{ secrets.SERVER_HOSTS }} | |
| username: ubuntu | |
| key: ${{ secrets.SERVER_KEY }} | |
| port: 22 | |
| debug: true | |
| script: | | |
| set -e | |
| echo "환경변수 설정.." | |
| export PATH=$PATH:/usr/local/bin | |
| BRANCH=${{ github.ref_name }} | |
| PORT=8080 | |
| CONTAINER_NAME="daye200-lab-back" | |
| if [ "$BRANCH" == "main" ]; then | |
| PORT=8087 | |
| elif [ "$BRANCH" == "test" ]; then | |
| CONTAINER_NAME="daye200-lab-back-test" | |
| PORT=8088 | |
| fi | |
| echo "브랜치: $BRANCH" | |
| echo "컨테이너 이름: $CONTAINER_NAME" | |
| echo "포트: $PORT" | |
| echo "도커 이미지 풀 : ${{ secrets.DOCKER_USERNAME }}/daye200-lab-back:${BRANCH}" | |
| sudo docker pull ${{ secrets.DOCKER_USERNAME }}/daye200-lab-back:${BRANCH} | |
| echo "컨테이너 $CONTAINER_NAME 존재 여부 확인 중..." | |
| if sudo docker ps -a --format '{{.Names}}' | grep -Eq "^${CONTAINER_NAME}\$"; then | |
| echo "컨테이너 $CONTAINER_NAME 이(가) 존재합니다. 중지 및 삭제 중..." | |
| sudo docker rm -f $CONTAINER_NAME | |
| echo "컨테이너 $CONTAINER_NAME 이(가) 삭제되었습니다." | |
| else | |
| echo "존재하는 컨테이너 $CONTAINER_NAME 이(가) 없습니다." | |
| fi | |
| echo "새로운 컨테이너 $CONTAINER_NAME 실행 중..." | |
| sudo docker run -d -p ${PORT}:8080 --name $CONTAINER_NAME \ | |
| -e TZ=Asia/Seoul \ | |
| -e "SPRING_PROFILES_ACTIVE=prod" \ | |
| -v /etc/localtime:/etc/localtime:ro \ | |
| -v /volume1/project/daye200_lab/backend:/mnt/daye200_lab \ | |
| ${{ secrets.DOCKER_USERNAME }}/daye200-lab-back:${BRANCH} | |
| echo "불필요한 dangling(<none>) 이미지 정리..." | |
| sudo docker image prune -af | |
| echo "배포가 성공적으로 완료되었습니다." |