Azure Devops - How to run a powershell script on an App Service post-deploy? - azure

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.

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).

Related

Trying to get Azure Devops pipeline to run the app dll

I have azure devops publishing a dotnet core web app to an AWS server, but the app serilog logs are not kicking in because even though the DLLS etc are updated the myapp.dll is not re-started, so the logging setup code in program.cs is not executed.
If I run the app in powershell using "dotnet myapp.dll" then the logging file appears but that starts a seperate process on a different port... so it doesnt help.
The app runs under IIS on the server and Im not sure if this is a devops issue or something i need to do in IIS..
I added this as the last step to the pipeline but I get an error
You misspelled a built-in dotnet command.
You intended to execute a .NET Core program, but dotnet-.\myWork.dll does not exist.
You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
The pipeline doesn't start your application. Even if it worked, you would have an app running on the Agent that's executing the pipeline, not on your server.
If you want the app hosted on your server, I'd start with Publish an ASP.NET Core app to IIS tutorial.
Get it deployed on your server manually and ensure it's running correctly. But don't do right-click publish - use commandline all the way.
Once you know what are the necessary steps (and what might go wrong), you can start automating it.

Provision a VM as part of an Azure Devops build pipeline

I have a build pipeline that is working pretty well currently in Azure DevOps. As part of the pipline/build process, I create an artifact, which is published and reachable. After that, I'd like to do the following:
Create/Start Up a new VM (Windows)
Grab the now published artifact, unzip it and run the executable within
Run the integration tests
Close the VM
I've looked around the Azure documentation but cannot find much that discusses this sort of solution. Please help!
There is nothing built-in (like a readymade task create a vm), so you can use any way to create a VM in Azure. Azure powershell, Azure Cli, ARM Templates, SDK calls. whatever works for you.
You would need to open ssh\winrm to talk to that vm to deploy stuff to it. thats about it. You can find lots of examples on how to create a VM online. VSTS got tasks for Azure Powershell\Cli\ARM Tempaltes so you dont need to handle auth.
You can create a VM using ARM templates with the task 'Azure Resource Group Deployment'
With a separate task 'Powershell on target machine' you can run a powershell script on the target VM, if you put the downloading, unzipping and running of this exe in this script you should be able to perform the tasks you need.
You could also look into the 'invoke-azurermvmruncommand' powershell command, this allows you to run a powershell script in the vm. https://learn.microsoft.com/en-us/powershell/module/azurerm.compute/invoke-azurermvmruncommand?view=azurermps-6.11.0

Continuous Dilvery as Windows Service and Web Api using TFS Build on Azure VM

I have TFS 2015 and i was able to automated the build process from the branch and get the files from the drop folder as shown below:
It has release for multiple projects like Web API and Windows Service
I want Azure VM on which i want to automate the deployment process - continuous delivery.
Deploy the Web API on IIS on Azure VM
Deploy the Windows Services On Azure VM.
Run Scripts SQL.
I have credentials of Azure VM. How i can perform the three above steps.
I have worked on a similar problem in the past so can probably help you out (MSFT, if it helps).
Web Api on IIS on Azure VM
This is almost completely automated in the form of WinRM - IIS Web App Deployment task that you can find and add in your release definition. The link provides complete instructions on what parameters to provide and tweaks to be done for Azure VM compared to on-premise ones. There are a few prerequisites to running this task, like installing and configuring IIS on the VM which the documentation discusses in detail. As a necessary input to this task, you need to provide the web deploy package which I am assuming was generated as your build output. If not, you can refer to this SO post to get the required output. If you have parameters like connection strings that you wish to modify at deploy time, using a parameters.xml file in the above task.
Windows Service on Azure VM
There is no completely automated task for this requirement, but it is pretty straight-forward. It can be achieved by using the PowerShell on Target Machines task along with Azure File Copy task. For the first task, all that is required as input is the .exe of the windows service that you wish to deploy, which should be generated as the output of your build process (build artifacts). Much of the remote machine inputs for this task is similar to the previous one so you should not have any problem there. You will need to check-in the Powershell script that does the actual windows service installation, in your source code as part of the same windows service project (copy local = True). This will ensure that as the build output, you will have access to the powershell script which you can use in the second task. Azure File Copy is required to copy your powershell script to the Azure VM so that the Powershell task can execute it. Let's assume you copied the powershell script to a folder C:\Data\ on the Azure VM.
$serviceName = "MyWindowsService"
$exeFullName = "path\\to\\your\\service.exe"
$serviceDisplayName = "MyWindowsService"
$pss = New-Service $serviceName $exeFullName -DisplayName $serviceDisplayName
-StartupType Automatic
Add this content to the checked in powershell file and name it installWindowsService.ps1. Then in the powershell task provide the path of the powershell file to execute as C:\Data\installWindowsService.ps1.
Run SQL Scripts on Azure VM
I haven't personally worked on this so the best I can do is point you in the right direction. If you are using DACPAC for your SQL deployment, you can use the WinRM - SQL Server Database Deployment task. If you just intend to execute scripts, use the remote powershell task from above and refer this post that will help you with running SQL commands through powershell script
Seems you want the CD release process picks up the artifacts published by your CI build and then deploys them to your IIS servers/Windows Services on Azure VM.
If you've just completed a CI build, then you should create a new release definition that's automatically linked to the build definition.
Open the Releases tab of the Build & Release hub, open the + drop-down in the list of release definitions, and choose Create release definition.
For 2, write a powershell script to handle this, ensure build outputs
were available to copy from the ‘Drop’ folder on the build and that
they are copied to C:\xxx\ on the target VM(s). More detail steps
please refer this blog.
For 3, you could use Azure SQL Database Deployment task. Either
select the SQL Script file on the automation agent or on a UNC path
that is accessible to the automation agent. Or directly enter the
InLine SQL Script to run against the Azure SQL Server Database. Also take a look at the tutorial.
Maybe not all the task is fully Compatible with TFS2015 version, you could upgrade your TFS version to get more new features or customize your own build/release task to handle it.

Using real Azure SQL databases on VSTS build definition

I want to run tests using real SQL databases. The SQL server and the databases would be on Azure. I'm trying to configure a build definition on VSTS where the tasks would set up an Azure resource group with the databases, set the connection strings on my solution and run the Entity Framework Core migrations to the databases. After the tests a task should delete the resource group.
I created an Azure resource group with the databases I need and downloaded the PowerShell deployment files, the script, template.json and so on.
The PowerShell script task can have an inline script or a path to the script. Should I add the Powershell scripts to the solution so that the VSTS could access them? The inline script option seems to be for small few line scripts and the Azure deployment is quite big with multiple files.
Setting up the connection strings shouldn't be too difficult. Several people suggest using the Replace Tokens task from the market place.
I'm not sure how to run the database migrations after that. Can I run the migrations on PowerShell script task? How can I ensure that the needed cmdlets work?
Since the script is quite big, you can’t use Inline script, you can add it to the project or other path of server (Add additional mapping for different path).
Regarding database migration, if you enabled EF migration in a web app, you can create a publish profile (web deploy package) with database migration enabled, then publish/deploy with /p:DeployOnBuild=true /p:PublishProfile=[profile name];DesktopBuildPackageLocation="$(build.artifactstagingdirectory)\webEF.zip" MSBuild arguments, then deploy package to Azure Web APP through Azure Web Deploy task.

Azure App Service ARM Templates - Run Command in the Console

I am currently building an ARM Template that deploys the following.
App Service Plan
App Service
MS Deploy .NET Core Application on the App Service
The problem is that after the .NET Core Application is deployed I want to execute a command in the console. I have tried a couple of different ways to do it via the CustomScriptExtension, but I keep getting :
"No route registered for '/CustomScriptExtension?api-version=2015-06-15'"
Which makes me think that the Custom Script Extensions are supported for VMs only and not for App Services (I am a bit new to ARM Templating and there is nothing useful I could find in the Azure Quickstart Templates).
Any suggestions on how I can execute a simple command in the App Service command promt via an ARM Template ?
I use the runcommand option of msdeploy to run a command after deployment.
In my case I added a manifest.xml to the root of the zip file that will be deployed:
<MSDeploy.iisApp>
<runcommand path="move D:\home\site\wwwroot\applicationHost.xdt D:\home\site\" dontUseCommandExe="true" MSDeploy.MSDeployKeyAttributeName="path" />
</MSDeploy.iisApp>
The "MSDeploy.MSDeployKeyAttributeName="path"" is important. More details here.
The content of the path attribute will be executed on the remote mashine.
I hope this helps,
KirK

Resources