How to read csv file in jenkins mail content
To read a CSV file in the Jenkins mail content, you can use the readCSV()
function in Groovy, which is a scripting language used in Jenkins. Here's an example of how you can do it:
- In your Jenkins job, go to the "Post-build Actions" section and click on "Add post-build action" and select "Send an email".
- In the "Email" section, click on the "Advanced" button and select "Use Groovy script" as the email content type.
- In the Groovy script, you can use the
readCSV()
function to read the CSV file. For example:import groovy.csv.CsvParser
// Set the path to your CSV file def csvFile = 'path/to/your/file.csv'
// Read the CSV file def csvData = new CsvParser().parse(csvFile)
// Loop through the CSV data and print it to the email csvData.each { row -> def emailContent = "" row.each { cell -> emailContent += cell + "\n" } println emailContent }
4. In the "Email" section, click on the "Send" button to send the email.
Note: Make sure to replace `'path/to/your/file.csv'` with the actual path to your CSV file.
Alternatively, you can also use the `readLines()` function to read the CSV file and then parse it manually. Here's an example:
```groovy
def csvFile = 'path/to/your/file.csv'
def csvData = readLines(csvFile)
def emailContent = ""
csvData.each { line ->
def cells = line.split(",")
cells.each { cell ->
emailContent += cell + "\n"
}
emailContent += "\n"
}
println emailContent
This will read the CSV file line by line, split each line into cells using the comma as a delimiter, and then print the cells to the email.
Make sure to adjust the script according to your specific CSV file format and requirements.