Jenkins Pipeline: How to write UTF-8 files with writeFile?

35,505

Your code looks correct. See that ANSI and UTF-8 are the same if you are using just non accented chars and numbers. Try to have some accented letters (áéíóúç) in your file that the editor will probably recognize it as an UTF-8 file.

You must use the Jenkins pipeline writeFile function. This is a special Jenkins method to write files inside your workspace. The default Java File objects won't work.

To specify the encoding, you must use named parameters. Here is a an example:

writeFile(file: "filename.txt", text: "áéíóú", encoding: "UTF-8")

This will create at the root of your workspace, a file named filename.txt with "áéíóú" as content and encoded as UTF-8.

BTW, if you have full control of the file you must search and replace, consider using Groovy's builtin SimpleTemplateEngine

Share:
35,505
Hoss
Author by

Hoss

Updated on October 24, 2020

Comments

  • Hoss
    Hoss over 3 years

    I'm hoping this is not a bug and that I'm just doing something wrong. I have a Jenkins (v2.19.1) Pipeline job and in it's groovy script, I need to search and replace some text in an existing text file, on a Windows node.

    I've used fart.exe and powershell to do the search and replace, but I would really like to do this with just the groovy in Jenkins and eliminate dependency on fart/powershell/etc. and make this code more reusable on both linux and windows nodes.

    After much googling and trying various approaches, the closest I got was to use readFile and writeFile. However, I've not been able to get writeFile to create a UTF-8 file. It creates an ANSI file even when I specify UTF-8 (assuming I'm doing it correctly).

    Here's what I have so far...

    def fileContents = readFile file: "test.txt", encoding: "UTF-8"
    fileContents = fileContents.replace("hello", "world")
    echo fileContents
    writeFile file: "test.txt", text: fileContents, encoding: "UTF-8"
    

    I've confirmed with multiple text editors that the test.txt file is UTF-8 when I start, and ANSI after the writeFile line. I've tried all combinations of including/not-including the encoding property and "utf-8" vs "UTF-8". But in all cases, the file is written out as ANSI (as reported by both Notepad++ and VS Code). Also, a question mark (HEX 3F) is added as the very first character of the file.

    The echo line does not show the extra 3F character, so it seems the issue is in the writeFile line.

  • Prathamesh dhanawade
    Prathamesh dhanawade over 4 years
    You can also use variables like "writeFile file: "${outFileDest}" text: "${output}"
  • J D
    J D over 3 years
    No need for string interpolation if the whole content is what's behind the variable: writeFile([file: fileName, text: contents]). Unless you combine raw string with a avariable: writeFile([file: "/tmp/${fileName}", text: "Contents: ${contents}"]).