Jenkins is configured with a number of useful build tools (as Global Tools)
Tool | purpose |
---|---|
SoapUI-5.5.0 | Open Source SoapUI froo running SOAP and REST based testing |
gradle-4.6 | Gradle 4.6 environment |
gradle-4.9 | Gradle 4.9 environment |
gradle-5.4.1 | Gradle 5.4.1 environment |
SonarScanner4 | SonarQube scanner (to be used with configured SonarQube server |
maven | maven 3.6.2 environment |
node-10.16.2 | NodeJS environment |
Taurus | Invoke test automation tools, i.e. JMeter |
openjdk-11.0.5 | JDK 11 environment |
There are a number of ways to use a Global Tool in a Jenkins declarative pipeline.
This is an example of running a static code scan using Sonar Scanner, using project configuration in SonarQube.
// run a SonarQube scan task stage ('SonarQube Analysis') { // this is the pre-defined SonarScanner CLI configured in Jenkins environment { scannerHome = tool 'SonarScanner4' } steps { // add the node.js tool - sonar-scanner wants to run node (this is the pre-configured tool in Jenkins) nodejs('node-10.16.2') { // This is the pre-defined SonarQube server configured in Jenkins - we pull project info from SonarQube and push results back to SonarQube withSonarQubeEnv('SonarQube') { // run the scan using the default sonar-project.properties file sh "${scannerHome}/bin/sonar-scanner" } // end withSonarQubeEnv } // end nodejs } //end steps } // end stage |
Notes:
Line 4: Add the SonarScanner CLI to the pipeline. This will install the Sonar Scanner CLI into the currently selected agent.
Line 11: This will also install the NodeJS environment. Note that the nodejs pipeline plugin is installed allowing the use of the nodejs{} closure.
Line 13: When a Sonar scan is run server side configuration will be pulled from the configured SonarQube server (using Sonar Scanner properties in your code).
Line 15: This will initiate a SonarQube code scan. Results will be posted to your project in the SonarQube server and summary information is retained by Jenkins for each job.
A number of properties control the behavior of a SonarQube scan. The properties can be specified in a file (sonar-project.properties in your project's root) and the Jenkins plugin will use these properties when issuing the scan. Below is a sample properties file:
sonar.projectKey=<SONARQUBE_PROJECT_KEY> sonar.scm.disabled=true sonar.sources=. sonar.sourceEncoding=UTF-8 sonar.java.binaries=**/build/classes/java/main sonar.analysis.mode=publish |
Additional information on the properties that can be set with this version of SonarQube can be found here. Note that without the properties the SonarQube scan will fail.
The maven agent can include a custome settings.xml file which would specify alternate SNAPSHOT and release maven repositories. You can also provide credentials for the repositories.
The first step to using a custom settings.xml file is to use the Config File Porvider to track your settings.xml file. Navigate to your folder and choose the Config Files option:
Next choose Add a New Config:
Choose Maven settings.xml:
This will present you with a page where you can add a setings.xml file. You can also include credentials when this settings.xml file is loaded (these credentials should already exist and would typically contain your project service account):
This code snippet demonstrates the use of the Maven plugin and your custom settings.xml file
withMaven(jdk: 'openjdk-11.0.5', maven: 'maven', mavenSettingsConfig: '99ee6674-2c2e-4daa-83a8-7399a1089604') { sh 'mvn clean compile' } |
An alternate method of using the settings.xml file can be performed by loading the maven global tool and the Config File provider:
// install Maven and add it to the path env.PATH = "${tool 'maven'}/bin:${env.PATH}" configFileProvider( [configFile(fileId: '99ee6674-2c2e-4daa-83a8-7399a1089604', variable: 'MAVEN_SETTINGS')]) { sh 'mvn -s $MAVEN_SETTINGS clean compile' } |
This code snippet will install and run the maven Global Tool:
pipeline { agent { node { label 'SOME_AGENT_TYPE' } // end node } // end agent stages { stage ('Run mvn build'){ environment{ mvnHome = tool 'maven' } // end environment steps { sh '${mvnHome}/bin/mvn <PARAMETERS>' } // end steps } // end stage } // end stages } // end pipeline |
This code snippet uses the Global Tools Open JDK 11 and Maven in the xl-builder agent to build java projects.
pipeline { agent { node { // use the xl-builder pre-defined agent (more RAM/CPU for builds) label 'xl-builder' } // end node } //end agent stages { ... stage ('Maven build') { withEnv(["JAVA_HOME=${ tool 'openjdk-11.0.5' }", "PATH+MAVEN=${tool 'maven'}/bin:${env.JAVA_HOME}/bin"]) { steps{ // Apache Maven related side notes: // --batch-mode : recommended in CI to inform maven to not run in interactive mode (less logs) // -V : strongly recommended in CI, will display the JDK and Maven versions in use. // Very useful to be quickly sure the selected versions were the ones you think. // -U : force maven to update snapshots each time (default : once an hour, makes no sense in CI). // -Dsurefire.useFile=false : useful in CI. Displays test errors in the logs directly (instead of // having to crawl the workspace files to see the cause). sh "mvn --batch-mode -V -U -e clean deploy -Dsurefire.useFile=false" } //end steps } // end withEnv } // end stage } //end stages |
This snippet invokes another pre-configured Global Tool, SoapUI:
pipeline { agent { node { label 'SOME_AGENT_TYPE' } // end node } // end agent stages { stage ('Run SoapUI Tests'){ environment{ soapuiHome = tool 'SoapUI-5.5.0' } // end environment steps { sh '${soapuiHome}/bin/testrunner.sh <PARAMETERS>' } // end steps } // end stage } // end stages } // end pipeline |
Notes:
Line 4: Since this is a global tool that Jenkins will install on-the-fly, you can choose an agent type that fits your build. Substitute your selection for SOME_AGENT_TYPE.
Line 12: Invoke the SoapUI script passing in any project specific <PARAMETERS>
This example invokes a JMeter test plan using the Taurus testing framework
pipeline { // Use the taurus agent which has Taurus the bits in it agent { node { label 'taurus' } // end node } // end agent stages{ // Checkout code stage ('Checkout') { steps{ checkout([$class: 'GitSCM', branches: [[name: '**']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'CREDENTIALS', // where CREDENTIALS is based on your project service account url: 'GIT_REPO']]]) // where GIT_REPO is the repository containing your Jenkinsfile, Taurus configuration and JMeter Test Plan } //end steps } // end stage // Run JMeter test using Taurus stage ('Run Jmeter test') { steps { container('taurus'){ sh '/usr/local/bin/bzt config.yaml plan.yaml' // where config.yaml contains Taurus specific configuration and plan.yaml contains Test Plan information } //end container } // end steps } // end stage // Gather the results stage ('Gather results') { steps { container('taurus'){ perfReport filterRegex: '', sourceDataFiles: '**/*.jtl' // The performance plugin knows how to render results of various test frameworks into Jenkins } //end container } // end steps } // end stage } // end stages } //end pipeline |
below is a sample config.yaml:
modules: jmeter: path: ~/.bzt/jmeter-taurus/5.1.1/bin/jmeter # always us this path, the JMeter version is bundled in the image version: 5.1.1 # always use this version, it is bundled in the agent image force-ctg: true # true by default detect-plugins: false |
below is a (very) simple project configuration:
execution: - write-xml-jtl: full scenario: jmeter-test scenarios: jmeter-test: script: simple-test.jmx |
This sample runs a single JMeter test scenario, jmeter-test that invokes the simple-test.jmx JMeter Test Plan. The script: element is relative to your project root.
More information on running JMeter tests in Taurus can be found here https://gettaurus.org/docs/JMeter/.