Ryan Norbauer
Ryan Norbauer

Reputation: 1708

How can I upload a static HTML site to a Windows Azure Website programmatically?

I am currently building a local static site generator in C#. It compiles a bunch of templates together into a hierarchy of plain old HTML files. I want to upload the resulting files to my Windows Azure Website and have the changes reflected live, and I want to be able to do this programmatically via my script.

As it stands, I'm having to upload the generated files manually using WebMatrix, as I haven't been able to find an API or SDK that lets me directly upload HTML to a Windows Azure Website.

Surely there must be a way to do this from code, other than just using an sFTP library (which, because it doesn't use the WebMatrix/IIS protocol, which I think sends zipped diffs, would be slow and would mean out-of-sync data during the upload while some files have been updated and others haven't.) I'd also rather not have to commit my generated site to source control if I can avoid it. It seems conceptually wrong to me to be putting something into source control merely as an implementation detail of deployment.

Update: WebMatrix internally uses Web Deploy (MSDeploy). Theoretically you should be able to build the deployment package yourself using the API, but 99% of the examples I can find are using the command-line tool or the GUI tools in Visual Studio. I need to build the package and deploy it programmatically from within C#. Any ideas or guidance on how to go about this? The docs on MSDN don't really show any examples for this kind of scenario.

Upvotes: 2

Views: 4629

Answers (4)

Ryan Norbauer
Ryan Norbauer

Reputation: 1708

OK, so I worked out what to do with help from a couple of friendly folks at Microsoft. (See David's Ebbo's response to my forum question, and this very helpful info from Sayed Hashimi showing how to do exactly what I wanted to do from the msdeploy.exe console app).

Just grab your PublishSettings file from the Azure web portal. Open it in a text editor to get the values to paste into the below code.

var destinationOptions = new DeploymentBaseOptions()
{
    // userName from Azure Websites PublishSettings file
    UserName = "$msdeploytest",
    // pw from PublishSettings file
    Password = "ThisIsNotMyPassword",
    // publishUrl from PublishSettings file using https: protocol prefix rather than 443 port
    // and adding "/msdeploy.axd?site={msdeploySite-variable-from-PublishSettings}"
    ComputerName = "https://waws-prod-blu-003.publish.azurewebsites.windows.net/msdeploy.axd?site=msdeploytest",
    AuthenticationType = "Basic"
};
                                                             // This option says we're giving it a directory to deploy
using (var deploymentObject = DeploymentManager.CreateObject(DeploymentWellKnownProvider.ContentPath,
                                                             // path to root directory of source files 
                                                             @"C:\Users\ryan_000\Downloads\dummysite"))
{
    var syncOptions = new DeploymentSyncOptions();
    syncOptions.WhatIf = false;
                                                                                   // "msdeploySite" variable from PublishSettings file
    var changes = deploymentObject.SyncTo(DeploymentWellKnownProvider.ContentPath, "msdeploytest", destinationOptions, syncOptions);
    Console.WriteLine("BytesCopied:       " + changes.BytesCopied.ToString());
    Console.WriteLine("Added:             " + changes.ObjectsAdded.ToString());
    Console.WriteLine("Updated:           " + changes.ObjectsUpdated.ToString());
    Console.WriteLine("Deleted:           " + changes.ObjectsDeleted.ToString());
    Console.WriteLine("Errors:            " + changes.Errors.ToString());
    Console.WriteLine("Warnings:          " + changes.Warnings.ToString());
    Console.WriteLine("ParametersChanged: " + changes.ParameterChanges.ToString());
    Console.WriteLine("TotalChanges:      " + changes.TotalChanges.ToString());
}

You might also be able to stumble your way through the obscure documentation on MSDN. There is a lot of passing around of oddly-named options classes, but with a bit of squinting of one's eyes and flailing about in the docs it's possible to see how the command-line options (of which it is much easier to find examples online) map to API calls.

Upvotes: 3

Pranav
Pranav

Reputation: 543

An alternative is to use the VFS REST API (https://github.com/projectkudu/kudu/wiki/REST-API#wiki-vfs). The diagnostic console uses this to work with the file system today.

Upvotes: 1

cory-fowler
cory-fowler

Reputation: 4088

WebMatrix uses WebDeploy to upload the files to Windows Azure Web Sites.

Upvotes: 1

Rytmis
Rytmis

Reputation: 32047

The easiest way is probably to set up Git publishing for your website and programmatically do a git commit followed by a git push. You can think of it as a deployment mechanism instead of source control, given that Azure websites natively support a backing Git repository that doesn't have to have anything to do with your chosen SCM solution.

Upvotes: 2

Related Questions