Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline : templating a file with variables

I have a pipeline job that treats a template file (e.g. an XML file) and needs to replace some variables from the file with job parameters before using the rendered file, but I can't seem to find anything clean to do that, for now I'm just using shell script and sed to replace each variable one by one.

Here is an example XML template file :

<?xml version='1.0' encoding='UTF-8'?>
<rootNode>
    <properties>
        <property1>${property1}</property1>
        <property2>${property2}</property2>
        <property3>${property3}</property3>
    </properties>
</rootNode>

I would like the "variables" in my template file to be replaced with my job parameters $property1, $property2 and $property3. Here is what I'm doing today :

sh  "sed -i 's/@property1@/${property1}/' '${templateFile}'" +
    "sed -i 's/@property2@/${property2}/' '${templateFile}'" +
    "sed -i 's/@property3@/${property3}/' '${templateFile}'"

... but I find it quite ugly... is there anything in Jenkins for templating files such as what Jinja2 (or any templating framework) would do ?

like image 782
Pom12 Avatar asked Aug 25 '16 14:08

Pom12


1 Answers

Here is the solution I found: I created a global shared library with the following files:

resources/report.txt.groovy (this is my template file):

Hello from ${job}!

vars/helpers.groovy:

import groovy.text.StreamingTemplateEngine

def renderTemplate(input, variables) {
  def engine = new StreamingTemplateEngine()
  return engine.createTemplate(input).make(variables).toString()
}

Then, in my Pipeline, I added the following step:

variables = [ "job": currentBuild.rawBuild.getFullDisplayName() ]
template = libraryResource('report.txt.groovy')
output = helpers.renderTemplate(template, variables)

This generates a string stored in the output variable with the following content:

Hello from SIS Unix Automation Testing » myjob » master #29!

where SIS Unix Automation Testing » myjob » master is the full name of my Multibranch Pipeline job.

Of course you may do anything you want with the content of this variable, like write it out to a file or send it in an email, and you can use xml file templates or whatever file type you want, not just txt.

Note that you will need to disable the sandbox or approve/whitelist scripts to use this approach, as some of the internals of StreamingTemplateEngine will be blocked otherwise.

The template format for the Streaming Template Engine is as follows: anything of the form ${variable} or $VARIABLE will get replaced directly and <% %>/<%= %> syntax can be used to embed scriptlets (such as loops or if statements) as well (similar to ERB templates). The docs for StreamingTemplateEngine are available here.

like image 50
jayhendren Avatar answered Nov 03 '22 13:11

jayhendren