This course focuses on how to develop backend applications using Spring Boot and how to write GitHub Actions scripts to build a continuous integration (CI) pipeline. By the end of this article, you will acquire the necessary skills to build web applications with Spring Boot and set up a CI/CD process.
1. What is Spring Boot?
Spring Boot is a framework that helps you develop applications based on the Spring framework quickly and easily. It provides auto-configuration, embedded servers, and easy deployment features, allowing developers to add necessary functionalities conveniently.
- Auto-configuration: Spring Boot automatically configures the application’s settings, enabling developers to start projects with minimal configuration.
- Embedded servers: It includes servers like Tomcat, Jetty, and Undertow, allowing applications to run without separate server setup.
- Modularity: Spring Boot is designed to offer various starter packs, making it easy to add necessary libraries.
2. Setting Up the Spring Boot Development Environment
Let’s take a look at the environmental setup steps necessary to start with Spring Boot. This includes installing the JDK, setting up the IDE, and creating your Spring Boot project.
2.1 Install JDK
You need the Java Development Kit (JDK) to develop with Spring Boot. Install JDK version 11 or higher. You can download the JDK from the [Oracle official website](https://www.oracle.com/java/technologies/javase-jdk11-downloads.html) or [OpenJDK](https://jdk.java.net/).
2.2 Install IDE
The most commonly used IDEs for Spring Boot development are IntelliJ IDEA and Eclipse. The Community Edition of IntelliJ IDEA is available for free and offers powerful features. Download and install [IntelliJ IDEA](https://www.jetbrains.com/idea/download/).
2.3 Create a Spring Boot Project
You can create a Spring Boot project using [Spring Initializr](https://start.spring.io/). Follow the steps below to create your project:
- Visit the site and input as follows:
- Project: Maven Project
- Language: Java
- Spring Boot: Select the latest stable version
- Group: com.example
- Artifact: demo
- Dependencies: Add Spring Web, Spring Data JPA, H2 Database
- Click the “Generate” button to download the project.
- Open the downloaded project in your IDE and run the build.
3. Creating a Simple REST API
Now, let’s create a simple REST API. We will implement basic CRUD (Create, Read, Update, Delete) functionalities.
3.1 Writing the Entity Class
First, we write the entity class that will be mapped to the database table. The class to be used is as follows:
package com.example.demo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private double price;
// Getters and setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
3.2 Writing the Repository
Next, we write a JPA repository to interact with the database:
package com.example.demo.repository;
import com.example.demo.model.Item;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ItemRepository extends JpaRepository<Item, Long> {
}
3.3 Writing the Service Class
We will now add a service class to handle the business logic:
package com.example.demo.service;
import com.example.demo.model.Item;
import com.example.demo.repository.ItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ItemService {
@Autowired
private ItemRepository itemRepository;
public List- findAll() {
return itemRepository.findAll();
}
public Item save(Item item) {
return itemRepository.save(item);
}
public Item update(Long id, Item item) {
item.setId(id);
return itemRepository.save(item);
}
public void delete(Long id) {
itemRepository.deleteById(id);
}
}
3.4 Writing the Controller
Finally, we create a controller to provide REST API endpoints:
package com.example.demo.controller;
import com.example.demo.model.Item;
import com.example.demo.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/items")
public class ItemController {
@Autowired
private ItemService itemService;
@GetMapping
public List<Item> getAllItems() {
return itemService.findAll();
}
@PostMapping
public Item createItem(@RequestBody Item item) {
return itemService.save(item);
}
@PutMapping("/{id}")
public Item updateItem(@PathVariable Long id, @RequestBody Item item) {
return itemService.update(id, item);
}
@DeleteMapping("/{id}")
public void deleteItem(@PathVariable Long id) {
itemService.delete(id);
}
}
4. Introduction to GitHub Actions
GitHub Actions is a tool that automates CI/CD (Continuous Integration and Continuous Delivery) tasks in software development. It allows developers to set up automated execution of build, test, and deployment processes.
4.1 Reasons to Use GitHub Actions
- Automated CI/CD Setup: Build and deployment can occur automatically with every code change.
- Enhanced Collaboration: Multiple team members can work simultaneously with minimal conflicts.
- No Location Constraints: Operates on cloud infrastructure, so no server setup is required.
5. Setting Up GitHub Actions
Now, let’s create a GitHub Actions workflow file for the Spring Boot application.
5.1 Creating a GitHub Repository
First, create a new repository on GitHub and commit your Spring Boot project there.
5.2 Creating the Workflow File
Create a folder named “.github/workflows” in the project root directory and create a file named “ci.yml” inside that folder.
name: CI
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
- name: Build with Maven
run: mvn clean install
- name: Run tests
run: mvn test
The above code runs GitHub Actions every time code is pushed to the `main` branch. Each step performs the following tasks:
- Checks out the code.
- Sets up JDK 11.
- Builds the project using Maven.
- Runs tests.
5.3 Confirming GitHub Actions Execution
After pushing the code, you can check the ‘Actions’ tab in GitHub to see if the workflow runs successfully. If any issues occur, you can check the logs to identify and fix errors.
6. Continuous Deployment (CD)
In this section, we will look at additional configuration for deployment automation. This includes using AWS, Heroku, or other cloud hosting services for deployment.
6.1 Deploying to AWS EC2
You can deploy your application by creating an AWS EC2 instance. Here’s a simple setup method:
- Log in to AWS and create an instance from the EC2 dashboard.
- Set up a security group to allow port 8080.
- SSH into the instance to install JDK and Maven.
- Copy and run the application:
java -jar yourapp.jar
6.2 Deploying to Heroku
Heroku is a platform that allows for easy and quick deployment of applications. You can deploy your application using the Heroku CLI:
- Install the Heroku CLI and log in.
- Create an application with the following command:
- Push your code to deploy:
heroku create your-app-name
git push heroku main
7. Conclusion
This course covered backend development using Spring Boot and how to automate CI/CD setup with GitHub Actions. By combining the powerful features of Spring Boot with GitHub Actions, you can enhance your development efficiency. Through continuous learning and practice, I hope you can grow into a more advanced application developer.