Skip to main content

Token replacement in Jenkins

Hi Folks,
Today we are configuring our Jenkins to replace tokens in code before deployment. Please follow below steps.

Create Project in git (github or any other git providing tool)

  1. Create a project which will contains all token values. in my case here is public demo project.
  2. Deploy it in Jenkins as freestyle project , select git in Source Code Management and paste your git repository URL. If it is public then you don't need credentials to enter otherwise you have to. Build your project and check console output. Copy directory structure ( ref: below console snapshot). This is the physical directory structure of our deployed code in Jenkins hosted docker/OS.

Configure project (in which token need to be changed)

  1. Create build.xml in your project root and past below code
    <project name="demo-app-tokenparser" default="useregex" basedir=".">
     <target name="useregex">
      <property file="/var/jenkins_home/workspace/ant-properties-public/dev.properties"/>
      <replace  file="/var/jenkins_home/workspace/demo-app-tokenparser/application.properties" token="@mysql.host@" value="${mysql.host}" />
      <replace  file="/var/jenkins_home/workspace/demo-app-tokenparser/application.properties" token="@mysql.db@" value="${mysql.db}" />
      <replace  file="/var/jenkins_home/workspace/demo-app-tokenparser/application.properties" token="@mysql.username@" value="${mysql.username}" />
      <replace  file="/var/jenkins_home/workspace/demo-app-tokenparser/application.properties" token="@mysql.password@" value="${mysql.password}" />
     </target>
    </project>
    
    Below important to be noted
    1. This is the directory structure earlier we copied in above steps. Here we get the source file which contains token values
      <property file="/var/jenkins_home/workspace/ant-properties-public/dev.properties"/>
    2. Below line show the target file in which token will be replaced (we will create application.properties in next step). Also mysql.host is the token to be replaced.
      <replace  file="/var/jenkins_home/workspace/demo-app-tokenparser/application.properties" token="@mysql.host@" value="${mysql.host}" />
  2. create application.properties in app root as in above build.xml we are using it on root (you can place any where in your project and just correct the directory structure replace tag in build.xml.
    wd.mysql.host=@mysql.host@
    wd.mysql.db=@mysql.db@
    wd.mysql.username=@mysql.username@
    wd.mysql.password=@mysql.password@
    @mysql.host@ This will be replaced when building in Jenkins
  3. Deploy it in Jenkins and first build step as Invoke Ant
  4. Build your project and check console if for errors. when build is success go to Workspace and open application.properties and check if the token is changed. In my case check is is changed as below:
    wd.mysql.host=localhost
    wd.mysql.db=devopsdemo
    wd.mysql.username=root
    wd.mysql.password=mypassword

    Note: For Invoke Ant to work you have to install Ant plugin for Jenkins.

Comments


  1. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me. Great Article on jenkins
    DevOps Training in Chennai

    DevOps Online Training in Chennai

    DevOps Training in Bangalore

    DevOps Training in Hyderabad

    DevOps Training in Coimbatore

    DevOps Training

    DevOps Online Training

    ReplyDelete

Post a Comment

Popular posts from this blog