Variable Substitution of Properties File in Jenkins

Overview

This article describes the detailed steps to setup dynamic generation of property file having variable substitution capability for placeholders from Jenkins. This can be integrated with Jenkins pipeline.

Prerequisites

  • Jenkins

  • config-file-provider Jenkins plugin

Setting up the variable substitution of values in property file

Most common usecase in Jenkins jobs is parameterization of values by replacing placeholders. In this article we will explain how to use config-file-provider plugin to generate a property file on the fly.

For example, we need to generate an Asset Build Environment property file on the fly from Jenkins. Following is a sample.

sag.install.dir=C:\SoftwareAG
mws.build.encoding=UTF-8
enable.build.CC=true
enable.build.UniversalMessaging=true
enable.build.IS=true
localStore=..\\master_build\\repo
remoteStore=https://${domain}/integration/rest/internal/wmic-git/stage00-CCsolution-IS
message=Added new IS assets1
build.source.dir=..\\master_build\\source\\IS
build.output.dir=..\\master_build\\repo
username=user1
password=password
registryRoot=
fileCommand=transform-properties
tag=

 

Our requirement is to generate a property file by dynamically replacing placeholders. And these dynamic values will be sent through build parameters of Jenkins job.

Create a Managed file for properties

As we have already installed config-file-provider plugin, lets continue to create a managed file
From manage Jenkins > Click on Managed files

 

Click on Add new Config
From available option select properties file and provide an ID

 

Click Submit

Now we need to change the property values as a place holder which needs a variable substitution.

Updated property file will look like:

sag.install.dir=${ENV, var="installDir"}
mws.build.encoding=UTF-8
enable.build.CC=true
enable.build.UniversalMessaging=true
enable.build.IS=true
localStore=..\\master_build\\repo
remoteStore=https://${ENV, var="domain"}/integration/rest/internal/wmic-git/stage00-${ENV, var="SolutionName"}-${ENV, var="serverName"}
message=Added new IS assets1
build.source.dir=..\\master_build\\source\\IS
build.output.dir=..\\master_build\\repo
username=${ENV, var="username"}
password=${ENV, var="password"}
registryRoot=
fileCommand=transform-properties

 Now add this as a content of newly created managed file and also change the name of this file

 

Now let’s create a Jenkins job having installDir,SolutionName,serverName and user credentials as a build parameters.

Add build step: select provide configuration files as a build step and select created ABE_properties managed file. Notice that we have selected replace tokens option and set the target location of property file.

Running this job will create a new property file in the target location with variable substitution of placeholders.

Further Read:

To learn more about CI/DI check this article webMethods.io API Gateway - CI/CD Devops - Knowledge base - Software AG Tech Community & Forums