🚀 Goal:

To deploy your Spring Boot application to an AWS EC2 instance automatically using GitHub Actions (CI/CD) without needing to upload JAR files manually.


✅ Step-by-Step CI/CD Flow

✅ 1. Launch an EC2 Instance

Why? EC2 is your cloud server. You need SSH access (port 22) to connect, and 8080 is the default Spring Boot port.


✅ 2. Install Java on EC2 (one time)

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.


✅ 3. Create a systemd Service

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