Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 211
Hoss Avatar asked Feb 23 '17 14:02

Hoss


People also ask

How do you write Jenkins pipeline script?

To create a simple pipeline from the Jenkins interface, perform the following steps: Click New Item on your Jenkins home page, enter a name for your (pipeline) job, select Pipeline, and click OK. In the Script text area of the configuration screen, enter your pipeline syntax.

How do I create a Jenkins pipeline file?

To get started quickly with Pipeline: Copy one of the examples below into your repository and name it Jenkinsfile. Click the New Item menu within Jenkins. Provide a name for your new item (e.g. My-Pipeline) and select Multibranch Pipeline.

How do I catch exceptions in Jenkins pipeline?

try/catch is scripted syntax. So any time you are using declarative syntax to use something from scripted in general you can do so by enclosing the scripted syntax in the scripts block in a declarative pipeline. So your try/catch should go inside stage >steps >script.


1 Answers

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

like image 149
neves Avatar answered Sep 17 '22 06:09

neves