Spring Boot Backend Development Course, 12.2 Using GitHub Actions

Hello! In this tutorial, we will learn about GitHub Actions, a method to automate CI/CD (Continuous Integration/Continuous Deployment) for Spring Boot applications. GitHub Actions is a CI/CD tool that is directly integrated into GitHub repositories, making it very useful for automating software development workflows. In this article, we will cover the basic concepts of GitHub Actions, how to set it up, and how to apply it to a Spring Boot project.

1. What is GitHub Actions?

GitHub Actions is a CI/CD platform provided by GitHub that allows you to automatically perform various tasks based on events in your project repository. When code is pushed or a Pull Request (PR) is created, it can automatically handle tasks such as running tests, creating builds, and deploying by executing a specific workflow.

1.1 Features of GitHub Actions

  • Flexibility: You can easily create custom workflows and actions, leveraging numerous open-source actions.
  • Version Control: Each action supports versioning, allowing you to maintain a consistent environment by using specific versions of actions.
  • Security: It offers various security tools and authentication features to ensure safety in production environments.

2. Getting Started

First, to use GitHub Actions, you need to create a repository on GitHub. If you already have a repository, you can simply add actions to that repository.

2.1 Create a GitHub Account

If you do not have a GitHub account, sign up here.

2.2 Create a New Repository

After logging in to GitHub, click the ‘+’ button in the top-right corner and select ‘New repository’. Set the repository name, description, visibility, etc., and click ‘Create repository’ to create a new repository.

3. Create a Simple Spring Boot Application

Now let’s develop a Spring Boot application. We will use Spring Initializr to generate the basic structure.

3.1 Using Spring Initializr

Visit the following link to set up your project: Spring Initializr. Choose the necessary dependencies and click the ‘Generate’ button to download a zip file. Now unzip this file and open it in your IDE.

3.2 Writing Basic Code

To create a simple REST API, write the following code.

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello, Spring Boot!";
    }
}

4. Setting Up GitHub Actions

Next, let’s set up GitHub Actions to add automated builds and test executions.

4.1 Create .github/workflows Directory

Create a .github/workflows directory in the project root. This is where the action definition files will be placed.

4.2 Create Workflow File

The workflow file is written in YAML format. Write the following code in a file named java_ci.yml.

name: Java CI

on:
  push:
    branches: [ main ]
  pull_request:
    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@v1
      with:
        java-version: '11'
        
    - name: Build with Gradle
      run: ./gradlew build

4.3 Explanation of the Workflow

In the above code, the on keywords followed by push and pull_request define that the workflow will execute when those events occur. The jobs section can define multiple tasks, each running in a specific environment. The steps section defines necessary scripts to be executed in each step.

5. Adding Tests

Now, let’s add test code to the Spring Boot application. We will add a simple unit test as follows.

@SpringBootTest
public class HelloControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void helloShouldReturnMessage() throws Exception {
        mockMvc.perform(get("/hello"))
               .andExpect(status().isOk())
               .andExpect(content().string("Hello, Spring Boot!"));
    }
}

5.1 Updating the Test Workflow

Add the command to execute the tests in the YAML file created above.

- name: Run tests
      run: ./gradlew test

6. Commit and Push

Commit all changes and push to the remote repository.

git add .
git commit -m "Add GitHub Actions workflow"
git push origin main

6.1 Check Workflow Execution

Go to the ‘Actions’ tab of your GitHub repository to check if the workflow has executed successfully. If the build completes successfully, all settings have been configured correctly.

Conclusion

In this tutorial, we learned how to easily set up CI/CD using GitHub Actions in a Spring Boot application. Automated workflows can keep the development and deployment process efficient and consistent. Continuously leverage GitHub Actions to improve the quality of your projects!

Additional Resources