Using real Azure SQL databases on VSTS build definition - azure

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.

Related

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

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

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

Azure - backup app on vm before delivering new changes

I have several .NET applications that are hosted In Azure on virtual machines IIS. I want to automate deployment process directly from my machine or visual studio without using Azure portal because we are doing several deliveries each day. The biggest challenge for me now is to do backup before deployment. So I need to backup specific folder on VM remotely. Could anyone tell me how I can do it?
Are there any best practices for automation delivery from visual studio to Azure VM? Backup of app is required.
First, I'm assuming that you would like to back up some specific folders in your VM to an Azure Blobs or Azure Files storage. There are two parts you'd need to complete your continuous deployment:
Automate backing up some folders in your virtual machine.
Integrate task #1 into a CICD (Continuous Integration Continuous Deployment) tool, which I'd suggest VSTS (Visual Studio Team Services) for your beginning.
Approach #1
You can expose Windows Remote Management (WinRM) endpoint publicly and use PowerShell to perform a folder backup task. You will also need to invoke some scripts to write/copy your backup to Azure Blobs Storage. Your script must be authenticated silently so you don't need to key in Azure subscription admin or VM admin.
Once you have a PowerShell script, you can invoke this PowerShell by creating a new PowerShell task in your Build or Release definition. I'd suggest to define in Release definition.
Approach #2
It's more Cloud native and you don't have to expose WMI of your virtual machine which would lead to security threat. Instead, you utilize Azure Automation Worker to automate the entirely backup & copy to another Azure services (Blobs, Files, another backup/file server virtual machine..). In your run book, you need to use PowerShell DSC (Desired State Configuration) to interact with resources inside your virtual machine.
In VSTS, you have two ways to start your runbook
Invoke runbook's webhook: you can create a webhook for your runbook and call this webhook by creating a Http Task
Start runbook by PowerShell: similar to the approach #1, just create PowerShell task then use Start-AzureAutomationRunbook cmdlet with sample here.
Build an ARM template for your Runbook then define in Build definition. Here is the sample reference to deploy an ARM template in VSTS,
The reason I have to give several references because you might not have familiarity with CICD and DevOps concept, as well as some useful tools in Azure and VSTS which supports your continuous deployment. There are some awesome CICD solutions in the market you should also explore, such as Chef, Ansible, Puppet. They support CICD very well. Below are some references to get started with DevOps on Azure:
https://learn.microsoft.com/en-us/vsts/deploy-azure/
https://learn.microsoft.com/en-us/azure/automation/automation-dsc-overview
https://learn.microsoft.com/en-us/vsts/build-release/apps/cd/azure/azure-devops-project-aspnetcore

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.

Can i run powershell scripts through ARM Template?

I want to run powershell scripts to create users and usergroups in Azure AD . Is it possible to call ps scripts in ARM Template?
Update:
Now it is possible to do some operations with a new feature (still in Preview)
https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/deployment-script-template
Use deployment scripts in templates (Preview)
Learn how to use deployment scripts in Azure Resource templates. With
a new resource type called Microsoft.Resources/deploymentScripts,
users can execute deployment scripts in template deployments and
review execution results. These scripts can be used for performing
custom steps such as:
add users to a directory
create an app registration
perform data plane operations, for example, copy blobs or seed database
look up and validate a license key
create a self-signed certificate
create an object in Azure AD
look up IP Address blocks from custom system
The benefits of deployment script:
Easy to code, use, and debug. You can develop deployment scripts in your favorite development environments. The scripts can be embedded in
templates or in external script files.
You can specify the script language and platform. Currently, only Azure PowerShell deployment scripts on the Linux environment are
supported.
Allow specifying the identities that are used to execute the scripts. Currently, only * * Azure user-assigned managed identity is
supported.
Allow passing command-line arguments to the script.
Can specify script outputs and pass them back to the deployment.
Remember that ARM templates should be idempotent. You should write code that can be executed multiple times, even on environments where your code was already executed.
For example, if you are going to configure a setting, or create a resource, your powershell should probably check if the resource is already in place and properly configured.
No, Azure ARM could not execute scripts directly. Executing scripts need host, Azure template does not provide such host.
One solution, you could select Azure Custom Script Extension.
The Custom Script Extension downloads and executes scripts on Azure virtual machines.

Resources