Execute a function from post actions in Jenkinsfile

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Execute a function from post actions in Jenkinsfile

Kernel Panic

Hello there.

I looking for an elagant way to execute code from the post {} section,
I want to write a clean Jenkinsfile and execute code from shared libraries as
posible.
 
In the post section I have the tipical  clean working directory like this:

always {
      cleanWs()
    }

I also send notification based on pipeline completion, I mean, aobrted , failed,
changed and so on,  but I want to call a function to do that, I want to remove the
email code I have something like this:

emailext body: 'Check console output at $BUILD_URL to view the results. \n\n ${CHANGES} \n\n --------------------------------- \n${BUILD_LOG, maxLines=50, escapeHtml=false}',
                to: "${EMAIL_ADDRESSES}",
                subject: 'Pipeline Execution  Failed: $PROJECT_NAME - #$BUILD_NUMBER'

The problem seems that from the post you only can call an expected function of
type: always, changed, fixed, regression, aborted, success, unsuccessful, unstable, failure, notBuilt, cleanup.

Is there another way to accomplish this to write a more clean Post section?


Thanks
Regards

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/ea74e553-88a7-4925-ade2-27462aaceab1n%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: Execute a function from post actions in Jenkinsfile

Gianluca

Hi,

I'm not sure I understood your issue on calling functions. Maybe there is only a bit of confusion about the Jenkins pipeline syntax.

From what you said, you are already calling functions into the post sections: "cleanWs()" is a function, "emailext body: ... " is a function.

So, maybe you are confused by the fact Jenkins syntax in post doesn't allow something like:

post {

    aFunction()

}

So, nope, the above doesn't work. Because the "post" syntax are blocks based on the result of the pipeline and you actually listed all possible options:

"always, changed, fixed, regression, aborted, success, unsuccessful, unstable, failure, notBuilt, cleanup"

And if I understood, you want to call a function that does different things depending on the pipeline result ... then, you need to used "always":

post {

   always {

       aFunction(BUILD_RESULT)

   }

}

And then the function will do different things depending of the BUILD_RESULT value:

if (BUILD_RESULT == "SUCCESS") {

   message = "Pipeline failed"

} else {

   message = "Pipeline ... whatever"

}

I hope that helps.

Cheers,

Gianluca.


On 20/12/2020 17:35, Kernel Panic wrote:

Hello there.

I looking for an elagant way to execute code from the post {} section,
I want to write a clean Jenkinsfile and execute code from shared libraries as
posible.
 
In the post section I have the tipical  clean working directory like this:

always {
      cleanWs()
    }

I also send notification based on pipeline completion, I mean, aobrted , failed,
changed and so on,  but I want to call a function to do that, I want to remove the
email code I have something like this:

emailext body: 'Check console output at $BUILD_URL to view the results. \n\n ${CHANGES} \n\n --------------------------------- \n${BUILD_LOG, maxLines=50, escapeHtml=false}',
                to: "${EMAIL_ADDRESSES}",
                subject: 'Pipeline Execution  Failed: $PROJECT_NAME - #$BUILD_NUMBER'

The problem seems that from the post you only can call an expected function of
type: always, changed, fixed, regression, aborted, success, unsuccessful, unstable, failure, notBuilt, cleanup.

Is there another way to accomplish this to write a more clean Post section?


Thanks
Regards

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/ea74e553-88a7-4925-ade2-27462aaceab1n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/7379987a-7062-0c47-d447-98f6a6999142%40gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Execute a function from post actions in Jenkinsfile

Kernel Panic
Hello
Yes, that's what I want to do
Thank you so much.


El domingo, 20 de diciembre de 2020 a las 14:51:45 UTC-3, Gianluca escribió:

Hi,

I'm not sure I understood your issue on calling functions. Maybe there is only a bit of confusion about the Jenkins pipeline syntax.

From what you said, you are already calling functions into the post sections: "cleanWs()" is a function, "emailext body: ... " is a function.

So, maybe you are confused by the fact Jenkins syntax in post doesn't allow something like:

post {

    aFunction()

}

So, nope, the above doesn't work. Because the "post" syntax are blocks based on the result of the pipeline and you actually listed all possible options:

"always, changed, fixed, regression, aborted, success, unsuccessful, unstable, failure, notBuilt, cleanup"

And if I understood, you want to call a function that does different things depending on the pipeline result ... then, you need to used "always":

post {

   always {

       aFunction(BUILD_RESULT)

   }

}

And then the function will do different things depending of the BUILD_RESULT value:

if (BUILD_RESULT == "SUCCESS") {

   message = "Pipeline failed"

} else {

   message = "Pipeline ... whatever"

}

I hope that helps.

Cheers,

Gianluca.


On 20/12/2020 17:35, Kernel Panic wrote:

Hello there.

I looking for an elagant way to execute code from the post {} section,
I want to write a clean Jenkinsfile and execute code from shared libraries as
posible.
 
In the post section I have the tipical  clean working directory like this:

always {
      cleanWs()
    }

I also send notification based on pipeline completion, I mean, aobrted , failed,
changed and so on,  but I want to call a function to do that, I want to remove the
email code I have something like this:

emailext body: 'Check console output at $BUILD_URL to view the results. \n\n ${CHANGES} \n\n --------------------------------- \n${BUILD_LOG, maxLines=50, escapeHtml=false}',
                to: "${EMAIL_ADDRESSES}",
                subject: 'Pipeline Execution  Failed: $PROJECT_NAME - #$BUILD_NUMBER'

The problem seems that from the post you only can call an expected function of
type: always, changed, fixed, regression, aborted, success, unsuccessful, unstable, failure, notBuilt, cleanup.

Is there another way to accomplish this to write a more clean Post section?


Thanks
Regards

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/ea74e553-88a7-4925-ade2-27462aaceab1n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/c45f0c38-08e9-4b23-bb21-ed4b47179090n%40googlegroups.com.