Jenkins 管道 - 根据标志值运行构建后步骤

问题描述 投票:0回答:1

我想发布测试结果作为后期构建操作的一部分,只有当阶段 -

Execute Test
已经运行时,我的意思是如果构建在执行测试阶段之前失败,那么跳过发布测试结果作为后期构建的一部分.

我已经将标志变量定义为全局变量,如果运行了执行测试阶段,则将值操作为 True。如果标志为 True,则作为构建后操作的一部分执行发布测试结果功能,但它会抛出以下错误。 我究竟做错了什么 ?谢谢..

WorkflowScript: 51: Expected a stage @ line xxx, column x.

           post {

           ^

红色管道:

def flag = false
@Field String NEXUS = 'our-nexus-link'

def call(body) {
    def pipelineParams = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = pipelineParams
    body()

    pipeline {
        agent {
            .....
            }
        }
        
         stages {
            stage ('Git Setup') {
                steps {
                    .....       
                }
            }

            stage ('Compile') {
                .......
            }

            stage('Scan') {
                        .........
                    }
            
            stage('Execute Test') {
                        steps {
                            container('Go') {
                                function_to_Run_TestCases(parameters)
                                script { flag = true }      
                            }
                        }
                    }
        post {
            always {
                dir(workspace) {
                    archiveArtifacts artifacts: workspace, allowEmptyArchive: true
                }
                script {
                    if (flag == true) { 
                       function_to_PUBLISH_TestCases(testDir: checker_dir) 
                    }
                }
            }
} 
jenkins groovy jenkins-pipeline jenkins-groovy
1个回答
1
投票

您在这里面临的问题是

post
部分应该在
stages
部分完成之后出现,而不是在
stages
部分内。所以你在
}
之前错过了一个
post
。考虑文档中的这个example以获取更多信息。

另一方面,在“执行测试”阶段使用

post
可能会更好地完成您想要实现的目标,如下所示:

stage('Execute Test') {
    steps {
        container('Go') {
            function_to_Run_TestCases(parameters)    
        }
    }
    post {
        success {
            dir(workspace) {
                archiveArtifacts artifacts: workspace, allowEmptyArchive: true
            }
            script {
                function_to_PUBLISH_TestCases(testDir: checker_dir) 
            }
        }
    }
}

这样,您就不需要单独的标志,因为如果“执行测试”阶段失败,

function_to_PUBLISH_TestCases
将不会运行。

© www.soinside.com 2019 - 2024. All rights reserved.