diff --git a/.github/workflows/mailing-ci-cd.yml b/.github/workflows/mailing-ci-cd.yml index 21a6ac9d..ebdb663f 100644 --- a/.github/workflows/mailing-ci-cd.yml +++ b/.github/workflows/mailing-ci-cd.yml @@ -1,51 +1,106 @@ -name: Mailing CI/CD Pipeline +name: CI/CD Pipeline on: push: branches: - - main + - Mailing pull_request: branches: - - main + - Mailing jobs: build: runs-on: ubuntu-latest steps: - - name: Checkout code - uses: actions/checkout@v2 - - - name: Set up JDK 17 - uses: actions/setup-java@v2 - with: - distribution: 'temurin' - java-version: '17' - - - name: Build with Maven - run: mvn clean package -DskipTests - working-directory: mailing - - - name: Deploy JAR to VPS - uses: appleboy/ssh-action@v0.1.2 - with: - host: ${{ secrets.DEPLOYMENT_MACHINE_IP }} - username: ${{ secrets.DEPLOYMENT_MACHINE_USER }} - password: ${{ secrets.DEPLOYMENT_MACHINE_USER_PSW }} - script: | - scp -o StrictHostKeyChecking=no mailing/target/service-mailing.jar ${{ secrets.DEPLOYMENT_MACHINE_USER }}@${{ secrets.DEPLOYMENT_MACHINE_IP }}:~/mailing-service/ - sleep 10 - - echo "Starting Spring Boot service..." - cd ~/mailing-service - nohup java -jar service-mailing.jar --server.port=8081 > service-mailing.log 2>&1 & - echo "Spring Boot service deployed successfully!" - - sleep 30 - - echo "Performing health check..." - curl -X 'GET' 'http://localhost:8081' -H 'accept: application/json' || (echo "Service health check failed!" && exit 1) - - echo "Service deployed and healthy!" - - tail -n 50 ~/mailing-service/service-mailing.log \ No newline at end of file + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up JDK 17 + uses: actions/setup-java@v2 + with: + distribution: 'temurin' + java-version: '17' + + - name: Build with Maven + run: | + cd mailing/mailing/ + mvn clean package -DskipTests + + - name: Archive JAR Artifact + uses: actions/upload-artifact@v3 + with: + name: service-mailing.jar + path: mailing/mailing/target/mailing-1.0.0-SNAPSHOT.jar + + deploy: + runs-on: ubuntu-latest + needs: build + timeout-minutes: 30 + + steps: + - name: Install sshpass + run: sudo apt-get install -y sshpass + + - name: Download JAR Artifact + uses: actions/download-artifact@v3 + with: + name: service-mailing.jar + path: ./ + + - name: Verify Downloaded Files + run: | + ls -l + + - name: Add Server to Known Hosts + run: | + mkdir -p ~/.ssh + ssh-keyscan -H ${{ secrets.DEPLOYMENT_MACHINE_IP }} >> ~/.ssh/known_hosts + + - name: Stop Old Process + run: | + sshpass -p "${{ secrets.DEPLOYMENT_MACHINE_USER_PSW }}" ssh ${{ secrets.DEPLOYMENT_MACHINE_USER }}@${{ secrets.DEPLOYMENT_MACHINE_IP }} \ + "ps aux | grep 'service-mailing.jar' | grep -v grep | awk '{print \$2}' | xargs -r kill -9 || echo 'No old process running'" + + - name: Create destination directory on VPS + run: | + sshpass -p "${{ secrets.DEPLOYMENT_MACHINE_USER_PSW }}" ssh ${{ secrets.DEPLOYMENT_MACHINE_USER }}@${{ secrets.DEPLOYMENT_MACHINE_IP }} \ + "mkdir -p ~/mailing-service" + + - name: Transfer JAR to VPS + run: | + sshpass -p "${{ secrets.DEPLOYMENT_MACHINE_USER_PSW }}" scp service-mailing.jar \ + ${{ secrets.DEPLOYMENT_MACHINE_USER }}@${{ secrets.DEPLOYMENT_MACHINE_IP }}:~/mailing-service/service-mailing.jar + + - name: Verify if the JAR file exists on the VPS + run: | + sshpass -p "${{ secrets.DEPLOYMENT_MACHINE_USER_PSW }}" ssh ${{ secrets.DEPLOYMENT_MACHINE_USER }}@${{ secrets.DEPLOYMENT_MACHINE_IP }} \ + "ls -l ~/mailing-service/service-mailing.jar" + + - name: Show system resources usage after file transfer + run: | + sshpass -p "${{ secrets.DEPLOYMENT_MACHINE_USER_PSW }}" ssh ${{ secrets.DEPLOYMENT_MACHINE_USER }}@${{ secrets.DEPLOYMENT_MACHINE_IP }} \ + "df -h && free -h && top -bn1 | grep 'Cpu(s)'" + + - name: Deploy JAR to VPS + uses: appleboy/ssh-action@v0.1.2 + with: + host: ${{ secrets.DEPLOYMENT_MACHINE_IP }} + username: ${{ secrets.DEPLOYMENT_MACHINE_USER }} + password: ${{ secrets.DEPLOYMENT_MACHINE_USER_PSW }} + script: | + sleep 10 + + echo "Starting Spring Boot service..." + cd ~/mailing-service + nohup java -jar service-mailing.jar --server.port=8081 > service-mailing.log 2>&1 & + echo "Spring Boot service deployed successfully!" + + sleep 30 + + echo "Performing health check..." + curl -X 'GET' 'http://localhost:8081' -H 'accept: application/json' || (echo "Service health check failed!" && exit 1) + + echo "Service deployed and healthy!" + + tail -n 50 ~/mailing-service/service-mailing.log