4

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.

David Rabinowitz
  • 29,904
  • 14
  • 93
  • 125

2 Answers2

3

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).

Slav
  • 27,057
  • 11
  • 80
  • 104
  • Hi guys, I tried exactly this. Unfortunately the script seems to be deleted before Email-Ext can access it. Any idea? 15:52:14 provisoning config files... -> 15:52:14 copy managed file [CPI_Emailscript_Groovy] to file:/tmp/config4504079314751031710tmp 15:52:14 [OIP_PI-CPI_uidt7054_11.0_ci] $ /bin/sh -x /tmp/hudson8833997127231887496.sh ... 15:52:19 Build step 'Execute shell' marked build as failure -> 15:52:19 Deleting 1 temporary files 15:52:19 Sending email for trigger: Always -> 15:52:19 groovy.lang.MissingPropertyException: No such property: config4504079314751031710tmp for class: Script1 – Beasly Apr 01 '16 at 12:45
  • 1
    @Beasly after the file is brought over the first time, trying copying it manually to workspace (as one of the first build steps), then let Email-Ext access the one from workspace. – Slav Apr 03 '16 at 04:53
  • Thank you for answering. Strange is that it doesn't recognize the object "msg" anymore, when it tries to execute the script. If I paste it directly into "Pre-send Script" it works. Do you have an idea here too? – Beasly Apr 04 '16 at 06:21
  • Seems not to be possible: http://stackoverflow.com/questions/36572245/email-ext-objects-in-groovy-script-as-file/36647733#36647733 – Beasly Apr 18 '16 at 07:20
0

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/

sorin
  • 161,544
  • 178
  • 535
  • 806