Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Publish ASP.NET Core from Visual Studio to Linux

Can I edit a ASP.NET Core application in Visual Studio and deploy to Linux server (e.g., Ubuntu)?

like image 574
Hacskó Ádám Avatar asked Feb 15 '17 16:02

Hacskó Ádám


People also ask

How do I publish a Visual Studio Project in Linux?

To publish from Visual Studio, do the following: Change the solution configuration from Debug to Release on the toolbar to build a Release (rather than a Debug) version of your app. Right-click on the project (not the solution) in Solution Explorer and select Publish. In the Publish tab, select Publish.

Does ASP.NET Core work on Linux?

With ASP.NET Core, you can build and run . NET applications not only on Windows but also macOS and Linux.


1 Answers

You can check this page in the ASP.NET CORE documentation - https://docs.microsoft.com/en-us/aspnet/core/publishing/linuxproduction

A good example can also be found in this blog post from Scott Hanselman - https://www.hanselman.com/blog/PublishingAnASPNETCoreWebsiteToACheapLinuxVMHost.aspx

I currently use my own batch script to deploy which follows these steps:

  1. Publishes the app using dotnet publish command.
  2. Zips everything using Powershell.
  3. Copies the zip to the Linux machine using pscp - https://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter5.html
  4. Connects to the Linux machine using Windows Bash (you need Windows 10 Anniversary Update for this feature to be available).
  5. Calls the unzip command on the Linux machine - needs to be installed there first.
  6. Restarts the supervisor service on the Linux machine.

EDIT: I have added the version of my script which I used when I posted my original answer as requested by Andrew Basarab. I'm sure it needs some refactoring considering my poor scripting knowledge back then. Please use with caution:

@echo off

set PrivateKeyLocation="C:\fakepath\fakefile.ppk {private key for connecting to the remote Linux machine}"
set CertificateFileLocation="/mnt/c/fakepath/fakefile.pem {same key used to execute remote bash commands from my Windows machine}"

for %%* in (.) do set CurrentDirName=%%~nx*
set OutputFolder="%tmp%\%CurrentDirName%"
set OutputZipFile="%tmp%\%CurrentDirName%.zip"
set RemoteHost="[email protected] {remote host address}"
set RemoteLocation="/home/ubuntu {the location to copy the output to}"

dotnet publish -o "%OutputFolder%"

powershell -command "& {&'Compress-Archive' -Path %OutputFolder% -DestinationPath %OutputZipFile%}"
rmdir /s /q %OutputFolder%

pscp -i %PrivateKeyLocation% -pw {private key password} %OutputZipFile% %RemoteHost%:%RemoteLocation%
del /q %OutputZipFile%

bash -c "ssh -i %CertificateFileLocation% %RemoteHost% 'sudo rm -rf %CurrentDirName% ; unzip %CurrentDirName%.zip ; rm -r %CurrentDirName%.zip ; sudo service supervisor restart'"

Some tools and services need to be installed on both machines. Please refer to the aforementioned post by Scott Hanselman.

like image 68
Panayot Todorov Avatar answered Oct 15 '22 21:10

Panayot Todorov