Bootstrap Your Spring Boot Application With GitHub Actions
Motivation
Creating a Spring Boot process on GitHub is a tedious task. It involves the manual task of creating a GitHub repository, ensure Java, Spring Boot CLI or Maven CLI are installed, generate the Spring Boot project via command line, configure the project settings, clone the repository, and then push it back to the remote repository. We will show you how to automate these steps and create a Spring Boot project with just a single click.
Creating Spring Boot Project with Github Actions
You can fork or clone this repository https://github.com/yilengyao/github-actions-workflow
or
Create your own repository and
Create a .github/workflows folder. In that folder add the file create-spring-boot-repository.yaml.
name: Create Spring Boot Repository
on:
workflow_dispatch:
inputs:
name:
description: 'Repository Name'
required: true
description:
description: 'Repository Description'
required: true
private:
description: 'Private Repository'
required: true
default: 'false'
groupId:
description: 'Maven Group ID'
required: true
artifactId:
description: 'Maven Artifact ID'
required: true
springBootVersion:
description: 'Spring Boot Version'
required: true
default: '3.0.0'
mainClassName:
description: 'Main Class Name'
required: true
default: 'Application'
userName:
description: 'User Name'
required: true
userEmail:
description: 'User Email'
required: true
jobs:
create-spring-boot-repository:
runs-on: ubuntu-latest
steps:
- name: Step 1 - Checkout code
uses: actions/checkout@v3
- name: Step 2 - Generate Spring Boot Project with Maven Wrapper
run: |
curl https://start.spring.io/starter.zip -o project.zip \
-d type=maven-project \
-d bootVersion=${{ github.event.inputs.springBootVersion }} \
-d groupId=${{ github.event.inputs.groupId }} \
-d artifactId=${{ github.event.inputs.artifactId }} \
-d name=${{ github.event.inputs.mainClassName }}
unzip project.zip -d spring-boot-project
cd spring-boot-project
mvn wrapper:wrapper
- name: Step 3 - Create new GitHub repository
run: |
curl -X POST -H "Authorization: token ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}" \
-d '{
"name": "${{ github.event.inputs.name }}",
"description": "${{ github.event.inputs.description }}",
"private": ${{ github.event.inputs.private }}
}' \
https://api.github.com/user/repos
- name: Step 4 - Push to new repository
run: |
cd spring-boot-project
echo "# ${{ github.event.inputs.name }}" >> README.md
git config --global user.email "${{ github.event.inputs.userEmail }}"
git config --global user.name "${{ github.event.inputs.userName }}"
git init
git add .
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin [email protected]:${{ github.actor }}/${{ github.event.inputs.name }}.git
git remote set-url origin https://x-access-token:${{ env.GH_PERSONAL_ACCESS_TOKEN }}@github.com/${{ github.actor }}/${{ github.event.inputs.name }}
git push -u origin main # Push to 'main' branch
env:
GH_PERSONAL_ACCESS_TOKEN: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
Setting up GitHub Personal Access Token
Our GitHub Action workflow needs to call the GitHub API to create a GitHub repository and push code to the repository. In order to call the GitHub API it needs the GitHub Personal Access Token.
Click on your GitHub Profile (on the upper right corner), then select settings on the drop down menu, then select Developer settings on the lower right corner. Now you are on the Developers Setting page, select Fine-grained tokens then click on Generate a personal access token .
Provide a Token name , set an Expiration date, for Repository access select All Repositories.
For Permissions you need to set Read and Write access for
Please save you GitHub Personal Access Token.
Setting up Github Secrets
On your GitHub repository go to Settings then Secrets and variables , then click Actions
Generating Spring Boot Object with GitHub Actions Workflow
In your GitHub repository containing the GitHub Action, click on the Actions tab, then click on Clock on Run workflow , then fill in all the fields and then click Run workflow .
Congrats, your Spring Boot application has been created.
Here is the related GitHub repository for reference:
GitHub - yilengyao/github-actions-workflow: Workflows Automation using Github Actions
Workflows Automation using Github Actions. Contribute to yilengyao/github-actions-workflow development by creating an…
领英推荐
Happy Developing! ??.
(Optional) Manually Creating Spring Boot Project
Add MVNW
mvnw (Maven Wrapper) is a tool that allows you to run Maven projects without having Maven installed and present on the path. It automatically downloads the correct version of Maven for the project.
add mvnw to the project.
mvn wrapper:wrapper
Create maven project with maven cli
./mvnw archetype:generate \
-DgroupId=<group-id> \
-DartifactId=<project-name> \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
In youre pom.xml add
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
</parent>
<properties>
<java.version>17</java.version>
<maven.compiler.version>3.8.0</maven.compiler.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
The spring-boot-starter-web dependency
A “starter” dependency is to bootstrap your Spring Boot application with predefined set of dependencies to Maven configuration, prevent potential versions conflicts, and provida a consistent set of pompatible libraries.
Eg. spring-boot-starter-web bundles dependencies for web development, including an embedded server, Spring MVC, and Jackson for JSON.
“starter” dependencies previent potential conflicts by
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
The spring-boot-maven-plugin
you don’t need to explicitly set the version for spring-boot-starter-web or the spring-boot-maven-plugin when you’re using the spring-boot-starter-parent as the parent POM for your project. The spring-boot-starter-parent provides dependency management for these, which means it specifies default versions for these artifacts.
Create a Basic Application:
Inside src/main/java, create a package, e.g., com.example.myapp. Inside the package, create a new class named Application.java. Add the following content:
package com.example.myapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class <Project-Name> {
public static void main(String[] args) {
SpringApplication.run(<Project-Name>.class, args);
}
};
To start the Spring Boot Application
./mvnw spring-boot:run