Update gradle.yml #20
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: Java Deploy | |
on: | |
push: | |
branches: [ "main" ] | |
pull_request: | |
branches: [ "main" ] | |
jobs: | |
Deploy: | |
name: Deploy to EC2 | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Build & Deploy | |
env: | |
PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
HOSTNAME: ${{ secrets.SERVER_IP }} | |
USER_NAME: ${{ secrets.SSH_USERNAME }} | |
run: | | |
echo "$PRIVATE_KEY" > private_key | |
chmod 600 private_key | |
ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOSTNAME} << 'EOF' | |
# Navigate to the application directory | |
cd /home/ubuntu/FOREGG_SERVER || | |
{ echo "Failed to navigate to directory"; exit 1; } | |
# Fetch the latest code | |
git checkout main && | |
git fetch --all && | |
git reset --hard origin/main && | |
git pull origin main || | |
{ echo "Failed to update code"; exit 1; } | |
# Build the Spring application using Gradle | |
./gradlew clean build || | |
{ echo "Gradle build failed"; exit 1; } | |
# Find the process ID (PID) using port 8080 and kill it | |
PID=$(sudo netstat -tnlp | grep ":8080" | awk '{print $7}' | cut -d/ -f1) && | |
if [ -n "$PID" ]; then | |
echo "Stopping process with PID $PID" && | |
sudo kill -9 $PID | |
else | |
echo "No process found on port 8080" | |
fi || | |
{ echo "Failed to stop old process"; exit 1; } | |
# Replace the old JAR with the new one | |
sudo cp *.jar ~/cicd || | |
{ echo "Failed to copy JAR file"; exit 1; } | |
# Start the application again | |
nohup java -jar ~/cicd/*.jar > /dev/null 2>&1 & | |
EOF |