Building Spring Boot Jar and Releasing on GitHub
Automating the process of packaging and releasing your Spring Boot application can save time and ensure consistency. GitHub Actions makes it simple to create a Continuous Integration/Continuous Deployment (CI/CD) workflow for building a JAR file with Maven and publishing it to GitHub Releases. This enables developers to systematically version and distribute their Spring Boot application artifacts.
This guide will walk you through:
- Setting up a Maven package workflow for a Spring Boot project.
- Using the
actions/upload-release-asset
action to publish the JAR file to GitHub Releases. - Triggering the workflow automatically with version tags.
By the end, you’ll have a clear and actionable process for automating your JAR release workflow.
Why Use GitHub Releases for Your Spring Boot Application?
GitHub Releases provide a structured way to host and share versioned binaries or files, such as JARs, directly in your repository. Here are some key benefits:
- Versioning Made Simple: GitHub Releases link artifacts to specific repository tags.
- Accessible Distributions: Users and tools can easily download your release binaries.
- Automation at Its Best: GitHub Actions can automatically build and upload artifacts using triggers like version tags.
For Spring Boot developers working with Maven, this setup simplifies the distribution of application binaries to users or deployment pipelines.
Step 1: Create a Maven Package Workflow
To automate JAR creation, you first need to configure GitHub Actions. Maven will be used to build your Spring Boot project and package the application as a JAR.
Setting Up the Workflow Directory
- Create the
.github/workflows
directory at the root of your repository:mkdir -p .github/workflows
- Add a workflow YAML file (e.g.,
release.yml
):touch .github/workflows/release.yml
Define the Maven Build Workflow
Here’s an example workflow to build and package a JAR file:
name: Build and Release Spring Boot JAR
on:
push:
tags:
- 'v*' # Trigger on version tags like v1.0.0
jobs:
build:
runs-on: ubuntu-latest
steps:
# Step 1 - Check out the repository
- name: Checkout code
uses: actions/checkout@v3
# Step 2 - Set up Java
- name: Set up Java 17
uses: actions/setup-java@v3
with:
java-version: 17
distribution: temurin
cache: maven
# Step 3 - Build the Spring Boot JAR
- name: Build JAR with Maven
run: mvn clean package
How It Works:
- Trigger: The workflow runs when a Git tag matching the pattern
v*
(e.g.,v1.0.0
) is pushed. - Java Setup: The
actions/setup-java
action sets up Java 17, which is commonly used for modern Spring Boot development. - Maven Build: The
mvn clean package
command compiles the application and creates the JAR in thetarget
directory.
This workflow packages your Spring Boot app, preparing it for release.
Step 2: Publish the JAR to GitHub Releases
After building the Spring Boot application, you’ll want to upload the generated JAR file to GitHub Releases. GitHub Actions provides the actions/upload-release-asset
action to streamline this process.
Add Upload Steps to the Workflow
Update your release.yml
workflow to include steps for creating and uploading a release:
release:
needs: build
runs-on: ubuntu-latest
steps:
# Step 1 - Check out the code
- name: Checkout code
uses: actions/checkout@v3
# Step 2 - Get the tag name
- name: Extract tag
id: vars
run: echo "RELEASE_TAG=${GITHUB_REF##*/}" >> $GITHUB_ENV
# Step 3 - Create a GitHub Release
- name: Create GitHub Release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ env.RELEASE_TAG }}
release_name: ${{ env.RELEASE_TAG }}
draft: false
prerelease: false
# Step 4 - Upload Release Asset
- name: Upload JAR to GitHub Release
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create-release.outputs.upload_url }}
asset_path: target/*.jar
asset_name: spring-boot-app-${{ env.RELEASE_TAG }}.jar
asset_content_type: application/java-archive
Workflow Breakdown
- Extract Tag Name: The tag name is extracted from the
GITHUB_REF
environment variable and used to create a release. - Create GitHub Release: The
actions/create-release
action creates a new release for the tag. - Upload Release Asset: The
actions/upload-release-asset
action uploads the generated JAR file to the release.
Manage Output JAR Location
Ensure your pom.xml
includes the Spring Boot Maven Plugin so the build command generates the JAR correctly:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
</plugins>
</build>
The JAR will be located in the target
directory by default.
Step 3: Trigger the Workflow on Version Tags
Workflows can be triggered by specific events in your GitHub repository. For version-controlled releases, you can configure the workflow to activate when a new tag is pushed.
Creating Version Tags
To trigger the workflow, you need to create tags that follow a versioning convention (e.g., v1.0.0
).
- Tag your commit:
git tag v1.0.0
- Push the tag to GitHub:
git push origin v1.0.0
The workflow will trigger automatically, build the JAR, and upload the artifact to GitHub Releases.
Why Use Tags?
Tags are ideal for versioned releases because they:
- Precisely mark a snapshot of your codebase.
- Allow users to download and use specific versions of your application.
Full CI/CD Workflow Example
Below is the complete workflow for building and releasing a Spring Boot JAR to GitHub Releases:
name: Build and Release Spring Boot JAR
on:
push:
tags:
- 'v*' # Trigger on version tags
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Java 17
uses: actions/setup-java@v3
with:
java-version: 17
distribution: temurin
cache: maven
- name: Build JAR with Maven
run: mvn clean package
release:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Extract tag
id: vars
run: echo "RELEASE_TAG=${GITHUB_REF##*/}" >> $GITHUB_ENV
- name: Create GitHub Release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ env.RELEASE_TAG }}
release_name: ${{ env.RELEASE_TAG }}
draft: false
prerelease: false
- name: Upload JAR to GitHub Release
uses: actions/upload-release-asset@v1
with:
upload_url: ${{ steps.create-release.outputs.upload_url }}
asset_path: target/*.jar
asset_name: spring-boot-app-${{ env.RELEASE_TAG }}.jar
asset_content_type: application/java-archive
Final Thoughts
By building a Maven package workflow and releasing your Spring Boot JAR file to GitHub, you streamline the process of versioning and distributing your application. Using GitHub Actions, you can automate JAR creation, integrate secure workflows with actions/upload-release-asset
, and simplify the entire release process with version tags.
Start implementing these steps today to enhance your CI/CD pipeline and ensure your Spring Boot application is ready for deployment with every release!