-->

How to place Email-Ext groovy script on the jenkin

2020-07-23 00:27发布

问题:

I need to dynamically modify the notification e-mail recipients based on the build, so I'm using a groovy script. I want this script to be available to all jobs, so I want it to reside on the Jenkins file system and not within each project. It can be either in the recipients fields (using ${SCRIPT,...}) or in the pre-send script. A short (fixed) script that evaluates the master script is also good, as long it is the same for all projects.

回答1:

You should try Config File Provider plugin. It works together with the Credentials configuration of Jenkins.

  • Go to Manage Jenkins > Managed files
  • Click Add a new Config
  • Select Groovy file
  • Enter the contents of your script

Your script will now be saved centrally on Jenkins, available to master/slave nodes.

In your Job Configuration:

  • Under Build Environment
  • Check Provide Configuration file
  • Select your configured groovy File from dropdown
  • Set Variable with a name, for example: myscript

Now, anywhere in your job, you can say ${myscript} and it will refer to absolute location of the file on filesystem (it will be somewhere in Jenkins dir).



回答2:

My impression it that you would probably want to completely switch to Jenkins pipelines where the entire job is groovy file (Jenkinsfile) in the root of the repository.

Email-Ext already supports it even if it may be lacking some documentation. https://jenkins.io/doc/pipeline/steps/email-ext/