Gradle - Scanning Builds and Tests using Sealights plugin
In order to use Sealights Gradle plugin, you’ll need to update your build.gradle file with relevant settings and enable its execution during the relevant Gradle projects.
For example, by adding the -Psealights property to your Gradle command.
Configuring Gradle plugin in build.gradle
In the 'repositories > dependencies' section add:
classpath 'io.sealights.on-premise.agents.plugin:sealights-gradle-plugin:latest.release'
In the 'allprojects' section add the following:
apply plugin: 'io.sealights.on-premise.agents.plugin.sealights-gradle-plugin'
sealights section with the following parameters
token or tokenFile - set with a token or a file containing the token obtained from the SeaLights dashboard
If you create a Build Session ID externally via the config step, provide the following fields:
buildSessionId or buildSessionIdFile - Set with a build session id or a file containing the build session id created by the config step
createBuildSessionId - Set to false
If you want to create the Build Session ID by including the config step as part of your Gradle lifecycle, please refer to the section Creating a buildSessionId below
The following optional parameters:filesInclude - Comma separated list of files to scan
filesExcluded - Comma separated list of files to exclude from the scan (Supports wildcards)
runTestOnly - Set to true if you want to only run tests without scanning builds
filesStorage - location of temporary folder location to store cached agent files
logEnabled - Set to true if you want a log to be created
logLevel - Set the log level to create. For example WARN or INFO
logToFile - Set to true if you want the log to written to a file
logToConsole - Set to true if you want the log to written to the console
includeResources - Set to true if you want to include the token and build session ID files in the built resources
proxy - Provide the URL to the proxy to route the calles through if needed
sealightsJvmParams - Containing a map of all the parameters you want to pass to all the SeaLights execution steps (build & test), Note: the key and values must always be strings
buildScannerParams - Containing a map of all the parameters you want to pass to all the SeaLights build execution step, Note: the key and values must always be strings
testListenerJvmParams - Containing a map of all the parameters you want to pass to all the SeaLights test execution step, Note: the key and values must always be strings
testTasksAndStages - A map of tasks and their test stage names to integrate SeaLights into the jvmargs in. Default are test, unitPlatformTest with test stage "Unit Tests" and integrationTest with test stage "Integration Tests"
For additional parameters values and informaiton, please refer to Java Command Reference - Installing test listener as Java Agent
In the following example, the steps needed are surrounded by an if condition which allows you to only use Sealights when you pass the -Psealights property when executing your gradle command.
Without this condition and property, Sealights integration will always be applied and used.
Sample gradle build file
buildscript {
repositories {
mavenCentral()
dependencies {
if(project.hasProperty('sealights')){
classpath 'io.sealights.on-premise.agents.plugin:sealights-gradle-plugin:latest.release'
}
}
}
}
allprojects { p ->
if(project.hasProperty('sealights')){
apply plugin: 'io.sealights.on-premise.agents.plugin.sealights-gradle-plugin'
sealights {
// token = System.getenv('SL_TOKEN')
tokenFile="sltoken.txt"
createBuildSessionId = false
buildSessionIdFile = "buildSessionId.txt"
// buildSessionId = System.getenv('SL_BUILD_SESSION_ID')
// labId = "myLabId"
filesStorage="/tmp"
includeResources=true
logEnabled = false
logLevel = "off"
logToFile = false
logToConsole = true
// runTestOnly=true
// testTasksAndStages=["test":"Unit Tests", "junitPlatformTest":"Unit Tests", "integrationTest":"Integration Tests"]
// filesIncluded="*sample*"
// filesExcluded="*sample*"
// proxy = "http://localhost:8888"
// sealightsJvmParams=["sl.param1":"true","sl.param2":"value"]
// buildScannerParams=["sl.param1":"true","sl.param2":"value"]
// testListenerJvmParams=["sl.param1":"true","sl.param2":"value"]
}
}
}
Creating a buildSessionId
Instead of creating the build session ID externally, you can create it directly in the gradle.build file
To do so, you add the following entries in the sealights section:
appName - Name of the application as you want to see it on the SeaLights dashboard
branch - Name of the branch as you want to see it on the SeaLights dashboard
build - Name of the build as you want to see it on the SeaLights dashboard
packagesincluded - Comma-separated list of packages to include in scan
Sample of buildSessionId configuration
sealights {
// ...
createBuildSessionId = true
appName = System.getenv('JOB_NAME')
branch = System.getenv('GIT_BRANCH')
build = System.getenv('BUILD_NUMBER')
//build = new java.text.SimpleDateFormat("yyyy.MM.dd-HH.mm").format(new Date())
packagesIncluded = "*com.example.*"
// packagesExcluded = "*com.sample.*"
}
Configuring SCM
SeaLights by default provides all links to the SCM for Github.
You can configure SeaLights to prepare the links for Bitbucket and Gitlab as well with the following parameters placed under the sealightsJvmParams section:
sl.scm.provider - set to github, Bitbucket or gitlab
sl.scm.baseUrl - When working with an on-premise installation of your SCM and access from the build machine is diffrent than the one accessed by the users, then you can provide the base URL to use
version - set to the version of the on-premise version you use
Sample of SCM configuration
sealightsJvmParams=["sl.scm.provider":"bitbucket","sl.scm.baseUrl":"https://my.bitbucket.com/projects/ABCD/repos/XYZ/browse/A1","sl.scm.version":"4.6"]
Pre-downloading the agents
The Gradle plugin downloads the recommended agent at the beginning of the run. If you want to pre-download them and provide them to the plugin, you can do so with the flags buildScannerJar & testListenerJar.
Sample of SCM configuration
The agents used here should be downloaded either with the latest agent or the recommended one as stated in Downloading the java agent files