Skip to content

Commit

Permalink
Update gradle.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongmin39 authored Nov 18, 2024
1 parent c85c94c commit cecef39
Showing 1 changed file with 25 additions and 32 deletions.
57 changes: 25 additions & 32 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,48 +51,41 @@ jobs:

- name: Setup SSH
run: |
sudo apt-get update
sudo apt-get install -y openssh-client
mkdir -p ~/.ssh
echo "${{ secrets.EC2_KEY_DEV }}" > ~/.ssh/id_rsa
echo "${{ secrets.EC2_KEY }}" | base64 -d > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H ${{ secrets.EC2_HOST_DEV }} >> ~/.ssh/known_hosts
ssh-keyscan -H ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts
- name: Upload new JAR to EC2
shell: bash
run: |
scp -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no build/libs/spotserver-0.0.1-SNAPSHOT.jar ${{ secrets.EC2_USER_DEV }}@${{ secrets.EC2_HOST_DEV }}:/home/${{ secrets.EC2_USER_DEV }}/
scp -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no build/libs/spotserver-0.0.1-SNAPSHOT.jar ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }}:/home/${{ secrets.EC2_USER }}/
- name: Execute commands on EC2
run: |
ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ${{ secrets.EC2_USER_DEV }}@${{ secrets.EC2_HOST_DEV }} << 'EOF'
ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF'
# Set PATH if needed
export PATH=/usr/bin:/bin:/sbin:/usr/sbin
# Start Redis server
sudo systemctl start redis-server
# Ensure no other process is using the port
sudo fuser -k -n tcp 8080
# Wait for a moment to ensure Redis and port are ready
sleep 10
# Kill any existing Java processes
pgrep java | xargs kill -9
# Remove old JAR if exists
rm -f /home/${{ secrets.EC2_USER_DEV }}/spotserver-0.0.1-SNAPSHOT.jar
# Run the new Java application with environment variables
nohup java -jar /home/${{ secrets.EC2_USER_DEV }}/spotserver-0.0.1-SNAPSHOT.jar \
--spring.datasource.url=${{ secrets.RDS_URL }} \
--spring.datasource.username=${{ secrets.RDS_USERNAME }} \
--spring.datasource.password=${{ secrets.RDS_PASSWORD }} \
--cloud.aws.credentials.access-key=${{ secrets.AWS_ACCESS_KEY }} \
--cloud.aws.credentials.secret-key=${{ secrets.AWS_SECRET_KEY }} \
> /home/${{ secrets.EC2_USER_DEV }}/nohup.out 2>&1 &
# Check if the application is running on the expected port
sudo lsof -i tcp:8080
if ! systemctl is-active --quiet redis-server; then
sudo systemctl start redis-server
fi
# Kill any process on port 8080 if needed
if sudo lsof -i tcp:8080; then
sudo fuser -k -n tcp 8080
fi
# Backup existing JAR
if [ -f /home/${{ secrets.EC2_USER }}/spotserver-0.0.1-SNAPSHOT.jar ]; then
mv /home/${{ secrets.EC2_USER }}/spotserver-0.0.1-SNAPSHOT.jar /home/${{ secrets.EC2_USER }}/spotserver-0.0.1-SNAPSHOT.jar.bak
fi
# Run new application
nohup java -jar /home/${{ secrets.EC2_USER }}/spotserver-0.0.1-SNAPSHOT.jar \
--spring.config.location=/home/${{ secrets.EC2_USER }}/app.env \
> /home/${{ secrets.EC2_USER }}/nohup.out 2>&1 &
# Monitor application startup logs
tail -n 20 /home/${{ secrets.EC2_USER }}/nohup.out
EOF

0 comments on commit cecef39

Please sign in to comment.