简体   繁体   English

Jenkins管道:使用变量模板化文件

[英]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. 我有一个处理模板文件(例如XML文件)的管道作业,需要在使用渲染文件之前用作业参数替换文件中的一些变量,但我现在似乎无法找到任何干净的东西,现在我只是使用shell脚本和sed逐个替换每个变量。

Here is an example XML template file : 这是一个示例XML模板文件:

<?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 . 我希望我的模板文件中的“变量”替换为我的作业参数$property1$property2$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 ? ...但我觉得它很难看......詹金斯有什么东西用来模板文件,例如Jinja2 (或任何模板框架)会做什么吗?

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): resources/report.txt.groovy (这是我的模板文件):

Hello from ${job}!

vars/helpers.groovy : 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: 这将生成存储在output变量中的字符串,其中包含以下内容:

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. 其中SIS Unix Automation Testing » myjob » master是我的Multibranch Pipeline作业的全名。

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. 当然,您可以使用此变量的内容执行任何操作,例如将其写入文件或通过电子邮件发送,您可以使用xml文件模板或任何所需的文件类型,而不仅仅是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. 请注意,您需要禁用沙箱或批准/白名单脚本才能使用此方法,否则将阻止StreamingTemplateEngine某些内部功能。

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). 流模板引擎的模板格式如下: ${variable}$VARIABLE形式的任何内容都将直接替换, <% %> / <%= %>语法可用于嵌入scriptlet(例如循环或if语句)(类似于ERB模板)。 The docs for StreamingTemplateEngine are available here . StreamingTemplateEngine的文档可在此处获得

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. 如果您只需要XML文件的东西,那么Jenkins的config-file-provider插件可以正常工作但是对于我的用途它是有限的。 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 ). 然而问题是令牌选项针对所有或没有令牌(并且硬编码以寻找$ {VAR}或$ 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. 因此,如果您的所有令牌都可以在jenkins环境中使用,那么您会没事的,但如果您有自定义的东西,那么它将导致失败。 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.... 如果我想生成一个bash脚本,上面的代码将会工作....

VAR = ${BUILD_URL}
echo $VAR

This will fail because it cannot find a value for $VAR. 这将失败,因为它无法找到$ VAR的值。 Escaping the $ does not help so I'm not sure what to do in my case. 逃避$并没有帮助所以我不知道该怎么做我的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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