How do build an Azure Devops Pipeline with Fluent Migrator task? - azure-devops

I have been using Fluent Migrator (version 3.2.1) for some time and in my Visual Studio environment I use dotnet-fm to migrate or rollback my migrations. This is all great but now I want to automate this and use Azure Devops Pipelines to run the migration commands but I don't know how and where to start.
Has anyone done this and could be kind enough to point me in the right direction, maybe with some examples. I would greatly appreciate it!

How do build an Azure Devops Pipeline with Fluent Migrator task?
Not sure if what I did is exactly what you want. You could check if the information below is helpful.
According to the document Quickstart of fluentmigrator:
Created a .net core library project and add the package FluentMigrator, FluentMigrator.Runner, FluentMigrator.Runner.SQLite, Microsoft.Data.Sqlite.
Create a file called 20180430_AddLogTable.cs.
Build the project.
Open a cmd window, switch path to the project folder, and then execute the command line:
dotnet tool install -g FluentMigrator.DotNet.Cli
After install the FluentMigrator.DotNet.Cli and execute the command line:
dotnet fm migrate -p sqlite -c "Data Source=test.db" -a ".\bin\Debug\netcoreapp2.1\test.dll"
It works fine on my local side.
Then, submit the solution to the Azure devops repo, create a pipeline with following tasks:
NuGet tool installer
NuGet Restore
Dotnet build
Command line task with following scripts:
cd $(Build.SourcesDirectory)/test/test
dotnet tool install -g FluentMigrator.DotNet.Cli
dotnet fm migrate -p sqlite -c "Data Source=test.db" -a ".\bin\Debug\netcoreapp2.1\test.dll"
It works the same:
Hope this helps.

Related

How to build Visual Studio Installer Project in Azure Pipelines in Self-Hosted Windows Agent

The image above shows my Hello World project structure. I am trying to build the Setup-HelloWorld-x86.vdproj in Azure Pipeline in Self-Hosted Windows Agent (which is nothing but my local machine) by using the command line script task like shown in below image.
But I get the following error.
Kindly help me to build this Setup-HelloWorld-x86.vdproj
If you want to run the project via cmd devenv, you can refer to this doc.
As a workaround, you can install the extension Build VS Installer and use the task DutchWorkz - Build VS Installer(s) to build Visual Studio Installer Project in Azure Pipelines.
You can specify to build .sln or .vdproj to generate .msi file(s) in Task-mode option.
Update1
Thanks Tharunavignesh J for sharing.
The solution was to run the azure agent under proper account. Earlier my azure agent was running under some Network Service account, then I change it to the local account, then this cmd worked properly.
This other stack overflow link helped me get my visual studio installer project compiled with Azure DevOps pipelines. It is using yaml configuration instead of classic, but same steps / commands apply to both:
YAML Script for building Visual Studio Installer Projects using Azure DevOps
Basically the steps are:
Setup your variables to point to the tools
Download Nuget packages for your solution
Disable Out Of Process Builds command executed
Script task with the command line arguments to build your solution

Updating postgreSQL DB through EFCore migrations in Azure DevOps

In Azure DevOps, the way I used to update the SQL Server Database was with Entity Framework Core, using two tasks:
In my Build Pipeline: This task that generated a sql script with my db
migrations.
In the Release Pipeline: This task
to update the database using this script.
The thing is, now that I'm using a PostgreSQL database, I can't find an easy and clean way to update the database in the same way. I've seen there's another task for MySQL that does exactly the same my release pipeline task did with SQL Server, but nothing for PostgreSQL.
So I thought I could basically execute dotnet ef update database (with its proper options set) in the pipeline, but I was wondering if there's actually a way to keep updating the database in a smooth way as I did before.
I finally got to fix it.
There are two solutions I found to fix the issue.
First, there's a common fix for all the databases that support Entity Framework Migrations:
Using a .NET Core Task, we'll have to install the dotnet ef tool:
The task would look like this:
And this would be the YAML (in case you want to use it in out of the release pipeline):
- task: DotNetCoreCLI#2
displayName: 'dotnet custom'
inputs:
command: custom
custom: tool
arguments: 'install --global dotnet-ef --version 3.1.4 --ignore-failed-sources'
And once we have the required tools installed, with a CMD or a Bash Task, we'll have to execute a script like this:
dotnet ef database update -c <DBCONTEXT> -p <PROJECT> -s <STARTUP_PROJECT> -v --no-build
You just have to add the flag -c in case you have more than one context in your project (sometimes the other DbContexts can come from some nugget packages).
Notice I added the flag --no-build since I already built the project in the build pipeline to follow good practices.
The other option (and the one I finally used), it's been to use this task that basically does the same process, with the difference that it does it by using your already compiled .dll files, so you won't have to copy the entire project to make the migrations work. The setup of the task, although you have to fill many inputs, it's pretty straightforward, and it's supposed to work with other Databases as well.
However, if I had to use SQL Server or MySQL I would use a migrations script, since the process it's much easier (you just need to generate a .sql script and then it's the only file required for deploying the migrations).

Deploy a self-contained .NET Core application with Azure DevOps App Service Deploy task

From a local machine I can publish a .NET Core application to Azure Web Service as a self-hosted application by defining <SelfContained>true</SelfContained> in publish profile.
App Service Deploy task in Azure DevOps pipeline publishes it to IIS by default.
How configure it to publish as self-hosted?
I got this working in Azure Dev Ops with my Blazor Server Side App that is targeting a preview version of .NET Core 3.0. To do this without creating a yaml file for your build definition you should be able to add the following argument on your dotnet publish task if you're not targeting a preview version of .NET Core.
-r win-x86 --self-contained true
The -r is the run time that you want to target, in my case I selected win-x86 since that is what my app service is configured to use. Then just add the self contained argument. Your full argument will probably look something like this:
--configuration $(BuildConfiguration) -r win-x86 --self-contained true --output $(build.artifactstagingdirectory)
This link covers the dotnet publish command. This is the same command that is executed when you publish from your local machine dotnet publish
The full list of run time identifiers can be found here:
run time identfiers
gist of the complete build definition in yaml file
yaml

VSTS Build How to Add update-database command for EF Core Code First

I use VSTS and Git as source controller and Azure as host to my ASP.NET Core 2.1 API application, I'm trying to have a CI in VSTS, So every things worked except automatically update-database, I used EF core Code first and I need to update DB (in Azure) Automatically in CI process.
But For now, each time I update the connection string in my project(to Azure DB) and run the update-database manually.
For automatic CI process I find:
Using Migration.exe in TFSBuild
How to UPDATE DATABASE in Code First approach
So I search in Debug Console to find package folder irst to set the command to use packages/migration.exe
Microsoft.AspNetCore.AzureAppServices.SiteExtension\store\x86\netcoreapp2.1
I can't find any package folder, or entifyframework libraries
So how could I find the migration.exe file?
Is there any other way to have automatic update-database in VSTS build for EF Code first?
UPDATE:
Here is my nuget reference:
I add the copy file step:
And config is:
Bu I got the Error:
It seems the source folder path is not correct, What should I add as a Source folder?
I recommend that you can do migration during deploy process:
Configure your web core project with EF migration: Migrations
Create a publish profile with Entity Framework Migrations (Settings > Entity Framework Migrations)
Include the publish profile to source control (include pubxml.user file to source control or copy ItemGroup section (<ItemGroup><EFMigrations …) to your publish profile (.pubxml))
Publish web app through Visual Studio Build task (MSBuild Arguments: /p:DeployOnBuild=true /p:PublishProfile=CustomProfile;DesktopBuildPackageLocation="$(build.artifactstagingdirectory)\myapp.zip")
Deploy package (step4) to your site (e.g. through WinRM-IIS Web App Deployment task or Azure App Service Deploy task)

DACPAC Deployment in VSTS

I want to create a dacpac file using VSTS. If I create a dacpac file and commit in VSTS to execute the scripts using Execute Azure SQL Tak:Dacpactask Azure database, we are unable to maintain the history of the changes. So I want to make the changes in SQL Project and move the project to VSTS. Do we have any task/way to generate a DACPAC file using sql project that we have in VSTS?. Finally I want to call that dacpac file using Execute Azure SQL Tak:Dacpactask to execute the scripts in Azure database
Refer to these steps below:
Refer to Grant’s answer to add project (not dacpac file) to source control and track changes
Create a new build definition
Add NuGet Tool Installer task (Version of NuGet.exe to install: 4.3.0)
Add NuGet task (Version:2.*; Command: restore; Path to solution, packages.config, or project.json: **/*sln)
Add Visual Studio Build task (Solution: [click … to select solution file]; Visual Studio Version: Visual Studio 2015; MSBuild Arguments: /p:OutDir=$(Build.ArtifactStagingDirectory))
Add Azure SQL Database Deployment task: (DACPAC File: $(build.artifactstagingdirectory)\**\SQLDatabase.dacpac)
In order to track changes you just have to connect your database project to whatever source control system you're using. It's a fundamental part of Visual Studio. You can just do that. Once it's connected to source control, you can then automate builds from the source control system using all the other Visual Studio tools. Here's an example that seems fairly complete. The next step is to integrate through the deployment processes in VSTS to automate the whole thing.