iTranslated by AI
For Java Developers: Implementing CI Static Analysis with GitHub Actions, Gradle, and SpotBugs
Introduction
- I will not provide a detailed explanation of GitHub Actions.
- The explanation will focus on Gradle. I will not cover Maven.
- I'll share some of my mental models as well. If you spot any errors, I'd appreciate it if you could point them out so I can correct both the article and my understanding.
Visualizing GitHub Actions

Component Descriptions
Workflow
Defined by YAML files in the directory and contains a series of jobs. This is the largest framework in GitHub Actions.
Event
Defines when the CI is executed. You can set various triggers such as PR, commit, push, or clone.
Job
A group of independent tasks defined within a workflow, each of which can be executed in different runners (environments).
Runner
Refers to the server environment where GitHub Actions jobs are executed.
Step
Individual tasks or actions executed within each job. This includes running your own scripts or using specific actions. This is the place where you specifically command what to execute.
Specific Code
name: Java CI with SpotBugs
on: [ push, pull_request ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: My first step - check out repository
uses: actions/checkout@v4
- name: List files
run: ls -R
- name: Set up Java 17
uses: actions/setup-java@v3
with:
distribution: "temurin"
java-version: "17"
cache: "gradle"
- name: Generate gradle wrapper
run: gradle wrapper --gradle-version=7.6
- name: Execute SpotBugs
run: ./gradlew spotbugsMain
I will explain it based on the previous descriptions.
Workflow
This entire file constitutes a single workflow.
Event
The on: [push, pull_request] part acts as the trigger. This CI is executed upon push or PR.
Job
Tasks are organized under jobs:.
Runner
runs-on: ubuntu-latest: In this case, the latest version of Ubuntu is used as the execution environment.
Step
Everything after steps: defines what is actually performed.
The part I want you to pay attention to is the steps section.
It may look like a special syntax like uses: actions/checkout@v4, but this is using functionality that GitHub or other third parties have already prepared for the actual execution. Many other actions are already available for GitHub Actions, and since you mostly just combine them, the structure is surprisingly simple.
There are also things like with: below it, but these just pass specified arguments to customize the action, so it shouldn't be too difficult unless you're doing something extremely advanced.
Also, the name fields here are what you'll see when checking the CI on GitHub, so it's best to use descriptive names.
How to Configure SpotBugs
GitHub Actions provides official or third-party actions for many tools and libraries, but there may not be a dedicated action for SpotBugs. If you want to integrate SpotBugs into a GitHub Actions workflow, you need to follow these steps:
-
Integrating SpotBugs into the build tool: First, add the SpotBugs plugin to the build tool you are using (e.g., Gradle or Maven). This allows static code analysis to be performed automatically during the build process.
-
Invoking SpotBugs from GitHub Actions: Next, set up a step in the GitHub Actions workflow file to execute the build tool and call the SpotBugs task within it. This executes the analysis during the GitHub Actions CI process and reports any issues found.
Specifically, when using Gradle, you would add the SpotBugs plugin to your build.gradle file and include a command like ./gradlew spotbugsMain to execute the SpotBugs task within the GitHub Actions workflow file.
Integrating SpotBugs into the Build Tool
Gradle Build Configuration Files
Gradle build configuration files can be written in two languages: Kotlin and Groovy. This varies by project, and when creating a new project, you select which language to use. If your project contains a build.gradle file, it is written in Groovy; if it has a build.gradle.kts file, it is written in Kotlin. Gradle's official documentation provides both Kotlin and Groovy DSLs (Domain Specific Languages), so please refer to the documentation for the language you are using.
Integrating the SpotBugs Plugin into Gradle
Let's actually configure the build.gradle file. While following the URLs above should work fine, pay attention to the syntax. Modern Gradle recommends the DSL notation. Simply adding the following completes the plugin configuration. You can change the version as you like.
plugins {
id 'java'
id "com.github.spotbugs" version "6.0.8"
}
Using SpotBugs in CI
- name: Run SpotBugs
run: |
./gradlew spotbugsMain
The Gradle command spotbugsMain is a task automatically generated when the SpotBugs plugin is added. The spotbugsMain task performs static code analysis on the main source set (usually src/main/java), while the spotbugsTest task runs it on the test source set (usually src/test/java).
After pushing with these settings, the CI will start running. For more details, please refer to the documentation.
Running SpotBugs during Build with Gradle
- name: Execute Build and SpotBugs
run: ./gradlew
To run SpotBugs during the build, you need to change the command.
For Multi-Project Setups
subprojects {
apply plugin: 'java'
apply plugin: 'com.github.spotbugs'
spotbugs {
toolVersion = '4.8.3'
}
}
In Java project management, you might manage multiple projects such as app and api with a server directory as the root. In this case, you need to do a little extra work in your build.gradle.
One thing to be careful about is toolVersion = '4.8.3'. This is the version of SpotBugs itself, not the Gradle plugin version. Feel free to configure the properties within spotbugs as you like.
Discussion