Is there a way to move the entire post {} build section in Jenkinsfile to the global pipeline library?
Asked Answered
B

1

3

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?

Bi answered 10/4, 2019 at 0:12 Comment(2)
At the end of your question, it sounds like you have two different aims that are in direct conflict with each other. Could you please clarify?Hoiden
@mattSchuchard, not sure what do you mean I have two different aims. My only aim is to move the entire post {} block section from my every single Jenkinsfile to a shared library and replace it by single line where I would just call this functionality (or massive post {} block of code). I only latter tried to explain what I've already learned and tried and my concern how this all could work and asking for working example: - show me simple Jenkinsfile - show me simple implementation of post {} black in a shared lib Hope it's clearer now.Bi
T
0

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()
    }
}
Touchwood answered 28/4, 2019 at 1:0 Comment(2)
I'm not able to verify it as working because I am not working in Release Engineering space anymore, but will try to give a go if I'd have an opportunity again. Many thanks anyway.Bi
Can confirm this doesn't work. WorkflowScript: 20: Undefined section "script" @ line 20, column 5.Chacon

© 2022 - 2024 — McMap. All rights reserved.