Quick Links

Visual Studio has options to automatically publish your release build to remote servers over FTP. This can be very useful if you're deploying changes often or testing in a remote environment. We'll show you how to set it up and talk about how it works.

Publishing via FTP

FTP is an old, generally insecure protocol, but as long as your FTP server supports the modern extensions of FTPS, which uses TLS encryption, it should be fine. You can also chroot your FTP users so that they can only write to the target directory.

You can read our guide to setting up an FTP server to learn more. You'll want to make sure that FTPS is set up and that regular FTP is disabled.

Not all types of builds are supported, though. Class libraries, for example, have no options to publish. ASP.NET Core applications are supported better and have options to publish to Azure and Docker.

Luckily, for the applications that aren't supported, you can hook it up yourself, as Visual Studio also offers options to run post-build PowerShell scripts. These will run on build instead of on-publish, but you can just set up a custom "publish" build profile with copied release settings.

You can use these post-build scripts to make FTP or SCP transfers, or even to connect to external services like AWS S3 or a Docker Registry.

Setting Up Visual Studio

Under build, you should see an option to publish your solution. If you don't see this, you'll need to skip to the backup option of using post-build scripts.

Under build, you should see an option to publish your solution.

You'll need to make a new publish profile or edit the default one. Select FTPS Server, although you can publish to Azure, Docker, a local folder, or IIS web deploy if those work better for you.

Select FTPS Server, though you can publish to Azure, Docker, a local folder, or IIS web deploy if those work better for you.

Enter in the details of the connection. You'll need to give it a URL, a path to where it's going to upload, and a username/password.

Enter in the details of the connection. You'll need to give it a URL, a path to where it's going to upload, and a username/password.

Then, click publish, and you should see it transfer in the console after the build is complete. If it doesn't, you'll get an error or a log file where you can troubleshoot it.

Setting Up Post-Build Scripts for Other Applications

Visual Studio's built-in options are great, but when they don't work, you'll need to do a bit of scripting. Right-click your project and select "Properties":

 Visual Studio, right-click project, click "Properties."

Then, under "Build Events," you can set up a post-build command. Note that this is cmd, so you'll need to manually call PowerShell, and this is also scoped to the output folder of the build, so if you're storing the PowerShell script at the root of your project, you might need to reference it a few directories up with

        ......
    

.

Set it to run on post-build, and you can choose whether to run the script always, whenever it's successful, or whenever the build gets updated. If you're restarting servers or something, you'll only want to run the script when it's updated. Otherwise, select "on successful build" unless you have a reason to be running the script when the build fails.

Running prebuild and postbuild events.

PowerShell doesn't have built-in support for FTP. You have two options---use PowerShell Remoting over SSH to transfer to a Linux server, or use a PowerShell library for FTP.

$s = New-PSSession -HostName 123.123.123.123 -UserName ubuntu
    

Copy-Item .example.txt /home/ubuntu -ToSession $s

If you need to use FTP, WinSCP can handle the transfer pretty easily.

Add-Type -Path "WinSCPnet.dll"
    

$sessionOptions = New-Object WinSCP.SessionOptions

$sessionOptions.ParseUrl("ftp://username:password@example.com/")

$session = New-Object WinSCP.Session

$session.Open($sessionOptions)

$session.PutFiles("c:source*", "/destination/").Check()

$session.Dispose()

In either case, you'll need to make sure that your script has correct references to the proper file locations. If you click "Edit Post-Build," you'll see a window listing all the variables that you can pass to your script.

 "Edit Post-Build" variables.