Skip to content

Commit

Permalink
Build and Deploy a Spring Boot Application using GitHub Actions (#469)
Browse files Browse the repository at this point in the history
* Build and Deploy a Spring Boot Application using GitHub Actions

* Build a Spring Boot Application using Azure SQL DB and deploy into Azure

* Changed images to be relative

* Configure centralized Azure Spring Cloud Config Server

* Change Main class structure
  • Loading branch information
karthirm authored Nov 15, 2024
1 parent 290723d commit f760eaf
Show file tree
Hide file tree
Showing 26 changed files with 857 additions and 0 deletions.
75 changes: 75 additions & 0 deletions appservice/java-springboot-github-actions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

# Deploy a Spring Boot Application using Github Actions

## Prerequisites

This example assumes you have previously completed the following examples:

1. [Create an Azure Resource Group](../../group/create/README.md)
1. [Create an Azure App Service Plan](../create-plan/README.md)

After that, follow the below steps

1. Click on Resource groups as in the below image

Resource group - It is a container that holds the resources for any Azure solution and is a prerequisite for creating anything on Azure.
![Resource Group](images/ResourceGroup.png)

Click on create and provide a resource-group and then it would be created.
2. Now, click on App Services -> Create -> Web App

1. Provide the web app name, java version (17/21), operating system (Windows/Linux), region (US/India) etc.

2. Then click on "Review+Create" at the bottom.

This is how the newly created app service would look like

![App Service](images/AppService.png)


3. Create a Spring Boot application with help of https://start.spring.io


4. Then create a rest controller like below

```
@RestController
public class SpringAzureApplication {
@GetMapping("/message")
public String message(){
return "Azure deployment is working";
}
}
```
5. Execute the below git commands and push local code to github branch
```
git remote add origin <repository_url> (repository url indicates the newly created github repository)
git add . (to add all the changes done in local)
git commit -m "First commit" (does the commit with a message)
git push -u origin master (push all the changes to github repository)
```
6. Click on Deployment centre
![Deployment Centre](images/DeploymentCentre.png)


- In the source dropdown, select Github and Authorise which would ask for the username and password.

- After that, select the organization, repository, branch as well in the dropdown.

- You can click on the "Preview file" to see what steps would be performed as part of the build and deploy process

![Preview File](images/PreviewFile.png)

7. Click on save


8. The build will start & complete after which the deployment will start and succeed.


9. Users can test the application with the help of URL (will be present in Github Actions tab) which would look something like http://spring-azure-demo.azurewebsites.net and need to append "message" at the end because that was defined in the RestController.

- Upon successful hit of the URL, should get a message like "Azure deployment is working" which was given in the GetMapping method.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions appservice/java-springboot-github-actions/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.learning</groupId>
<artifactId>SpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBoot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.product;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProductApplication {

public static void main(String[] args) {
SpringApplication.run(ProductApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.product;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {

@GetMapping("/message")
public String message() {
return "Azure deployment is working";
}

}
174 changes: 174 additions & 0 deletions spring/spring-cloud-config-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# Setting Up Azure Spring Cloud Config Server for Centralized Configuration in Spring Boot

## Prerequisites

This example assumes you have previously completed the following examples:

[Create an Azure Resource Group](https://github.com/Azure-Samples/java-on-azure-examples/blob/main/group/create/README.md)
[Create an Azure Spring Apps environment](https://github.com/Azure-Samples/java-on-azure-examples/blob/main/spring/create/README.md)

## Create an Azure Spring Cloud Config Server
This guide demonstrates how to set up an Azure Spring Cloud Config Server for managing application configurations across multiple Spring Boot services. We'll use a Git repository for storing configuration files and build a simple Customer service with a Controller, Service, and Repository layer.

1. Create Configuration in a <b>Git Repository</b>

* Create a Git repository (e.g., spring-config-repo) to store configurations.
* Add application.yml for shared configuration and application-dev.yml for environment-specific values.

## Create a Spring Boot Application

1. On the other side, create a Spring Boot application with help of https://start.spring.io

2. Open the project in the IDE and then create the below classes

i. An application yaml file

```
spring:
application:
name: customer-service
datasource:
url: jdbc:mysql://localhost:3306/customerdb
username: ${DB_USER}
password: ${DB_PASS}`
```

ii. Application dev yaml file
spring:
datasource:
url: jdbc:mysql://dev-db-server:3306/customerdb
username: ${DB_USER_DEV}
password: ${DB_PASS_DEV}
cloud:
config:
uri: https://<YOUR_AZURE_CONFIG_SERVER_URL>
label: main
enabled: true

**Note: Sensitive data like database credentials should be securely stored in Azure Key Vault**

3. Add the below dependencies (pom.xml)

```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-spring-cloud-dependencies</artifactId>
<version>2.1.1</version>
</dependency>`
```

4. Configuration (application.properties in Spring Boot app)

```
spring.application.name=customer-service
spring.profiles.active=dev`
```

5. Create Model, Repository, Service, and Controller

```
@Entity
public class Customer {
@Id
@GeneratedValue
private Long id;
private String name;
private String email;
// Getters and setters
}
```

```
public interface CustomerRepository extends JpaRepository<Customer, Long> {
}
```


```
@Service
public class CustomerService {
@Autowired
private CustomerRepository repository;
public List<Customer> getAllCustomers() {
return repository.findAll();
}
}
```

```
@RestController
@RequestMapping("/api/customers")
public class CustomerController {
@Autowired
private CustomerService service;
@GetMapping
public List<Customer> getCustomers() {
return service.getAllCustomers();
}
}
```

6. Configure Azure Spring Cloud with the Config Server

Set up the Config Server to use your Git repository as a configuration source:

```
az spring-cloud config-server set \
--name <SPRING_CLOUD_APP_NAME> \
--config-file application-dev.yml \
--resource-group <RESOURCE_GROUP_NAME>
```
7. Connect Spring Boot to Config Server

The Spring Boot application will use Azure Spring Cloud Config Server to retrieve configuration properties automatically.

## Testing and Deployment
### Testing the Endpoint

After deploying the application, verify it with the following tools and methods:

1. Using cURL, run the following command to test the /api/customers endpoint:
```
curl -X GET http://<APP_URL>/api/customers
```
2. Using Insomnia/Postman
* Open Insomnia or Postman and create a new GET request.
* Set the URL to http://<APP_URL>/api/customers.
* Send the request and check for a JSON response containing customer data.

3. Using Browser
* Navigate to http://<APP_URL>/api/customers in a web browser. The JSON response should be displayed if the API is accessible.

4. Refreshing Configuration
* For real-time configuration updates, you can refresh the Spring Boot application properties without restarting the app. Enable the Spring Boot Actuator refresh endpoint in application.properties:

```
management.endpoints.web.exposure.include=refresh
```

Trigger the refresh by calling the actuator endpoint:
```
curl -X POST http://<APP_URL>/actuator/refresh
```
This command reloads the configuration from the Config Server, applying any updates in the Git repository to the running application.


Cleanup
* To avoid ongoing charges, delete the resources once you are done:
```
az group delete --name <RESOURCE_GROUP_NAME>
```
49 changes: 49 additions & 0 deletions spring/spring-cloud-config-server/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>spring-cloud-config-example</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-spring-cloud-dependencies</artifactId>
<version>2.1.1</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package customer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CustomerServiceApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerServiceApplication.class, args);
}
}
Loading

0 comments on commit f760eaf

Please sign in to comment.