Skip to main content

Pipeline Code Examples for Running the Scan

Jenkins supports two syntax types for the development of Pipeline code:

  • Scripted syntax—The “traditional” syntax used to develop the Pipeline as a script using Groovy as the domain-specific language.

  • Declarative syntax—A simple, user-friendly syntax with a predefined hierarchy of statements that makes Pipeline development easier than with the Scripted syntax. Additionally, it does not require knowledge of the Groovy language. Jenkins support for the Declarative syntax was introduced with Jenkins Pipeline Plugin 2.5.

The following examples show both types of Pipeline code syntax in which the Pipeline script for the scan has been incorporated:

The Pipeline script for the scan step is highlighted in each example.

Example Declarative Pipeline Code to Run the Scan

The following is an example of Declarative code used to run the Code Insight scan as a StartScan step in the Pipeline process:

pipeline {
agent any
stages {
stage('Checkout build and scan project1') {
steps {
git credentialsId: 'abcd', url: 'git://git.company.com/organization/repository1.git'
sh "'PATH_TO_MAVEN/bin/mvn' clean install"

StartScan (baseUrl: '<http://HOST_NAME:PORT/>', projectName: '<CODEINSIGHT_PROJECT_NAME>', alias: '<SCAN-AGENT_ALIAS>', host: '<SCAN-AGENT_HOST>', token: '<JWT_TOKEN>')
}
}
}
}

Example Scripted Pipeline Code to Run the Scan

The following is an example of Scripted Pipeline code used to run the Code Insight scan as a StartScan step in the Pipeline process. The example also shows how to set up individual scans within a single Pipeline job by specifying multiple directories.

node {
checkout1()
checkout2()
}

def checkout1(){
dir("project-1"){
stage ('Checkout project 1'){
git credentialsId: 'abcd', url: 'git://git.company.com/organization/repository1.git'
}
stage ('Build Project 1'){
build()
}
stage ('Scan Project 1'){
StartScan (baseUrl: '<http://HOST_NAME:PORT/>', projectName:
'<CODEINSIGHT_PROJECT_NAME>', alias: '<SCAN-AGENT_ALIAS>', host:
'<SCAN-AGENT_HOST>', token: '<JWT_TOKEN>')
}
}
}

def checkout2(){
dir("project-2"){
stage ('Checkout project 2'){
git credentialsId: 'abcd', url: 'git://git.company.com/organization/repository2.git'
}
stage ('Build Project 2'){
build()
}
stage ('Scan Project 2'){
StartScan (baseUrl: '<http://HOST_NAME:PORT/>', projectName: '<CODEINSIGHT_PROJECT_NAME>',
alias: '<SCAN-AGENT_ALIAS>', host: '<SCAN-AGENT_HOST>', token:
'<JWT_TOKEN>')
}
}
}
def build(){
sh "'PATH_TO_MAVEN/bin/mvn' clean install"
}