0

I have made a console application for databse migration in .net core 2.0. It is working perfect, but now i want to go for CI/CD in jenkins for the migration. Can someone guide me for the same. and if possible example for the powershell to run .net core console application will be great.

1 Answers1

1

So I found this question when I was searching to do the same thing for my CI process (which is TeamCity and Octopus Deploy) and figured I would add an answer to help out so hopefully people don't waste as much time as I did on this in the future.

I have a .NET Core 2.1 application which basically uses DbUp in the way listed on their read the docs page and then built with the following command:

dotnet.exe publish DbUpDemo\DbUpDemo.csproj --configuration Release

Because I'm not doing a self contained deployment I have not specified a runtime which means that an exe will not be created but instead we will need to run the application via the dll.

The results of the publish are zipped (in my case using octo) and then released to a server.

To run DbUp using powershell you will need the following lines in a script file (.ps1 extension):

$dbUpDllPath = "\DbUpDemo.dll";
$dotNetCommand = "dotnet " + $dbUpDllPath;

iex $dotNetCommand

This assumes that the script is run from the same directory as the dll: if the script is in a different folder from the dll you can set the full path in $dbUpDllPath (so for example if you have extracted the published folder into c:\temp\DbUpDemo you would use $dbUpDllPath = "c:\temp\DbUpDemo\DbUpDemo.dll";)

You could add this script to your source control and ensure it's included during the publish (I am not doing this as I use the run a script step in Octopus after releasing the code to the server)

A couple of things to note:

If you do not specify the runtime during publish you will need to ensure that the appropriate .NET core run time is installed on the server.

You may need to be careful about how you inject your configuration: I use the following code to get the connection string from the appsettings.json file:

 IConfiguration config = new ConfigurationBuilder()
                                .SetBasePath(Directory.GetCurrentDirectory())
                                .AddJsonFile("appsettings.json", true, true)
                                .Build();

string connectionString = config.GetConnectionString("DatabaseToUpdate");

This hinges on the appsettings.json file existing in the current directory and I found that if the script was not contained in the same folder as the dll and json file it would not work. One way round that would be to set the location of the script prior to execution

        Set-Location "c:\temp\DbUpDemo";

        $dbUpDllPath = "\DbUpDemo.dll";
        $dotNetCommand = "dotnet " + $dbUpDllPath;

        iex $dotNetCommand

Or alternatively you could pass the connection string as a parameter to the dotnet command.


One last thing for Octopus specific users if you are using a run script step you can use the following script:

Set-Location $OctopusParameters['InstallationDirectory'];
Get-Location | Write-Host

$dbUpDllPath = $OctopusParameters['InstallationDirectory'] + "\DbUpDemo.dll";
dotnet $dbUpDllPath | Write-Host

The thing to note is that the dotnet command can be called directly without having to wrap it up in an invoke command: I found with my setup Octopus was not playing nice with the invoke command (Error: The term 'dotnet c:\temp\DbUpDemo\DbUpDemo.dll' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.) but calling the dotnet command directly (instead of wrapping it in an invoke) worked a charm.

Also a side note: In the example I am using an Octopus variable called InstallationDirectory which is used by this run script step and the previous step (which is a deploy package step).