主要内容

用于 Polyspace 分析的 Jenkins Pipeline 脚本示例

使用 Jenkins® Pipeline,可以通过 Jenkins 中的脚本自动执行持续交付管道的工作流。您可以编写 Pipeline 脚本以编译工程和运行测试套件,并在代码准备好交付之前执行所有必要的检查。您可以将这些脚本作为版本控制系统的一部分签入,并对它们进行与代码本身相同的审查和版本控制。

您可以在 Jenkins Pipeline 脚本中运行 Polyspace® 分析。如果您在 Jenkins 中使用的是 Freestyle 工程而不是 Pipeline,请使用 Polyspace 插件以便于编写脚本。请参阅使用 Jenkins 进行 Polyspace 分析的示例脚本。如果您使用的是 Pipeline,请修改下面提供的脚本来运行 Polyspace 分析。

前提条件

要在服务器端运行 Polyspace 分析并在 Polyspace Access™ Web 界面中审查结果,您必须执行以下一次性设置。

  • 要运行该分析,您必须安装 Polyspace Server 产品的一个实例。

  • 要上传结果,您必须设置托管 Polyspace Access 的 Web 界面所需的组件。

  • 要审查上传的结果,您和审查结果的每位开发人员都必须拥有一个 Polyspace 许可证。

请参阅安装 Polyspace Server 和 Access 产品

在 Pipeline 脚本中分阶段运行 Polyspace 分析

要创建 Jenkins Pipeline 脚本,请执行以下操作:

  1. 在 Jenkins 界面中,从左侧选择新项目。选择 Pipeline

  2. 在工程的 Pipeline 部分中,为 Definition 选择 Pipeline script。输入此脚本。

    粗体部分表示您必须修改源代码和 Polyspace 安装的脚本的位置。

    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"
        }
    }

编译此工程时,您可以查看该分析的各个阶段,如下所示:

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

此脚本可以是保存在 Jenkinsfile 中并提交到版本控制系统的较大脚本的一部分。请参阅使用 Jenkinsfile

您可以根据需要修改脚本:

  • 此脚本在单独的 stage 部分中运行 Polyspace 分析工作流的每个步骤。您可以将多个步骤组合到一个 stage 中。

  • 此脚本会使用 sh 指令运行 Linux® Shell 命令。您可以改用 bat 指令运行 Windows® 命令。

  • 此脚本使用凭据插件中的数据提取用户名和密码。如果您以其他形式保存凭据,则可以替换将用户凭据绑定到变量的 withCredentials 命令。

  • 此脚本通过以下 make 命令在 Git 沙盒上使用 makefile 来编译源代码:

    make -C $git_sandbox
    如果您使用其他编译命令,则可以将此行替换为您自己的编译命令。

有关此脚本中特定于 Pipeline 的语法的详细信息,请参阅:

有关此脚本中 Polyspace 命令的详细信息,请参阅:

另请参阅

| (Polyspace Access)