Search code examples
jenkinsgroovyjenkins-pipelinejenkins-groovy

Jenkins pipeline - Run post build step based on flag value


I wanted to publish the test results as part of the post build action, only if the stage - Execute Test has ran, I mean if the build fails before Execute Test stage, then skip the publish test results as part of the post build.

I've defined a flag variable as a global variable, and manipulate the value to True, if the Execute Test stage is run. If the flag is True, then execute the publish test result function as part of post build action, but it is throwing the below error. What am I doing wrong ? Thank you..

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

           post {

           ^

Redcated pipeline:

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

Solution

  • The problem you are facing here is that the post section should come after the stages section is finished, not within the stages section. So you're missing a } before post. Consider this example from the documentation for more information.

    On the other hand, what you are trying to achieve might be better done by using post within the "Execute Test" stage, like this:

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

    This way, you won't need a separate flag since function_to_PUBLISH_TestCases won't run if the "Execute Test" stage fails.