Skip to content

Deploy Spring Boot App #13

Deploy Spring Boot App

Deploy Spring Boot App #13

Workflow file for this run

name: Deploy Spring Boot App
on:
workflow_dispatch: # manual trigger
push:
branches:
- master
jobs:
deploy:
runs-on: ubuntu-latest
env:
APP_NAME: 'waa-project-spring-backend'
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Java
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'
- name: Build with Maven
run: mvn clean package -DskipTests
- name: Set up SSH key
run: |
echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > ec2_key.pem
chmod 600 ec2_key.pem
- name: Create directory on EC2
run: |
ssh -i ec2_key.pem -o StrictHostKeyChecking=no ec2-user@${{ secrets.EC2_HOST }} "mkdir -p ~/${{ env.APP_NAME }}"
- name: Copy JAR to EC2
run: |
scp -i ec2_key.pem -o StrictHostKeyChecking=no target/${{ env.APP_NAME }}-0.0.1-SNAPSHOT.jar ec2-user@${{ secrets.EC2_HOST }}:~/${{ env.APP_NAME }}/app.jar
- name: Stop old Spring Boot app on EC2
run: |
ssh -i ec2_key.pem -o StrictHostKeyChecking=no ec2-user@${{ secrets.EC2_HOST }} << 'EOF'
PORT=${{ secrets.API_LISTENING_PORT }}
PID=$(sudo ss -tulnp | grep -w ":$PORT" | awk -F'[=,]' '{print $3}')
if [ -n "$PID" ]; then
echo "Stopping Spring Boot app running on port $PORT with PID: $PID"
sudo kill $PID
sleep 10
if ps -p $PID > /dev/null; then
echo "Process did not terminate gracefully, force killing."
sudo kill -9 $PID
fi
else
echo "No application running on port $PORT"
fi
EOF
- name: Run the Spring Boot app on EC2 with Environment Variables
run: |
ssh -i ec2_key.pem -o StrictHostKeyChecking=no ec2-user@${{ secrets.EC2_HOST }} << 'EOF'
export DB_HOST="${{ secrets.DB_HOST }}"
export DB_PORT="${{ secrets.DB_PORT }}"
export DB_USERNAME="${{ secrets.DB_USERNAME }}"
export DB_PASSWORD="${{ secrets.DB_PASSWORD }}"
export DB_NAME="${{ secrets.DB_NAME }}"
export API_LISTENING_PORT="${{ secrets.API_LISTENING_PORT }}"
sudo -E nohup java -jar ~/${{ env.APP_NAME }}/app.jar > ~/${{ env.APP_NAME }}/app.log 2>&1 &
EOF