6
votes

Is it possible for Azure DevOps to update part of my code when release kicks in?

For example, I have a setting file inside my react app. This setting file has export const ISPROD = false in it. I need Azure DevOps to change this false value to true before it builds the react app and deploys it. Is that possible?

Note: My Server build is Linux.

4

4 Answers

4
votes

You could update your code in your javascript files using the "Replace Tokens" task e.g.

- task: replacetokens@3
    inputs:
      targetFiles: "yourJavascriptFile.js"
      encoding: "auto"
      writeBOM: true
      verbosity: "detailed"
      actionOnMissing: "warn"
      keepToken: false
      tokenPrefix: "#{"
      tokenSuffix: "}#"
    displayName: Perform variable substitution in javascript file file

You'd add this task before the task you use to build your application.

In your javascript file you would write the variables to be replaced as e.g.

export const ISPROD = #{IS_PROD}#

This task when run would then replace "#{IS_PROD}#" with your Azure Devops variable named "IS_PROD" with it's value set as true.

3
votes

Since you're on linux you can just add the bash task or shell script task to your build and add an inline script or path to a script in your repo that does the setting update.

You'll need to take a look at the available environment variables in your pipeline that you can use in your script to access the working directory of your code.

You can optionally specify conditions under which the task runs. For example, I do something very similar when versioning our components for release, which are only done during builds triggered by a git tag.

2
votes

Here's a free Visual Studio Marketplace Pipeline task that will do the trick: Replace Text in Source Files

This one will also work: RegexReplace Build Task

1
votes

If you want to make a custom solution following might help full to you

  1. Go with Bash Task in azure pipeline
  2. In Yaml steps define inline Ex: steps:
    • task: Bash@ReplaceTextInFile inputs: targetType: 'inline' script: bashscript
  3. use sed command to replace text in your file.