首页 > 解决方案 > 从当前工作空间中创建的 xml 文件中读取 jenkinsfile 中的数据

问题描述

前段时间,我尝试连接 jenkins 和 gerrit,并将 jenkins 的 cppcheck 输出作为注释发送到 gerrit:

  1. 我为 jenkins 和 gerrit 安装了适当的补丁(没关系,它可以工作)
  2. 在 jekinsfile 我试图运行 cppckeck 并将其输出保存到 xml 文件(它的作品)

问题就在这里,当我试图读取 xml 文件时,我有信息表明没有这样的文件。我看到该脚本有不同的根目录(我很好地打印了目录)。我认为我的实验 jenkinsfile 的代码解释了问题:

pipeline {
agent any

stages {
    stage('Example') {
        steps {
            gerritReview labels: [Verified: 0]
            sh 'pwd'
            sh 'cppcheck --enable=all --inconclusive --xml --xml-version=2 *.c* 2> cppcheck.xml'
            script {
            def parser = new XmlParser()
            def doc = parser.parse("cppcheck.xml"); // No xml file here because script {} 
            // is run in different place  
        }
    }
}
post {
    always {
        gerritReview score: 1
        step([$class: 'CppcheckPublisher', pattern: 'cppcheck.xml', ignoreBlankFiles: false, treshold: "0"])
    }
         
} }

如何加载此文件。或者我做错了?(我的意思是把 gerrit 与 jenkins 集成,目的是运行 cppcheck 和 cpplint 并在 gerrit 中显示结果)。

标签: jenkinsgroovygerrit

解决方案


如果文件在您的仓库中,您需要先查看仓库 https://www.jenkins.io/doc/pipeline/steps/workflow-scm-step/

pipeline {
    agent any

    stages {
        stage('Example') {
            steps {
                checkout scm // ADD THIS LINE
                gerritReview labels: [Verified: 0]
                sh 'pwd'
                sh 'cppcheck --enable=all --inconclusive --xml --xml-version=2 *.c* 2> cppcheck.xml'
                script {
                    def parser = new XmlParser()
                    def doc = parser.parse("cppcheck.xml");
                }
        }
    }
    post {
        always {
            gerritReview score: 1
            step([$class: 'CppcheckPublisher', pattern: 'cppcheck.xml', ignoreBlankFiles: false, treshold: "0"])
        }
    }
}

推荐阅读