To deploy your Spring Boot application to an AWS EC2 instance automatically using GitHub Actions (CI/CD) without needing to upload JAR files manually.
application.properties file with server.port=your_port_number.pem key file to your local machine.
Save it in a specific folder, for example key/yourkey.pem. Then open that folder using Git Bash or your terminal to run the SSH commands.Why? EC2 is your cloud server. You need SSH access (port 22) to connect, and 8080 is the default Spring Boot port.
sudo apt update
sudo apt install openjdk-21-jdk -y // Use your Java version
Why? Your Spring Boot app needs Java to run on the EC2 machine.
Create the service:
The .service file is used by systemd, which controls how your app starts, stops, and restarts.
sudo nano /etc/systemd/system/portfolio.service
Paste this:
[Unit]
Description=Spring Boot Portfolio Application
After=network.target
[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu
ExecStart=/usr/bin/java -jar /home/ubuntu/portfolio-0.0.1-SNAPSHOT.jar
SuccessExitStatus=143
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target