Spring Boot Backend Development Course, Writing GitHub Action Scripts, CD

Writing GitHub Action Scripts: Continuous Deployment (CD)

In modern software development, Continuous Deployment (CD) and Continuous Integration (CI) are essential elements. Automating these processes can significantly enhance developer productivity, especially when developing backend applications using Spring Boot. In this tutorial, we will explore how to automatically deploy a Spring Boot application using GitHub Actions.

1. What is Spring Boot?

Spring Boot is an open-source framework that extends the Spring Framework, making it easier to develop standalone Spring applications. It provides several useful features that simplify and expedite the use of the existing Spring Framework. Notably, it reduces the burden of initial setup and supports rapid application execution through built-in servers (e.g., Tomcat, Jetty).

2. What is GitHub Actions?

GitHub Actions is a CI/CD tool provided by GitHub that detects changes in the code and allows tasks to be executed automatically. Users can define and manage workflows in YAML file format, which can be triggered by various events (push, issue creation, etc.). This allows for the automation of tasks such as testing, building, and deployment.

3. Prerequisites

  • You should have already developed or created a Spring Boot application.
  • You need to create a GitHub repository for the project.
  • A GitHub account is required to use GitHub Actions.

4. Creating a Spring Boot Application

To create a Spring Boot application, you can use Spring Initializr. Set the appropriate meta information and add necessary dependencies to download the project. For example, you can select web (application), JPA, and H2 database.

 
// Example Maven dependency (pom.xml)
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

5. Creating a GitHub Repository

Access GitHub and create a new repository. Set the repository name according to the project, and you may select the ‘Initialize this repository with a README’ option. Then, push your local project to the GitHub repository.


# Run in the local project directory
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/username/repository.git
git push -u origin main

6. Writing a GitHub Actions Workflow

Now you need to create a GitHub Actions workflow file. Create a YAML file under the `.github/workflows` directory. For example, create a file named `ci.yml` and write the following:


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'
          distribution: 'adopt'

      - name: Build with Maven
        run: mvn clean package

7. Writing the Deployment Workflow

After setting up the basic CI workflow, it is time to write the workflow for CD. Here, we will describe the process of deploying to AWS EC2 as an example. Create a new YAML file named `deploy.yml` and write the following:


name: Deploy to EC2

on:
  push:
    branches:
      - main

jobs:
  deploy:
    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'
          distribution: 'adopt'

      - name: Build with Maven
        run: mvn clean package

      - name: Deploy to EC2
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.EC2_HOST }}
          username: ${{ secrets.EC2_USER }}
          key: ${{ secrets.EC2_SSH_KEY }}
          port: 22
          source: target/*.jar
          target: /path/to/deploy/

The `appleboy/scp-action` used here helps send files to the EC2 instance using SSH. Each item can be stored in GitHub Secrets to enhance security.

8. Setting Up GitHub Secrets

You now need to set up secret variables used in the CI/CD workflow. Go to the ‘Settings’ -> ‘Secrets and variables’ -> ‘Actions’ of the GitHub repository page and add the secret variables to be hidden. For example:

  • EC2_HOST: IP address of the EC2 instance
  • EC2_USER: SSH username
  • EC2_SSH_KEY: SSH private key

9. Checking GitHub Actions Execution

Once all configurations are complete, commit and push to the main branch of the repository. This will automatically trigger GitHub Actions, executing the configured CI/CD process. You can check the execution logs in the ‘Actions’ tab to verify the success of each step.

10. Conclusion

In this tutorial, we learned how to automatically deploy a Spring Boot backend application using GitHub Actions. GitHub Actions allows you to set up CI/CD processes with just a simple YAML file, making it a powerful tool for automating various tasks. This can enhance developer productivity and improve the reliability of deployments. Additionally, you might consider extensions such as integrating with other cloud providers or deploying with Docker.

References