Update inventory-ci-cd.yml #6
This file contains 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: CI/CD Pipeline | |
on: | |
push: | |
branches: | |
- inventory | |
pull_request: | |
branches: | |
- inventory | |
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: | | |
cd inventory/inventory-service/ | |
mvn clean package -DskipTests | |
- name: Archive JAR Artifact | |
uses: actions/upload-artifact@v3 | |
with: | |
name: service-inventory.jar | |
path: inventory/inventory-service/target/*.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-inventory.jar | |
path: ./ | |
- name: Verify if the correct JAR file exists | |
run: | | |
ls /home/runner/work/projet-soa-ecommerce-enit-2024-3AINFO2/projet-soa-ecommerce-enit-2024-3AINFO2/service-inventory.jar || (echo "JAR file not found!" && exit 1) | |
- name: Add Server to Known Hosts | |
run: | | |
mkdir -p ~/.ssh | |
ssh-keyscan -H ${{ secrets.DEPLOYMENT_MACHINE_IP }} >> ~/.ssh/known_hosts | |
- name: Transfer JAR to VPS | |
run: | | |
sshpass -p "${{ secrets.DEPLOYMENT_MACHINE_USER_PSW }}" scp ./target/service-inventory-*.jar \ | |
${{ secrets.DEPLOYMENT_MACHINE_USER }}@${{ secrets.DEPLOYMENT_MACHINE_IP }}:~/service-inventory/service-inventory.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 ~/inventory-service/service-inventory.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 ~/inventory-service | |
nohup java -jar service-inventory.jar --server.port=8083 > service-inventory.log 2>&1 & | |
echo "Spring Boot service deployed successfully!" | |
sleep 30 | |
echo "Performing health check..." | |
curl -X 'GET' 'http://localhost:8083/actuator/health' -H 'accept: application/json' || (echo "Service health check failed!" && exit 1) | |
echo "Service deployed and healthy!" | |
tail -n 50 ~/inventory-service/service-inventory.log |