Record warnings from a given stage in a jenkins pipeline

1.2k views Asked by At

I'm trying to use the Warnings Next Generation Plugin to record GCC warnings in a Jenkins pipeline.

I have multiple stages inside a matrix section and I'd like to record the warnings which appear on a given stage and ideally as a bonus, be able to discriminate per axis value (products).

As a minimal example, I wrote the following pipeline:

pipeline{
    agent { label 'master' }
    stages {
        stage('Create workspace') {
            steps {
                deleteDir()
                sh "echo 'main() { }' > build_1_file.c"
                sh "echo 'int main() { }' > build_2_file.c"
            }
        }
        stage('Main stage') {
            matrix {
                axes {
                    axis {
                        name 'PRODUCT'
                        values 'first_product', 'second_product'
                    }
                }
                stages {
                    stage('Build 1') {
                        steps {
                            echo "Build 1 for ${PRODUCT}"
                            sh "if [ ${PRODUCT} = first_product ]; then gcc build_1_file.c; fi"
                        }
                    }
                    stage('Build 2') { 
                        steps {
                            echo "Build 2 for ${PRODUCT}"
                            sh "gcc build_2_file.c -Wstrict-prototypes"
                            recordIssues tool: gcc(name: "${PRODUCT} GCC warnings")
                        }
                    }
                }
            }
        }
    }
}

First issue is that if I put recordIssues in one stage, then the warnings which appear in a stage before, will be recorded, when they shouldn't. For example, the warning detected in build_1_file.c in stage 'Build 1', will be recorded. Second, both first_product GCC warnings and second_product GCC warnings will show 2 warnings, when only the second should (because of the if [ ${PRODUCT} = first_product ]).

Is there a solution to do what I want?

1

There are 1 answers

0
ncarrier On BEST ANSWER

Well, I think I finally found a solution. The key is to redirect the compilation logs to a file and use that file's path as a pattern for the gcc tool. This gives:

pipeline{
    agent { label 'master' }
    stages {
        stage('Create workspace') {
            steps {
                deleteDir()
                sh "echo 'main() { }' > build_1_file.c"
                sh "echo 'int main() { }' > build_2_file.c"
            }
        }
        stage('Main stage') {
            matrix {
                axes {
                    axis {
                        name 'PRODUCT'
                        values 'first_product', 'second_product'
                    }
                }
                stages {
                    stage('Build 1') {
                        steps {
                            echo "Build 1 for ${PRODUCT}"
                            sh "gcc build_1_file.c"
                        }
                    }
                    stage('Build 2') { 
                        steps {
                            echo "Build 2 for ${PRODUCT}"
                            sh "if [ ${PRODUCT} = second_product ]; then gcc build_2_file.c -Wstrict-prototypes |& tee ${PRODUCT}.log; fi"
                            recordIssues tool: gcc(pattern: "${PRODUCT}.log", name: "${PRODUCT} GCC warnings", id: "${PRODUCT} GCC warnings")
                        }
                    }
                }
            }
        }
    }
}