Skip to content

Commit f760eaf

Browse files
authored
Build and Deploy a Spring Boot Application using GitHub Actions (#469)
* 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
1 parent 290723d commit f760eaf

File tree

26 files changed

+857
-0
lines changed

26 files changed

+857
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
# Deploy a Spring Boot Application using Github Actions
3+
4+
## Prerequisites
5+
6+
This example assumes you have previously completed the following examples:
7+
8+
1. [Create an Azure Resource Group](../../group/create/README.md)
9+
1. [Create an Azure App Service Plan](../create-plan/README.md)
10+
11+
After that, follow the below steps
12+
13+
1. Click on Resource groups as in the below image
14+
15+
Resource group - It is a container that holds the resources for any Azure solution and is a prerequisite for creating anything on Azure.
16+
![Resource Group](images/ResourceGroup.png)
17+
18+
Click on create and provide a resource-group and then it would be created.
19+
2. Now, click on App Services -> Create -> Web App
20+
21+
1. Provide the web app name, java version (17/21), operating system (Windows/Linux), region (US/India) etc.
22+
23+
2. Then click on "Review+Create" at the bottom.
24+
25+
This is how the newly created app service would look like
26+
27+
![App Service](images/AppService.png)
28+
29+
30+
3. Create a Spring Boot application with help of https://start.spring.io
31+
32+
33+
4. Then create a rest controller like below
34+
35+
```
36+
@RestController
37+
public class SpringAzureApplication {
38+
39+
@GetMapping("/message")
40+
public String message(){
41+
return "Azure deployment is working";
42+
}
43+
}
44+
```
45+
5. Execute the below git commands and push local code to github branch
46+
```
47+
git remote add origin <repository_url> (repository url indicates the newly created github repository)
48+
49+
git add . (to add all the changes done in local)
50+
51+
git commit -m "First commit" (does the commit with a message)
52+
53+
git push -u origin master (push all the changes to github repository)
54+
```
55+
6. Click on Deployment centre
56+
![Deployment Centre](images/DeploymentCentre.png)
57+
58+
59+
- In the source dropdown, select Github and Authorise which would ask for the username and password.
60+
61+
- After that, select the organization, repository, branch as well in the dropdown.
62+
63+
- You can click on the "Preview file" to see what steps would be performed as part of the build and deploy process
64+
65+
![Preview File](images/PreviewFile.png)
66+
67+
7. Click on save
68+
69+
70+
8. The build will start & complete after which the deployment will start and succeed.
71+
72+
73+
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.
74+
75+
- Upon successful hit of the URL, should get a message like "Azure deployment is working" which was given in the GetMapping method.
Loading
Loading
Loading
Loading
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.2.3</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.learning</groupId>
12+
<artifactId>SpringBoot</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>SpringBoot</name>
15+
<description>Demo project for Spring Boot</description>
16+
<properties>
17+
<java.version>17</java.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-web</artifactId>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-test</artifactId>
28+
<scope>test</scope>
29+
</dependency>
30+
</dependencies>
31+
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-maven-plugin</artifactId>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
41+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.product;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ProductApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(ProductApplication.class, args);
11+
}
12+
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.product;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
@RestController
7+
public class ProductController {
8+
9+
@GetMapping("/message")
10+
public String message() {
11+
return "Azure deployment is working";
12+
}
13+
14+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# Setting Up Azure Spring Cloud Config Server for Centralized Configuration in Spring Boot
2+
3+
## Prerequisites
4+
5+
This example assumes you have previously completed the following examples:
6+
7+
[Create an Azure Resource Group](https://github.com/Azure-Samples/java-on-azure-examples/blob/main/group/create/README.md)
8+
[Create an Azure Spring Apps environment](https://github.com/Azure-Samples/java-on-azure-examples/blob/main/spring/create/README.md)
9+
10+
## Create an Azure Spring Cloud Config Server
11+
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.
12+
13+
1. Create Configuration in a <b>Git Repository</b>
14+
15+
* Create a Git repository (e.g., spring-config-repo) to store configurations.
16+
* Add application.yml for shared configuration and application-dev.yml for environment-specific values.
17+
18+
## Create a Spring Boot Application
19+
20+
1. On the other side, create a Spring Boot application with help of https://start.spring.io
21+
22+
2. Open the project in the IDE and then create the below classes
23+
24+
i. An application yaml file
25+
26+
```
27+
spring:
28+
application:
29+
name: customer-service
30+
datasource:
31+
url: jdbc:mysql://localhost:3306/customerdb
32+
username: ${DB_USER}
33+
password: ${DB_PASS}`
34+
```
35+
36+
ii. Application dev yaml file
37+
spring:
38+
datasource:
39+
url: jdbc:mysql://dev-db-server:3306/customerdb
40+
username: ${DB_USER_DEV}
41+
password: ${DB_PASS_DEV}
42+
cloud:
43+
config:
44+
uri: https://<YOUR_AZURE_CONFIG_SERVER_URL>
45+
label: main
46+
enabled: true
47+
48+
**Note: Sensitive data like database credentials should be securely stored in Azure Key Vault**
49+
50+
3. Add the below dependencies (pom.xml)
51+
52+
```
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-data-jpa</artifactId>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-starter-web</artifactId>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.springframework.cloud</groupId>
63+
<artifactId>spring-cloud-starter-config</artifactId>
64+
</dependency>
65+
<dependency>
66+
<groupId>com.microsoft.azure</groupId>
67+
<artifactId>azure-spring-cloud-dependencies</artifactId>
68+
<version>2.1.1</version>
69+
</dependency>`
70+
```
71+
72+
4. Configuration (application.properties in Spring Boot app)
73+
74+
```
75+
spring.application.name=customer-service
76+
spring.profiles.active=dev`
77+
```
78+
79+
5. Create Model, Repository, Service, and Controller
80+
81+
```
82+
@Entity
83+
public class Customer {
84+
@Id
85+
@GeneratedValue
86+
private Long id;
87+
private String name;
88+
private String email;
89+
// Getters and setters
90+
}
91+
```
92+
93+
```
94+
public interface CustomerRepository extends JpaRepository<Customer, Long> {
95+
}
96+
```
97+
98+
99+
```
100+
@Service
101+
public class CustomerService {
102+
@Autowired
103+
private CustomerRepository repository;
104+
105+
public List<Customer> getAllCustomers() {
106+
return repository.findAll();
107+
}
108+
}
109+
```
110+
111+
```
112+
@RestController
113+
@RequestMapping("/api/customers")
114+
public class CustomerController {
115+
@Autowired
116+
private CustomerService service;
117+
118+
@GetMapping
119+
public List<Customer> getCustomers() {
120+
return service.getAllCustomers();
121+
}
122+
}
123+
```
124+
125+
6. Configure Azure Spring Cloud with the Config Server
126+
127+
Set up the Config Server to use your Git repository as a configuration source:
128+
129+
```
130+
az spring-cloud config-server set \
131+
--name <SPRING_CLOUD_APP_NAME> \
132+
--config-file application-dev.yml \
133+
--resource-group <RESOURCE_GROUP_NAME>
134+
```
135+
7. Connect Spring Boot to Config Server
136+
137+
The Spring Boot application will use Azure Spring Cloud Config Server to retrieve configuration properties automatically.
138+
139+
## Testing and Deployment
140+
### Testing the Endpoint
141+
142+
After deploying the application, verify it with the following tools and methods:
143+
144+
1. Using cURL, run the following command to test the /api/customers endpoint:
145+
```
146+
curl -X GET http://<APP_URL>/api/customers
147+
```
148+
2. Using Insomnia/Postman
149+
* Open Insomnia or Postman and create a new GET request.
150+
* Set the URL to http://<APP_URL>/api/customers.
151+
* Send the request and check for a JSON response containing customer data.
152+
153+
3. Using Browser
154+
* Navigate to http://<APP_URL>/api/customers in a web browser. The JSON response should be displayed if the API is accessible.
155+
156+
4. Refreshing Configuration
157+
* 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:
158+
159+
```
160+
management.endpoints.web.exposure.include=refresh
161+
```
162+
163+
Trigger the refresh by calling the actuator endpoint:
164+
```
165+
curl -X POST http://<APP_URL>/actuator/refresh
166+
```
167+
This command reloads the configuration from the Config Server, applying any updates in the Git repository to the running application.
168+
169+
170+
Cleanup
171+
* To avoid ongoing charges, delete the resources once you are done:
172+
```
173+
az group delete --name <RESOURCE_GROUP_NAME>
174+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.example</groupId>
6+
<artifactId>spring-cloud-config-example</artifactId>
7+
<version>1.0.0</version>
8+
<packaging>jar</packaging>
9+
10+
<dependencies>
11+
<dependency>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-data-jpa</artifactId>
14+
</dependency>
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-web</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.springframework.cloud</groupId>
21+
<artifactId>spring-cloud-starter-config</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.microsoft.azure</groupId>
25+
<artifactId>azure-spring-cloud-dependencies</artifactId>
26+
<version>2.1.1</version>
27+
<scope>import</scope>
28+
<type>pom</type>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-actuator</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>com.h2database</groupId>
36+
<artifactId>h2</artifactId>
37+
<scope>runtime</scope>
38+
</dependency>
39+
</dependencies>
40+
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-maven-plugin</artifactId>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package customer;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class CustomerServiceApplication {
8+
public static void main(String[] args) {
9+
SpringApplication.run(CustomerServiceApplication.class, args);
10+
}
11+
}

0 commit comments

Comments
 (0)