Skip to content

merge dev2 to main #135

merge dev2 to main

merge dev2 to main #135

name: Build and Deploy
on:
push:
branches: ["main"]
pull_request:
branches: ["main"]
permissions:
contents: read
jobs:
build-docker-image:
runs-on: ubuntu-latest
steps:
# 리포지토리 체크아웃
- name: Checkout repository
uses: actions/checkout@v3
# JDK 17 설정
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
# 줄 끝 형식을 Unix 형식으로 변환
- name: Convert line endings to Unix
run: |
sudo apt-get install dos2unix
dos2unix ./gradlew
# gradlew에 실행 권한 부여
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
# Gradle을 사용하여 Spring Boot 애플리케이션 빌드
- name: Build with Gradle
run: ./gradlew clean bootJar
# Docker 이미지를 빌드
- name: Docker image build
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/cogoserver .
# Docker Hub에 로그인
- name: Docker login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
# Docker 이미지를 Docker Hub에 푸시
- name: Docker Hub push
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/cogoserver
run-docker-image-on-ec2:
needs: build-docker-image
runs-on: self-hosted
steps:
# Docker 이미지를 EC2에서 pull
- name: Docker pull
run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/cogoserver
# 기존 cogo 컨테이너 중지
- name: Docker stop container
run: sudo docker stop cogo 2>/dev/null || true
# 기존 cogo 컨테이너 삭제
- name: Docker remove container
run: sudo docker rm cogo 2>/dev/null || true
# 새 컨테이너 실행
- name: Docker run new container
run: |
sudo docker run --name cogo --rm -d -p 8080:8080 \
-e DB_URL=${{ secrets.DB_URL }} \
-e DB_NAME=${{ secrets.DB_NAME }} \
-e DB_USERNAME=${{ secrets.DB_USERNAME }} \
-e DB_PW=${{ secrets.DB_PW }} \
-e MAIL_SENDER=${{ secrets.MAIL_SENDER }} \
-e MAIL_PW=${{ secrets.MAIL_PW }} \
-e JWT_SECRET=${{ secrets.JWT_SECRET }} \
-e NAVER_ID=${{ secrets.NAVER_ID }} \
-e NAVER_SECRET=${{ secrets.NAVER_SECRET }} \
-e NAVER_REDIRECT=${{ secrets.NAVER_REDIRECT }} \
-e GOOGLE_ID=${{ secrets.GOOGLE_ID }} \
-e GOOGLE_SECRET=${{ secrets.GOOGLE_SECRET }} \
-e GOOGLE_REDIRECT=${{ secrets.GOOGLE_REDIRECT }} \
-e KAKAO_ID=${{ secrets.KAKAO_ID }} \
-e KAKAO_SECRET=${{ secrets.KAKAO_SECRET }} \
-e KAKAO_REDIRECT=${{ secrets.KAKAO_REDIRECT }} \
-e BUCKET_NAME=${{ secrets.BUCKET_NAME }} \
-e S3_ACCESS=${{ secrets.S3_ACCESS }} \
-e S3_SECRET=${{ secrets.S3_SECRET }} \
-e REDIS_HOST=${{ secrets.REDIS_HOST }} \
-e REDIS_PORT=${{ secrets.REDIS_PORT }} \
-e SMS_KEY=${{ secrets.SMS_KEY }} \
-e SMS_SECRET=${{ secrets.SMS_SECRET }} \
-e SMS_FROM=${{ secrets.SMS_FROM }} \
${{ secrets.DOCKERHUB_USERNAME }}/cogoserver
sleep 5 # 컨테이너 실행 시간을 확보
sudo docker ps -a # 모든 컨테이너 목록 출력
# 실행된 컨테이너의 로그 확인
- name: Check Docker logs
run: sudo docker logs cogo || true # 컨테이너 로그 출력
# 미사용 Docker 이미지 삭제
- name: Delete old docker image
run: sudo docker system prune -f