Changing a value in a .js file

Hi,

I have a JS file I use in my application and I would like to update a value within this during deployment.

eg: var baseUrl = ‘http://localhost:2036/api’;

Can this be done and what is the bast way to do it?

Thanks,

Karl

Hi Karl,

Thanks for getting in touch! You can use the substitute variables in files feature and define the name of the js file you want to have variables substituted in, then use the Octopus variables syntax.

Hope that helps!
Vanessa

Using these variables in my code will stop it working during development. I want to use this to update the values at deployment.

Does this mean I need to create another file that has the variables and then add a powershell script to the proccess that replaces the original file with the file holding the variables?

Thanks

Hi Karl,

In that case there is a library script that might be of help: https://library.octopusdeploy.com/#!/step-template/actiontemplate-file-system-regular-expression-find-and-replace

Vanessa

Hi,

I have been tryign to get this working, but it needs the file path. My project is being deployed via a NuGet package, which is output by TeamCity.

Is this still the way I should be changing values in Javascript files or is there another way to do it?

Many thanks,

Karl

Hi Karl,

There shouldn’t be any reason you cant use a system variable to get the path, such as Octopus.Action[_name_].Output.Package.InstallationDirectoryPath as described here: http://docs.octopusdeploy.com/display/OD/System+variables

The three ways to replace variables are:

  1. Substitute variables in files
  2. The script i’ve linked
  3. Write a powershell script

Vanessa

Thank you Vanessa, That is exactly what I was looking for.

Kind regards,

Karl

Colleague of mine came up with this requireJS define:

define("settings",[], function() {
   "use strict";

   function undefinedIfTemplate(value) {
       if (value.substr(0, 2) === "#{") {
           return;
       }
       return value;
   }

   return {
       BackendUrl: undefinedIfTemplate("#{BackendUrl}") || "http://localhost/api"
   };
});

I just had the same problem and wrote a post about the solution I came up with. Hope it’s helpful to someone…