projectkudu / kudu

Kudu is the engine behind git/hg deployments, WebJobs, and various other features in Azure Web Sites. It can also run outside of Azure.
Apache License 2.0
3.12k stars 652 forks source link

Post Deployment Action - Set Environment Variable From File Content #1934

Closed aaronhoffman closed 8 years ago

aaronhoffman commented 8 years ago

I have a post-build event action set up in Visual Studio for my web project that takes the current git hash for HEAD and stores it in a local file called version.txt at the root of the web project.

On a successful deployment to Azure, I would like the contents of the version.txt file (the git hash) to become an environment variable with key CURRENT_VERSION.

I have tried two methods and they both do not appear to be working, but I am also having difficulty debugging them.

  1. I tried the post deployment hook method by placing a script in the /site/deployments/tools/PostDeploymentActions directory as this wiki states https://github.com/projectkudu/kudu/wiki/Post-Deployment-Action-Hooks. I had to create the PostDeploymentActions folder myself as it did not already exist, and those scripts do not appear to be being run (I am not aware of where the deployment logs are stored that that wiki page references to verify). I assume they are not running because one of the scripts I tried, named aaron.cmd, contained one line SET CURRENT_VERSION=HELLO and I am attempting to verify that envar was set by using the debug console https://myapp.scm.azurewebsites.net/DebugConsole. If I execute echo %WEBJOBS_STOPPED% I get a value (that is being set via the portal app settings page), but if I execute echo %CURRENT_VERSION%, I get nothing.
  2. I also tried setting the POST_DEPLOYMENT_ACTION app setting as described here: http://stackoverflow.com/questions/26350201/how-to-add-a-custom-post-deployment-script-to-azure-websites, but that also didn't appear to run.

I verified that the aaron.cmd file was being deployed to the app service. I even manually executed the aaron.cmd file via the DebugConsole to ensure that it could run without error and it did (and afterward performing an echo %CURRENT_VERSION% returned HELLO)

I have not tried the method described here: https://azure.microsoft.com/en-us/blog/new-azure-web-sites-feature-configurable-environment-variables-2/ but I wasn't sure I could use an XDT transform to read the contents of a file...

What would be the recommended way to get the contents of the version.txt file to be set as an environment variable using a post deployment hook? (thinking about just dropping something in the startup of the web project, but I'd prefer not to have to do that)

davidebbo commented 8 years ago

I think too many things are getting raised in this one issue. Can we simplify and start with just: how to successfully run a Post Deployment action, leaving the details of what you do in that action off the table?

aaronhoffman commented 8 years ago

Yes, that would be fine.

What is the preferred method of running custom scripts as a post deployment action? If it is by placing the scripts in the /site/deployments/tools/PostDeploymentActions directory, what is the best way to debug an issue if those do not appear to be running?

davidebbo commented 8 years ago

Start simple. e.g.

In the deployment details, you should see something like this:

image

And if you click View Logs, you'll see the output of the batch file.

Does that work for you?

aaronhoffman commented 8 years ago

Are scripts within the site\deployments\tools\PostDeploymentActions directory only activated on a git deployment? If so, that could be the issue, we are using webdeploy.

Are any Post Deployment Action hooks available for webdeploy?

davidebbo commented 8 years ago

Yes, this only happen during Kudu based deployments. msdeploy works quite differently. Unfortunately, at this time there is no great equivalent for msdeploy.

NehaOberoi92 commented 7 years ago

Hi,

I am facing same issue as stated above. I am using TFS source control and visual studio online 2015 for continuous integration(build definition). In build definition deploying package on app service using : Azure Deployment task.

davidebbo commented 7 years ago

@NehaOberoi92 Please see http://stackoverflow.com/questions/41561899/add-post-deployment-script-on-azure-websiteapp-service/41574522#41574522