在始终发布部分中发布Nunit测试结果

时间:2018-08-15 14:30:27

标签: powershell jenkins nunit jenkins-pipeline pester

我正在尝试运行进行Pester测试并发布NUnit结果的管道。

引入了新的测试,无论出于何种原因,Jenkins都不再在Powershell脚本之后立即发布测试结果和错误。因此,它并没有涉及到nunit发布。我收到这个:

ERROR: script returned exit code 128 Finished: FAILURE

我一直试图在Jenkinsfile的post部分的always部分中包含publish,但是,我在如何使NUnit测试文件可用方面遇到了问题。

我尝试建立一个agentunstash文件(即使如果powershell脚本取消了整个管道,它也可能不会隐藏)。当我使用agent时,出现以下异常:

java.lang.NoSuchMethodError: No such DSL method 'agent' found among steps

这是Jenkins文件:

pipeline {
    agent none

    environment {
        svcpath = 'D:\\svc\\'
        unitTestFile = 'UnitTests.xml'
    }
    stages {
        stage ('Checkout and Stash') {
            agent {label 'Agent1'}
            steps {
                stash name: 'Modules', includes: 'Modules/*/**'
                stash name: 'Tests', includes: 'Tests/*/**'
            }
        }
        stage ('Unit Tests') {
            agent {label 'Agent1'}
            steps {
                dir(svcpath + 'Modules\\'){deleteDir()}
                dir(svcpath + 'Tests\\'){deleteDir()}
                dir(svcpath){
                unstash name: 'Modules'
                unstash name: 'Tests'
            }
            dir(svcpath + 'Tests\\'){
            powershell """

            \$requiredCoverageThreshold = 0.90

            \$modules = Get-ChildItem ../Modules/ -File -Recurse -Include *.psm1

            \$result = Invoke-Pester -CodeCoverage \$modules -PassThru -OutputFile ${unitTestFile} -OutputFormat NUnitXml

            \$codeCoverage = \$result.CodeCoverage.NumberOfCommandsExecuted / \$result.CodeCoverage.NumberOfCommandsAnalyzed

            Write-Output \$codeCoverage

            if (\$codeCoverage -lt \$requiredCoverageThreshold) {
                Write-Output "Build failed: required code coverage threshold of \$(\$requiredCoverageThreshold * 100)% not met. Current coverage: \$(\$codeCoverage * 100)%."
                exit 1
            } else {
                write-output "Required code coverage threshold of \$(\$requiredCoverageThreshold * 100)% met. Current coverage: \$(\$codeCoverage * 100)%."
            }
            """
            stash name: 'TestResults', includes: unitTestFile
            nunit testResultsPattern: unitTestFile
        }
    }
    post {
        always {
            echo 'This will always run'
            agent {label 'Agent1'}
            unstash name: 'TestResults'
            nunit testResultsPattern: unitTestFile
        }
        success {
            echo 'This will run only if successful'
        }
        failure {
            echo 'This will run only if failed'
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
        }
        changed {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
       }
    }
}

欢迎任何输入!谢谢!

2 个答案:

答案 0 :(得分:0)

您遇到的异常是由于Jenkins严格的管道DSL。允许使用代理的文档为here

当前不允许在帖子部分使用agent {...}。也许将来会改变。如果您要求整个作业在服务标签为“ Agent1”的节点上运行,那么当前唯一的方法是

  1. agent {label 'Agent1'}放在pipeline {下方,以使其全局化
  2. 在每个阶段删除agent {label 'Agent1'}的所有实例
  3. 从帖子部分删除agent {label 'Agent1'}

答案 1 :(得分:0)

post 部分的作用更像是传统的脚本化 DSL,而不是管道声明式 DSL。所以你必须使用 node() 而不是 agent

我相信我自己也有过同样的问题,还有 this SO post has the answer 和一些很好的上下文。

This Jenkins issue 并不完全相同,但在后期阶段显示了 node 语法。