Skip to content

change things back to hello #32

change things back to hello

change things back to hello #32

Workflow file for this run

name: Java CI and Deploy
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout code
- name: Checkout code
uses: actions/checkout@v2
# Step 2: Set up JDK
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '11'
# Step 3: Grant execute permission for Gradle wrapper
- name: Grant execute permission for gradlew
run: chmod +x gradlew
# Step 4: Run tests with Gradle
- name: Run tests
run: ./gradlew test
# Step 5: Build the jar file (if applicable)
- name: Build with Gradle
run: ./gradlew build
- name: Set up SSH key for rsync
run: |
mkdir -p ~/.ssh
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H ${{ secrets.SERVER_IP }} >> ~/.ssh/known_hosts
- name: Rsync files to server
run: |
rsync -avz --delete -e "ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no" ./build/libs/*.jar ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }}:${{ secrets.SERVER_PATH }}
# Step 6: Deploy to the server
- name: Deploy to Server
if: success() # This step will only run if all previous steps are successful
run: |
ssh -i ~/.ssh/id_rsa -p 22 ${{ secrets.SSH_USER }}@${{ secrets.SERVER_IP }} << 'EOF'
# Navigate to the server directory and stop the old server (if running)
cd ${{ secrets.SERVER_PATH }} || echo "cannot change to directory"
# Kill server if another one is running
PID=$(pgrep -f 'CDDemo-1.0-SNAPSHOT.jar')
if [ -n "$PID" ]; then
kill $PID
echo "Waiting for process to terminate..."
sleep 5 # Add a 5-second delay
fi
# Start the new server
nohup java -jar ${{ secrets.SERVER_PATH }}/CDDemo-1.0-SNAPSHOT.jar > output.log 2>&1 &
EOF