Search code examples
jenkinsgroovygerrit

Read data in jenkinsfile from xml file created in current workspace


Some time ago I tried to connect jenkins and gerrit and send cppcheck output from jenkins to gerrit as comment:

  1. I installed proper patches for jenkins and gerrit(that is ok it's work)
  2. In jekinsfile I'm tried to run cppckeck and save it's output to xml file(it's works)

Problem is here that when I'm trying to read xml file, and I have information that there is no such file. I see that script have different root catalog(i groovy I printed dir). I think code with my experimental jenkinsfile explain problem:

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

How to load this file. Or I'm doing it's all wrong?(I mean integration gerrit with jenkins who purpose is to run cppcheck and cpplint and show results in gerrit).


Solution

  • If the file is in your repo, you need to check out the repo first 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"])
            }
        }
    }