Search code examples
azure-devopsazure-pipelinesazure-pipelines-build-task

Managing Environment-Specific Property Files across stages with Azure DevOps Pipeline Overrides them on stages


I am facing challenges maintaining property files across our repository's various development stages.

Within our legacy monolith, all properties and config files reside inside the repository.

Our repository has three stages for development, staging, and master environments, obviously with varying property values across these stages.

For example, application.properties file contains different values for each stage:

dev

app.imagedir=E:\images
app.backupdir = E:\backup   
installdir=C:\Program Files\IBM\WebSphere\  

staging

app.imagedir=D:\images
app.backupdir = D:\backup   
installdir=C:\IBM\WebSphere\    

master

app.imagedir=C:\images
app.backupdir = C:\backup   
installdir=C:\IBM\WebSphere\    

My goal is to maintain a unified application.properties file within the repository across all stages, yet allow these values to be overridden when the pipeline runs. How can I implement such behavior in Azure DevOps pipelines?

I'm looking for best practices, or methods to manage these environment-specific configurations


Solution

  • How can I implement such behavior in Azure DevOps pipelines?

    I suggest that you can use the Replace Tokens task from the Replace Tokens Extension.

    Here are the steps:

    1.Set the #{varname}# (e.g. #{app.imagedir}# ) in the application.properties file

    For example:

    app.imagedir=#{app.imagedir}#
    app.backupdir =#{app.backupdir}# 
    installdir=#{installdir}#
    

    You can keep the format above in the application.properties file.

    2.Set Pipeline variables in different stages and use the Replace Token task.

    enter image description here

    enter image description here

    When you run the pipeline, the replace token task will automatically update value in application.properties file according to the variables set in the corresponding stages.

    If you are using YAML Pipeline, you can define the variables at stage level.

    For example:

    stages:
    - stage: dev
      variables:
        app.imagedie: E:\images
        app.backupdir: E:\backup   
        installdir: C:\Program Files\IBM\WebSphere\
      jobs:
      - job: 
        steps:
        - task: replacetokens@5
          inputs:
            rootDirectory: 'folderpath'
            targetFiles: 'application.properties'
            encoding: 'auto'
            tokenPattern: 'default'
            writeBOM: true
            actionOnMissing: 'warn'
            keepToken: false
            actionOnNoFiles: 'continue'
            enableTransforms: false
            enableRecursion: false
            useLegacyPattern: false
            enableTelemetry: true
      
    

    For more information, you can refer to my ticket.