简体   繁体   中英

Programatically set app setting / env variable during Azure (Kudu) deployment

Summary

I'm trying to deploy an MVC5 web app to Azure. I need to set WEBSITE_NODE_DEFAULT_VERSION programatically to ensure all configuration is atomically contained in the repo. When I try to set that app setting / env variable in .deployment or deploy.cmd , it gets ignored by the deployment. Why?

Background

My web app uses bower for client-side libraries, and a simple gulp script to place the minimized libs in a target folder. My cshtml files then consume them from said target folder.

Per this comment , I've brought down the deployment script ( .deployment and deploy.cmd ) from Azure and tweaked it to install bower.

Then download your custom deployment script. if you go to https://.scm.azurewebsites.net then click on Tools -> Download custom deployment script or just download it from D:\\home\\deployment\\tools

My research then showed that npm is available by default in Azure web app deployments, and the bower package is pre-installed, but gulp isn't. So I need to add 3 custom commands to the deployment scripts:

  1. npm install (required to make gulp available)
  2. bower install (pulls down the client-side libs)
  3. gulp (pipelines the client-side libs)

Per this question , The problem I'm facing is that node (and therefore npm) being used are an old version. The npm install command is resulting in filenames that are too long, which is a known issue in older versions of npm.

Per this set of Kudu runtime settings , I'm trying to set the WEBSITE_NODE_DEFAULT_VERSION to 6.7.0 (the latest at the time of this question), because that would ensure that the latest npm also runs.

Here's where my problem occurs. In deploy.cmd , before running npm install , I add a line set WEBSITE_NODE_DEFAULT_VERSION = 6.7.0 (I've tried variations, including with quotes, without spaces around the equal sign, with setlocal , etc.) I echo %WEBSITE_NODE_DEFAULT_VERSION% on either side of setting the variable. The output before and after is always 4.4.7 .

I tried it in .deployment as well, to no avail. I even tried changing the position (sometimes before command = deploy.cmd and sometimes after).

From what I can decipher off the net, at least one of my methods above should work... Why can't I set this app setting / env variable in the deployment script?

Update

According to this question , I can't set the app settings in the .deployment file. This differs from other articles on the net, but it still doesn't explain why SET isn't working in the deploy.cmd file.

Files

.deployment

[config]
;Change node version to change npm version to avoid long-file-name situations
WEBSITE_NODE_DEFAULT_VERSION = 6.7.0
command = deploy.cmd

deploy.cmd

(REDACTED - default stuff)
echo :: 4. NPM Install (borrowing from https://stackoverflow.com/questions/39480274/how-do-i-run-gulp-js-in-my-asp-net-core-project-when-deploying-to-azure-using-gi)
IF EXIST "%DEPLOYMENT_TARGET%\package.json" (
  :: Set the node version. Tried this in .deployment, but that didn't work.
  :: HELP! Why isn't this working???
  echo %WEBSITE_NODE_DEFAULT_VERSION% before set
  :: Output: "4.4.7 before set"
  SET WEBSITE_NODE_DEFAULT_VERSION=6.7.0
  echo %WEBSITE_NODE_DEFAULT_VERSION% after set
  :: Output: "4.4.7 after set"
  pushd "%DEPLOYMENT_TARGET%"
  call :ExecuteCmd npm install
  IF !ERRORLEVEL! NEQ 0 goto error
  popd
)

echo :: 5. Bower Install (borrowing from https://stackoverflow.com/a/28591913/1876622)
IF EXIST "%DEPLOYMENT_TARGET%\bower.json" (
  pushd "%DEPLOYMENT_TARGET%"
  call :ExecuteCmd bower install
  IF !ERRORLEVEL! NEQ 0 goto error
  popd
)

echo :: 6. Run Gulp (borrowed from https://blogs.msdn.microsoft.com/azureossds/2016/08/12/run-npm-bower-composer-gulp-grunt-in-azure-app-services-during-deployment/)
IF EXIST "%DEPLOYMENT_TARGET%\gulpfile.js" (
  pushd "%DEPLOYMENT_TARGET%"
  call :ExecuteCmd gulp
  IF !ERRORLEVEL! NEQ 0 goto error
  popd
)

Turns out that when I created the Azure web app, it pre-set the WEBSITE_NODE_DEFAULT_VERSION in the portal (portal.azure.com). So the UI was superseding my programmatic requests to set the app setting.

After deleting that app setting in the portal (below is a screenshot after removing the setting), I tried deploying again by setting the env var in the .deployment file only... success!

在此处输入图片说明

.deployment

[config]
;Change node version to change npm version to avoid long-file-name situations https://blogs.msdn.microsoft.com/azureossds/2016/04/20/nodejs-and-npm-versions-on-azure-app-services/
;Make sure to delete the value in portal.azure.com > Application settings > App settings... because that will override this
WEBSITE_NODE_DEFAULT_VERSION = 6.7.0
command = deploy.cmd

The reason why comes from the CMD.EXE documentation.

Delayed environment variable expansion is NOT enabled by default.

/V:ON Enable delayed environment variable expansion using ! as the delimiter. For example, /V:ON would allow !var! to expand the variable var at execution time. The var syntax expands variables at input time, which is quite a different thing when inside of a FOR loop.

Because WEBSITE_NODE_DEFAULT_VERSION is already set it doesn't re-intreprete it when executing the echo statements. Add this around the top SETLOCAL ENABLEDELAYEDEXPANSION then try echo !WEBSITE_NODE_DEFAULT_VERSION! after set echo !WEBSITE_NODE_DEFAULT_VERSION! after set instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM