如何使用Jenkins声明性管道在多个平台上进行构建和测试

idv4meu8  于 2022-11-21  发布在  Jenkins
关注(0)|答案(1)|浏览(167)

我想做一件我觉得应该很简单的事,但我不知道怎么做。
基本上我有一个Jenkins主程序(运行在Linux上)和两个从程序,一个在Windows上,另一个在MacOS上。
我想在所有3个平台上构建我的项目,并在所有3个平台上运行GTest测试。
我可以构建并运行测试,但是junit步骤似乎没有收集到任何测试结果。
我试着把post块放在任何地方,但是它不起作用。如果我试着把post块放在测试阶段或者作为stages的兄弟,我会得到下面的错误:
Required context class hudson.FilePath is missing Perhaps you forgot to surround the code with a step that provides this, such as: node,由agent none引起-post块不知道运行到哪里。
因此,我尝试在测试阶段的parallel步骤中将post块放在node块内,但它似乎没有做任何事情-它甚至没有显示在控制台输出中。
下面是我的Jenkinsfile

pipeline {
    agent none
    stages {
        stage ('Clean') {
            steps {
                parallel (
                    "linux" : {
                        node ("linux") {
                            dir("build") {
                                deleteDir()
                                writeFile file:'dummy', text:'' // Creates the directory
                            }
                        }
                    },
                    "windows" : {
                        node('windows') {
                            dir("build") {
                                deleteDir()
                                writeFile file:'dummy', text:'' // Creates the directory
                            }
                        }
                    },
                    "mac" : {
                        node('mac') {
                            dir("build") {
                                deleteDir()
                                writeFile file:'dummy', text:''  // Creates the directory
                            }
                        }
                    }
                )
            }
        }

        stage ('Build') {
            steps {
                parallel (
                    "linux" : {
                        node ("linux") {
                            checkout scm
                            dir("build") {
                                sh '/opt/cmake/bin/cmake .. -DCMAKE_BUILD_TYPE=Release'
                                sh 'make'
                            }
                        }
                    },
                    "windows" : {
                        node('windows') {
                            checkout(changelog: false, scm: scm) // Changelog to false, otherwise Jenkins shows duplicates. Only linux (the Jenkins master) has the changelog enabled.
                            dir("build") {
                                bat 'cmake .. -G "Visual Studio 15 2017 Win64" -DCMAKE_PREFIX_PATH=C:/Qt/5.9.1/msvc2017_64'
                                bat "\"${tool 'MSBuild'}\" project.sln /p:Configuration=Release /p:Platform=\"x64\" /p:ProductVersion=1.0.0.${env.BUILD_NUMBER} /m"
                            }
                        }
                    },
                    "mac" : {
                        node('mac') {
                            checkout(changelog: false, scm: scm) // Changelog to false, otherwise Jenkins shows duplicates. Only linux (the Jenkins master) has the changelog enabled.
                            dir("build") {
                                sh 'cmake .. -DCMAKE_PREFIX_PATH=/usr/local/Cellar/qt/5.9.1 -DCMAKE_BUILD_TYPE=Release'
                                sh 'make'
                            }
                        }
                    }
                )
            }
        }

        stage ('Test') {
            steps {
                parallel (
                    "linux" : {
                        node ("linux") {
                            dir('Build') {
                                sh './bin/project-tests --gtest_output=xml:project-tests-results.xml'
                                // Add other test executables here.
                            }

                            post {
                                always {
                                    junit '*-tests-results.xml'
                                }
                            }
                        }
                    },
                    "windows" : {
                        node('windows') {
                            dir("build") {
                                bat 'tests\\project\\Release\\project-tests --gtest_output=xml:project-tests-results.xml'
                                // Add other test executables here.
                            }

                            post {
                                always {
                                    junit '*-tests-results.xml'
                                }
                            }
                        }
                    },
                    "mac" : {
                        node('mac') {
                            dir("build") {
                                sh './bin/project-tests --gtest_output=xml:project-tests-results.xml'
                                // Add other test executables here.
                            }
                            post {
                                always {
                                    junit '*-tests-results.xml'
                                }
                            }
                        }
                    }
                )
            }
        }
    }

}

我做错了什么?

ikfrs5lh

ikfrs5lh1#

  1. post{}块应仅在steps{}parallel{}(对于并行级)之后生效。
    1.如果需要在节点环境中执行post,则应向整个阶段提供一个节点(agent{}语句)。
    你可以尝试使用并行阶段执行。我也建议使用函数来缩短代码。
    大概是这样的:
void Clean() {
    dir("build") {
        deleteDir()
        writeFile file:'dummy', text:'' // Creates the directory
    }
}

void SmthElse(def optionalParams) {
    // some actions here
}

pipeline {
    agent none
    options {
        skipDefaultCheckout(true)   // to avoid force checkouts on every node in a first stage
        disableConcurrentBuilds()   // to avoid concurrent builds on same nodes
    }
    stages {
        stage('Clean') {
            failfast false
            parallel {
                    stage('Linux') {
                        agent {label 'linux'}
                        steps {Clean()}
                        post {
                            // post statements for 'linux' node
                            SmthElse(someParameter)
                        }
                    }
                    stage('Windows') {
                        agent {label 'windows'}
                        steps {Clean()}
                        post {
                            // post statements for 'windows' node
                        }
                    }
                    stage('MacOS') {
                        agent {label 'mac'}
                        steps {Clean()}
                        post {
                            // post statements for 'mac' node
                        }
                    }
            }
            post {
                // Post statements OUTSIDE of nodes (i.e. send e-mail of a stage completion)
            }
        }

        // other stages (Build/Test/Etc.)
    }
}

或者,您可以在post语句中使用node

stage('Test') {
    steps {
        // your parallel Test steps
    }
    post {
        always {
            script {
                parallel (
                    "linux" : {
                        node('linux') {
                            // 'linux' node post steps
                        }
                    },
                    "windows" : {
                        node('windows') {
                            // 'windows' node post steps
                        }
                    }

                    // etc
                )
            }
        }
    }
}

相关问题