Josh Smeaton
Josh Smeaton

Reputation: 48720

Generating a file based on build parameters with Jenkins

I want to generate an XML file that I will feed into a deployment tool once a build is complete. The template of the XML file is like this:

<Settings>
    <Version>${BUILD_NUMBER}</Version>
    ..
</Settings>

The template exists within my project (a .NET solution), and is used to deploy the project as a click once application. I'm trying to figure out how I'd get Jenkins to push the build number into a template file, so then I can feed it to the application using a command line Exec task.

Do any plugins exist that are capable of generating files from templates? What other avenues can I use to accomplish this task?

Upvotes: 2

Views: 3280

Answers (1)

Mike D
Mike D

Reputation: 6195

I found that Jinja2 works nicely for this. It has some powerful filters but can also handle basic templates. Incidentally, Jinja2 is the templating mechanism used by Ansible.

To install Jinja2 (assuming you have some python 2.6+ tools available):

pip install Jinja2

or

easy_install Jinja2

From this post, you can get a command line wrapper for the Jinja2 library which I show simply below:

curl -LO https://bitbucket.org/tebeka/pythonwise/raw/31769f04bac832f8d781827f55ad6cc4d4ef3c07/jj2.py
chmod u+x jj2.py

Then you setup your jinja template file (let's call it build-descriptor.j2):

<Settings>
    <Version>{{BUILD_NUMBER}}</Version>
    ..
</Settings>

and use this in your Jenkins job:

jj2.py -v BUILD_NUMBER=${BUILD_NUMBER} build-descriptor.j2 > build-info.xml

Upvotes: 3

Related Questions