diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml new file mode 100644 index 0000000..0bd3be8 --- /dev/null +++ b/.github/workflows/cicd.yml @@ -0,0 +1,40 @@ +name: Java CI/CD with Gradle + +on: + push: + branches: [ "dev" ] + pull_request: + branches: [ "dev" ] + +jobs: + deploy: + + runs-on: ubuntu-latest + + steps: + - name: Set up SSH + run: | + mkdir -p ~/.ssh + echo "${{ secrets.EC2_KEY }}" > ~/.ssh/id_rsa + chmod 400 ~/.ssh/id_rsa + ssh-keyscan ${{ secrets.HOST }} >> ~/.ssh/known_hosts + chmod 600 ~/.ssh/known_hosts + + - name: Build & Deploy + run: | + ssh ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} ' + cd /home/ubuntu && + git pull && + ./gradlew clean build && + sudo cp build/libs/spotserver-0.0.1-SNAPSHOT.jar /home/ubuntu/spotserver-0.0.1-SNAPSHOT.jar + ' + + - name: Restart Service + run: | + ssh ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} ' + # 기존 spotserver 프로세스 종료 + pkill -f "spotserver-0.0.1-SNAPSHOT.jar" || true && + # 새로운 spotserver 실행 + nohup java -jar /home/ubuntu/spotserver-0.0.1-SNAPSHOT.jar > /home/ubuntu/nohup.out 2>&1 & + ' +