Main Content

Sample Jenkins Pipeline Scripts for Polyspace Analysis

Jenkins® Pipelines enable automating the workflow of a continuous delivery pipeline through scripts in Jenkins. You can write Pipeline scripts that build projects, run test suites and perform all necessary checks before your code is ready for shipping. You can check in these scripts as part of a version control system and subject them to the same review and versioning as the code itself.

You can run a Polyspace® analysis in a Jenkins Pipeline script. If you are using Freestyle Projects instead of Pipelines in Jenkins, use the Polyspace plugin for scripting conveniences. See Sample Scripts for Polyspace Analysis with Jenkins. If you are using Pipelines, modify the script provided below to run a Polyspace analysis.

Prerequisites

To run a Polyspace analysis on a server and review the results in the Polyspace Access web interface, you must perform a one-time setup.

  • To run the analysis, you must install one instance of the Polyspace Server product.

  • To upload results, you must set up the components required to host the web interface of Polyspace Access.

  • To view the uploaded results, you and each developer reviewing the results must have one Polyspace license.

See Install Polyspace Server and Access Products.

Run Polyspace Analysis in Stages in a Pipeline Script

To create a Jenkins Pipeline script:

  1. In the Jenkins interface, select New Item on the left. Select Pipeline.

  2. In the Pipeline section of the project, select Pipeline script for Definition. Enter this script.

    The parts in bold indicate places where you have to modify the script for your source code and Polyspace installation.

    node {
        def module = "folder_to_hold_sources_and_results" // Replace with another foldername (or use job workspace)
        def git_url = "https://github.com/lz4/lz4.git" //Replace with another Git URL
        def git_sandbox = "${module}/lz4_sources" //Replace with path to sources
        def polyspace_bin = "/usr/local/Polyspace/R2019a/polyspace/bin" // Replace with Polyspace installation folder
        def configure = "${polyspace_bin}/polyspace-configure"
        def configure_opts = "${module}/lz4.opts"
        def analyze = "${polyspace_bin}/polyspace-bug-finder-server" // Replace with polyspace-code-prover-server to run Code Prover
        def results_dir = "${module}/results"
        def protocol = "https" //Replace with "http" if Polyspace Access URL uses http
        def host = "your-host-name" //Replace with host name of server hosting Polyspace Access
        def port = "9443" //Replace with port number of server hosting Polyspace Access
        def subject = "Polyspace analysis: $git_url" //Replace with a different mail subject line
        def body = "Log available on ${BUILD_URL}/ConsoleFull" //Replace with a different mail body line
        def to = "john@email.com" //Replace with a comma-separated list of email addresses
        
        stage ("Prepare") {
            sh script: "env"
            sh label: "Cleanup", script: "rm -fr $module; mkdir -p $module"
        }
        
        stage ("Checkout") {
            sh label: "Check out", script: "git clone $git_url $git_sandbox"
        }
        
        stage ("Configure") {
            sh label: "Polyspace configure", script: "$configure -allow-overwrite -prog \"lz4\" -author jenkins -output-options-file $configure_opts make -C $git_sandbox"
        }
        
        stage ("Analyze") {
            sh label: "Polyspace analysis", script: "$analyze -options-file $configure_opts -checkers all -results-dir $results_dir "
        }
        
        stage ("Upload") {
            withCredentials([usernamePassword(credentialsId: 'credentials-id-from-jenkins-credentials-plugin', passwordVariable: 'password', usernameVariable: 'username')]) 
            {
                def access = "${polyspace_bin}/polyspace-access -tmp-dir tmp-dir -protocol ${protocol} -host ${host} -port ${port} -login ${username} -encrypted-password ${password}"
                sh label: "Create folder on server", script: "$access -create-project \"/public/JenkinsProject\""
                sh label: "Upload results on server", script: "$access -parent-project \"/public/JenkinsProject\" -upload $results_dir"
            }
        }
        
        stage ("Notification") {
            mail body: "$body", subject: "$subject", to: "$to"
        }
    }

When you build this project, you can see the various stages of the analysis like this:

Graphical view of a Jenkins Pipeline run showing the duration of each stage of Pipeline

This script can be part of a larger script that you save in a Jenkinsfile and commit to your version control system. See Using a Jenkinsfile.

You can modify the script as needed:

  • The script runs each step of the Polyspace analysis workflow in a separate stage section. You can combine several steps together in one stage.

  • The script runs Linux® Shell commands by using the sh directive. You can run Windows® commands by using the bat directive instead.

  • The script uses data from the Credentials plugin to extract user name and password. If you save credentials in some other form, you can replace the withCredentials command that binds user credentials to variables.

  • The script builds source code using a makefile on a Git sandbox with this make command:

    make -C $git_sandbox
    If you use a different build command, you can replace this line with your build command.

For more information on the Pipeline-specific syntax in this script, see:

For more information on the Polyspace commands in this script, see:

See Also