Running Groovy in Email Extension Plugin Jenkins

404 views Asked by At

I need to create a url and send a mail from the Jenkins Email Plugin. I simply need the filenames to creat the url. The files are in the workspace itself. After searching around I found that only groovy script can be used in the email body.

My attempt was this, I created a folder called email-template in the Jenkins folder where it looks for email template. Ref . In the email body, I called it like this

${SCRIPT, template="email.groovy"}

This is the script

import groovy.io.FileType

File folder = new File('C://Tosca_Projects//OneDrive//ToscaPDFLogs')

folder.eachFileRecurse FileType.FILES,  { file ->

    
    if (!file.name.endsWith(".pdf")) {
        println "Processing file ${file.absolutePath}"
    }
}

This gives me an error

Exception raised during template rendering: No such property: file for class: SimpleTemplateScript13

Info:- When I run a build step 'run groovy script' ..it works fine. One other hint is..when I change the code to any other groovy code..it starts printing the code itself. For e.g import groovy.io.FileType new File('C://Tosca_Projects//OneDrive//ToscaPDFLogs').traverse(type: groovy.io.FileType.FILES) { it -> println it }

It will send the script text in the email.

1

There are 1 answers

0
Iyad Omry On

I have the following example may help you to get the file name

import groovy.io.FileType

def list = []

def dir = new File("/home/multi_files/files")
dir.eachFileRecurse (FileType.FILES) { file ->
  list << file.getName()
}

list.each {
  println it
}

result 1.txt 2.txt 3.txt Result: [1.txt, 2.txt, 3.txt]