diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 73bb8fc..c01c42b 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -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