|
| 1 | +# CI-CD for a Standalone Java App |
| 2 | +Deploy the [Spring PetClinic Application](https://github.com/spring-projects/spring-petclinic) on Amazon Cloud using Github Actions Workflow. |
| 3 | + |
1 | 4 | ## Run Petclinic locally |
2 | 5 |
|
3 | 6 | Spring Petclinic is a [Spring Boot](https://spring.io/guides/gs/spring-boot) application built using [Maven](https://spring.io/guides/gs/maven/) or [Gradle](https://spring.io/guides/gs/gradle/). You can build a jar file and run it from the command line (it should work just as well with Java 17 or newer): |
4 | 7 |
|
| 8 | +Create fork of the repository: https://github.com/spring-projects/spring-petclinic.git |
5 | 9 | ```bash |
6 | | -git clone https://github.com/spring-projects/spring-petclinic.git |
| 10 | +git clone <your-repository> |
7 | 11 | cd spring-petclinic |
8 | | -./mvnw package |
9 | | -java -jar target/*.jar |
| 12 | +chmod +x mvnw |
| 13 | +./mvnw package (to build, test and generate artifacts) |
| 14 | +java -jar target/*.jar (starts the application) |
| 15 | +``` |
| 16 | +You can then access the Petclinic at <http://localhost:8080/> |
| 17 | +</br> |
| 18 | + |
| 19 | +```bash |
| 20 | +cd target |
| 21 | +ls |
| 22 | +.nvmw test (for running unit tests) |
10 | 23 | ``` |
| 24 | +Run Sonarqube analysis locally on java code and check results on Sonarqube dashboard ```http://<IP-address>:9000``` |
| 25 | + |
| 26 | +```bash |
| 27 | +mvn clean verify sonar:sonar \ |
| 28 | + -Dsonar.projectKey=<sonarqube-project-name> \ |
| 29 | + -Dsonar.projectName='<sonarqube-project-name>' \ |
| 30 | + -Dsonar.host.url=<sonarqube-host-url> \ |
| 31 | + -Dsonar.token=<your-sonarqube-project-token> |
| 32 | +``` |
| 33 | +--- |
| 34 | +Now proceed with CI-CD using Githb Actions. |
| 35 | + |
| 36 | +## Steps: |
| 37 | +- Running the Petclinic App locally to ensure it works as expected (optional) |
| 38 | +- Setting up a Continuous Integration (CI) pipeline using GitHub Actions to build and test the code using Maven |
| 39 | +- Integrating SonarQube for code quality analysis |
| 40 | +- Packaging and storing artifacts (JAR files) |
| 41 | +- Deploying the JAR file to an AWS EC2 instance |
| 42 | +</br> |
| 43 | + |
| 44 | +### Continuous Integration |
| 45 | +- Check out the code |
| 46 | +- Build the code using Maven (wrapper included) |
| 47 | +- Run tests |
| 48 | +- Analyze code quality using SonarQube |
| 49 | +- Archive the JAR file as an artifact for later use |
| 50 | + |
| 51 | +### Continuous Deployment |
| 52 | +The second part of the pipeline (the deploy job) runs after the build job if it’s successful |
| 53 | +- Download the JAR artifact from the previous build |
| 54 | +- Securely copy (scp) the new JAR to /home/ubuntu on the EC2 instance |
| 55 | +- Ensure Java is installed (```sudo apt update && sudo apt install -y openjdk-17-jre```) |
| 56 | +- SSH into the EC2 machine, killing any process that might already be running on port 8080 |
| 57 | +- Start Spring PetClinic in the background using ```nohup``` |
| 58 | + |
| 59 | +**Important Notes**: Make sure your EC2 instance’s security group allows inbound traffic on port 8080 so that you can reach the PetClinic app in a browser. </br> |
| 60 | +In your GitHub repository settings, under “Secrets and variables” → “Actions”, add: </br> |
| 61 | +SONAR_HOST_URL, SONAR_TOKEN, EC2_HOST, EC2_PRIVATE_KEY |
| 62 | + |
| 63 | +### Verification |
| 64 | +After the pipeline finishes, visit: |
| 65 | + |
| 66 | +``` |
| 67 | +http://<EC2_PUBLIC_IP_OR_DNS>:8080 |
| 68 | +``` |
| 69 | +You should see the Spring PetClinic landing page. Additionally, log into your SonarQube server ```http://<sonarqube-EC2-instance-public-ip-or-host>:9000``` and verify that the Spring-PetClinic analysis is present. |
| 70 | + |
| 71 | +### Troubleshooting |
| 72 | +SSH into your EC2 instance manually: ```ssh -i <your-private-key> <user>@<IP adress>``` |
11 | 73 |
|
12 | | -You can then access the Petclinic at <http://localhost:8080/> |
| 74 | +1. Verify Java is installed and JAR file exists: ```java -version``` and ```ls -l /home/ubuntu/app.jar``` |
| 75 | +2. Run the nohup java -jar command manually: ```nohup java -jar /home/ubuntu/spring-petclinic-*.jar > app.log 2>&1 &``` |
| 76 | +3. Verify Application running: ```curl localhost:8080``` or ```ps aux | grep java``` |
| 77 | +4. If not, check the app.log file for any error messages: ```tail app.log``` |
0 commit comments