A mio parere, un plug-in di Jenkins consente di creare un'interfaccia semplice per un comportamento ampio e complesso, che di solito implica un qualche tipo di integrazione. Quello di cui hai probabilmente bisogno è la funzione Libreria condivisa:
Da link :
As Pipeline is adopted for more and more projects in an organization, common patterns are likely to emerge. Oftentimes it is useful to share parts of Pipelines between various projects to reduce redundancies and keep code "DRY".
Pipeline has support for creating "Shared Libraries" which can be defined in external source control repositories and loaded into existing Pipelines.
A Shared Library is defined with a name, a source code retrieval method such as by SCM, and optionally a default version. The name should be a short identifier as it will be used in scripts.
La libreria condivisa è essenzialmente un repository git (o altro) che Jenkins può eseguire. Il codice all'interno di tale repository può essere chiamato da più pipeline.
Ho avuto lo stesso problema di te e ho usato una libreria condivisa per risolverlo, creando un metodo Groovy personalizzato per incapsulare e condividere la logica intorno alla notifica via email:
#!/usr/bin/env groovy
/*
This script sends notification of pipeline results
to the email addresses listed in emailsToNotify.
Email is sent if the build fails, or if
the build succeeds, and the previous build failed.
*/
// send notifications based on build result
import org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper
def call(Map<String, Object> options) {
String[] emailsToNotify = options."emailsToNotify"
RunWrapper currentBuild = options."currentBuild"
String SUCCESS = options."success"
String FAILURE = options."failure"
// name of the upstream application that triggered the pipeline
String appName = options."appName"
Boolean currentBuildFailed = currentBuild.result != SUCCESS
Boolean previousBuildFailed = false
// ensure there is a previous build
if (currentBuild.getPreviousBuild()) {
previousBuildFailed = currentBuild.getPreviousBuild().getResult().toString() != SUCCESS
}
// only send email on failure, or on success if the previous build failed
Boolean sendEmail = currentBuildFailed || (!currentBuildFailed && previousBuildFailed)
if(sendEmail) {
String jobId = "'${env.JOB_NAME} [${env.BUILD_NUMBER}]'".toString()
String message
if (previousBuildFailed && !currentBuildFailed) {
message = "CHANGED FROM ${FAILURE} TO ${SUCCESS}".toString()
} else {
message = currentBuild.result
}
String subject = "${message}: ${appName} at ${jobId}".toString()
String details = """\
<p>${message}: ${appName} at ${jobId}:</p><p>Check console output at "
<a href='${env.BUILD_URL}'>${appName} ${jobId}</a>" </p>
""".toString()
emailext(
to: emailsToNotify.join(","),
subject: subject,
body: details
)
}
}