HariHaravelan
HariHaravelan

Reputation: 1057

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

Upvotes: 0

Views: 414

Answers (2)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35504

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.

Upvotes: 1

Miao Tian-MSFT
Miao Tian-MSFT

Reputation: 5632

If you are using the yaml pipeline, you can use the parameters in pipelines to decide the variables value.

Sample yaml:

trigger:
- none

pool:
  vmImage: windows-latest

parameters:
  - name: environment
    type: string
    displayName: environment value
    default: dev
    values:
      - dev
      - staging
      - master


variables:
  - name: app.imagedir
    ${{ if eq(parameters.environment, 'dev') }}:
      value: "E:\\images"
    ${{ if eq(parameters.environment, 'staging') }}:
      value: "D:\\images"
    ${{ if eq(parameters.environment, 'master') }}:
      value: "C:\\images"
  - name: app.backupdir
    ${{ if eq(parameters.environment, 'dev') }}:
      value: "E:\\backup"
    ${{ if eq(parameters.environment, 'staging') }}:
      value: "D:\\backup"
    ${{ if eq(parameters.environment, 'master') }}:
      value: "C:\\backup"
  - name: installdir
    ${{ if eq(parameters.environment, 'dev') }}:
      value: "C:\\Program Files\\IBM\\WebSphere\\"
    ${{ if eq(parameters.environment, 'staging') }}:
      value: "C:\\IBM\\WebSphere\\"
    ${{ if eq(parameters.environment, 'master') }}:
      value: "C:\\IBM\\WebSphere\\"
steps:
- script: echo This is ${{ parameters.environment }} environment. app.imagedir is $(app.imagedir). app.backupdir is $(app.backupdir). installdir is $(installdir).
  displayName: 'show ${{ parameters.environment }} environment variables'

If you are using the classic release pipeline, you can set different variable value for different stages.

enter image description here enter image description here

Upvotes: 1

Related Questions