简体   繁体   中英

Jenkins pipeline : templating a file with variables

I have a pipeline job that treats a template file (eg 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 ?

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 .

If you just need something for XML files then the config-file-provider plugin for Jenkins will work fine but for my uses it is limited. It provides a centralized location for template files (or you can point to a file in the filesystem) and optionally replace tokens. The problem however is that the token option targets all or no tokens (and hardcoded to look for tokens in the format of ${VAR} or $VAR ). So if all your tokens are available in the jenkins environment then you'll be fine but if you have something custom then its going to cause a failure. So for example:

This is my instruction file. This file is saved to ${BUILD_URL}/file.txt

The above will work however if i want to generate a bash script....

VAR = ${BUILD_URL}
echo $VAR

This will fail because it cannot find a value for $VAR. Escaping the $ does not help so I'm not sure what to do in my case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM