[Main Merge] Plantory-V1 #89
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: PLANTORY CI/CD | |
| on: | |
| push: | |
| branches: [ main ] | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v3 | |
| - name: Set up JDK 17 # Java 개발 킷 설정 | |
| uses: actions/setup-java@v3 | |
| with: | |
| distribution: 'temurin' | |
| java-version: '17' | |
| - name: Make application.yml # application.yml 파일 생성 | |
| run: | | |
| mkdir -p src/main/resources | |
| cd ./src/main/resources | |
| echo "${{ secrets.APPLICATION_YML }}" > ./application.yml | |
| shell: bash | |
| - name: Grant execute permission for gradlew # gradlew 실행 권한 부여 | |
| run: chmod +x gradlew | |
| - name: Build Spring Boot App | |
| run: ./gradlew clean build -x test | |
| - name: Log in to DockerHub | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
| - name: Build and Push Docker image | |
| uses: docker/build-push-action@v4 | |
| with: | |
| context: . | |
| push: true | |
| tags: ${{ secrets.DOCKERHUB_USERNAME }}/plantory:latest | |
| deploy: | |
| needs: build # build 작업이 성공적으로 완료된 후 실행 | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Deploy to EC2 # EC2에 배포 | |
| uses: appleboy/ssh-action@master | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USERNAME }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script: | | |
| echo "💡 컨테이너 중지 및 삭제 시도 중..." | |
| CONTAINER_ID=$(docker ps -q --filter "name=plantory-app") | |
| if [ -n "$CONTAINER_ID" ]; then | |
| echo "🔴 기존 컨테이너를 중지하고 제거합니다." | |
| docker stop $CONTAINER_ID | |
| docker rm $CONTAINER_ID | |
| else | |
| echo "🟢 중지할 기존 컨테이너 없음." | |
| fi | |
| echo "📦 최신 Docker 이미지 pull 중..." | |
| docker pull ${{ secrets.DOCKERHUB_USERNAME }}/plantory:latest | |
| echo "🚀 새 컨테이너 실행 중..." | |
| docker run -dp 8080:8080 \ | |
| --name plantory-app \ | |
| -v /home/ubuntu/plantory-config/fcm:/home/ubuntu/plantory-config/fcm \ | |
| -v /home/ubuntu/plantory-config/apple:/home/ubuntu/plantory-config/apple \ | |
| -e SPRING_PROFILES_ACTIVE=prod \ | |
| -e JAVA_TOOL_OPTIONS=-Duser.timezone=Asia/Seoul \ | |
| ${{ secrets.DOCKERHUB_USERNAME }}/plantory:latest |