10

I'm struggling to understand how to run a powershell script (it just runs an exe) on the App Service after deployment using Azure Devops.

FYI - the exe just reads some config info in the web.config and does some database updates. So it needs access to the transformed web.config and the SQL Server database that the App Service has.

I've tried adding a Powershell step using a Release Pipeline deployment task, but it seems to only run on the Agent, which does not have the context that the exe needs.

I can run the script manually using the Kudu command line window tool, but I need to automate this thing.

I've seen a few SO articles like this: How to add a custom post deployment script to azure websites? or Kudu post-deployment commands not running but I think that only works when you use the inbuilt deployment mechanism for App Services. ie - I couldn't get it to trigger the script in post-deploy from Azure Devops.

Deploy Azure App Service task

riQQ
  • 9,878
  • 7
  • 49
  • 66
Lanceomagnifico
  • 1,293
  • 15
  • 22
  • 9
    If you're using the `Azure App Service Deploy` task, expand `Post Deployment Action`, there's an option to run a script (or inline write one) there. That is executed as part of the deploy and should allow you to do what you want. It's run at the wwwroot folder. – Daniel Morritt Dec 21 '18 at 12:09
  • 1
    It's a 'Deploy Azure App Service' task. As part of the Release pipeline. – Lanceomagnifico Dec 21 '18 at 20:46
  • Yes, you're fine then, if you haven't already click the task, find the bit I mentioned above and put your code in there, or run it from the script (remember it will need to be included in your deployed website then). – Daniel Morritt Dec 22 '18 at 16:37
  • 1
    The 'Deploy Azure App Service' has properties. One of the properties is a 'Post Deployment Action' that allows you to run a script file. However, the script location is in the Linked Artifacts > project > drop folder. This folder only contains a few deployment configuration files and the zip file containing all the website files. The zip file contains the script I need to run. ie the location isn't the website, but the pre-deploy file drop. See: https://www.screencast.com/t/vDeR1yPCN – Lanceomagnifico Dec 22 '18 at 21:33
  • If the script is in your solution, set it to "content" or "copy always" so it gets included in the zip, then it will be copied to the site when the zip is deployed. – Daniel Morritt Dec 25 '18 at 22:19
  • Thanks @DanielMorritt - but you misunderstand. During that post-deployment action, it doesn't have ANY of the solution files available! They are all in a big zip drop file. In fact, it's not even the POST deployment state of the files, it's the PRE-DEPLOYMENT state... but the job says 'Post deployment'. Argh. – Lanceomagnifico Dec 27 '18 at 16:43
  • If the zip has been uploaded to your site correctly, and contains the script you want to run (so it's in the website, you can check in Kudu), then you just need to run it using the post deployment action. I know this works as we use this to replace the applicationHost.xdt ourselves (it's one level above the wwwroot folder so can't do it as part of the normal deploy). – Daniel Morritt Dec 29 '18 at 14:03
  • No, that step only shows the zip, not the files in it! Even though it's a 'post deployment' step, it only allows you to choose 'pre deployment' files. I'm guessing because it's under the 'Run on Agent' block. I need a 'Run on Host' block. – Lanceomagnifico Dec 29 '18 at 20:48
  • 3
    It literally does what you want: https://github.com/Microsoft/azure-pipelines-tasks/blob/master/Tasks/AzureRmWebAppDeploymentV4/README.md (down under "Deployment script"). If you click the button then yes, it will only show you the files in the drop folder, just enter the script name manually then, as I said it's executed from the wwwroot folder. – Daniel Morritt Jan 02 '19 at 22:44
  • 1
    @DanielMorritt you should post this as an Answer. I'd upvote it. – Vince Bowdren Nov 28 '21 at 10:40
  • Looks like the answer is here as a comment. @DanielMorritt should add it as an answer and I'd upvote. – Joshua K Sep 16 '22 at 16:00

1 Answers1

1

This is in the comments above, but if people find it useful, the answer to this is

Using the Azure App Service Deploy task, expand Post Deployment Action, there's an option to run a script (or inline write one).

This will be executed as part of the app service deployment and should allow you to do what you want. It's run at the wwwroot folder so the files need to be present in your actual deployment (zip/folder).

Daniel Morritt
  • 1,787
  • 17
  • 25