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:

  1. classpath 'io.sealights.on-premise.agents.plugin:sealights-gradle-plugin:latest.release'

In the 'allprojects' section add the following:

  1. apply plugin: 'io.sealights.on-premise.agents.plugin.sealights-gradle-plugin'

  2. sealights section with the following parameters

    1. token or tokenFile - set with a token or a file containing the token obtained from the SeaLights dashboard

    2. If you create a Build Session ID externally via the config step, provide the following fields:

      1. buildSessionId or buildSessionIdFile - Set with a build session id or a file containing the build session id created by the config step

      2. createBuildSessionId - Set to false

    3. 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:

    1. filesInclude - Comma separated list of files to scan

    2. filesExcluded - Comma separated list of files to exclude from the scan (Supports wildcards)

    3. runTestOnly - Set to true if you want to only run tests without scanning builds

    4. filesStorage - location of temporary folder location to store cached agent files

    5. logEnabled - Set to true if you want a log to be created

    6. logLevel - Set the log level to create. For example WARN or INFO

    7. logToFile - Set to true if you want the log to written to a file

    8. logToConsole - Set to true if you want the log to written to the console

    9. includeResourcesSet to true if you want to include the token and build session ID files in the built resources

    10. proxy - Provide the URL to the proxy to route the calles through if needed

    11. 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

    12. 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

    13. 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

    14. 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

  • packagesincludedComma-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