I'm relatively new to Jenkins pipelines, but having implemented already a few, I've realised I need to start using jenkins shared library before I go mad.

Have already figured out how to define some repetitive steps in the library and call them with less clutter from Jenkinsfile, but not sure if the same thing can be done for the entire post build section (thought I've read about to how to define the entire pipeline in the lib and similar), as this is pretty much static end of every single pipeline code:

@Library('jenkins-shared-library')_
pipeline {
    agent none
    stages {
        stage ('System Info') { agent any
            steps { printSysInfo() }
        }

        stage ('Init'){ agent {label 'WinZipSE'}
            steps { init('SCMroot') }
        }
        stage('Build') { agent any 
            steps { doMagic() }
        }
    }

    // This entire 'post {}' section needs to go to a shared lib
    // and be called just with a simple methed call, e.g.
    // doPostBuild()
    post {
        always {
            node ('master') {
                googlechatnotification (
                message: '[$BUILD_STATUS] Build $JOB_NAME $BUILD_NUMBER has finished',
                url: 'id:credential_id_for_Ubuntu')

                step (
                    [$class: 'Mailer',
                    recipients: '[email protected] [email protected]',
                    notifyEveryUnstableBuild: true,
                    sendToIndividuals: true]
                )
            }
        }
        success {
            node ('master') {
                echo 'This will run only if successful'
            }
        }
        failure {
            node ('master') {
                echo 'This will run only if failed'
            }
        }
        // and so on
    }
}

I just dunno how to syntactically achieve that. For sure, I can define the entire post build section an a lib/var like: doPotBuild.groovy

def call () {
  post {...}
}

but how I will eventually call it from within my Jenkinsfile outside of that defined post {} build block section (AKA stages).

I can call it within some stage('post build){doPostBuild()}, but it won't serve the way how the true post {} section is supposed to work, e.g. it won't get executed it there was failure in one of the previous stages.

Any thoughts on that and mainly working examples?

1 Answers

0
metalisticpain On

I am not entirely if this will work as I don't use declarative pipelines, so am unsure how rigid the top level structure is. But I would revert to a script block.

@Library('jenkins-shared-library')_
pipeline {
    agent none
    stages {
        stage ('System Info') { agent any
            steps { printSysInfo() }
        }

        stage ('Init'){ agent {label 'WinZipSE'}
            steps { init('SCMroot') }
        }
        stage('Build') { agent any 
            steps { doMagic() }
        }
    }

    // This entire 'post {}' section needs to go to a shared lib
    // and be called just with a simple methed call, e.g.
    // doPostBuild()
    script {
        doPostBuild()
    }
}