2

我目前正在用 C# 构建一个本地静态站点生成器。它将一堆模板一起编译成普通的旧 HTML 文件的层次结构。我想将生成的文件上传到我的 Windows Azure 网站并实时反映更改,并且我希望能够通过我的脚本以编程方式执行此操作。

就目前而言,我不得不使用 WebMatrix 手动上传生成的文件,因为我无法找到让我直接将 HTML 上传到 Windows Azure 网站的 API 或 SDK。

当然,必须有一种方法可以从代码中执行此操作,而不仅仅是使用 sFTP 库(因为它不使用 WebMatrix/IIS 协议,我认为它发送压缩差异,会很慢并且意味着超出-在上传过程中同步数据,而某些文件已更新而其他文件尚未更新。)如果可以避免的话,我也宁愿不必将生成的站点提交给源代码管理。在我看来,将某些东西仅仅作为部署的实现细节放入源代码控制中似乎在概念上是错误的。

更新:WebMatrix 内部使用 Web Deploy (MSDeploy)。从理论上讲,您应该能够使用 API 自己构建部署包,但我能找到的 99% 的示例都是使用命令行工具或 Visual Studio 中的 GUI 工具。我需要在 C# 中构建包并以编程方式部署它。关于如何进行此操作的任何想法或指导?MSDN 上的文档并没有真正展示这种场景的任何示例。

4

4 回答 4

3

好的,所以我在微软几个友好的人的帮助下想出了该怎么做。(请参阅David 的 Ebbo 对我的论坛问题的回复,以及来自Sayed Hashimi 的非常有用的信息,展示了如何从 msdeploy.exe 控制台应用程序中准确地执行我想要执行的操作)。

只需从 Azure Web 门户获取您的 PublishSettings 文件。在文本编辑器中打开它以获取要粘贴到以下代码中的值。

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());
}

您还可以通过MSDN 上晦涩的文档偶然发现。有很多名字奇怪的选项类的传递,但是稍微眯起眼睛并在文档中晃来晃去,可以看到命令行选项的方式(其中在线查找示例要容易得多) 映射到 API 调用。

于 2014-03-02T02:07:01.687 回答
2

最简单的方法可能是为您的网站设置Git 发布,并以编程方式执行 git commit,然后执行 git push。您可以将其视为一种部署机制,而不是源代码控制,因为 Azure 网站本身支持支持 Git 存储库,该存储库与您选择的 SCM 解决方案没有任何关系。

于 2014-03-01T18:49:59.887 回答
1

WebMatrix 使用WebDeploy将文件上传到 Windows Azure 网站。

于 2014-03-01T20:38:46.723 回答
1

另一种方法是使用 VFS REST API ( https://github.com/projectkudu/kudu/wiki/REST-API#wiki-vfs )。今天的诊断控制台使用它来处理文件系统。

于 2014-03-03T14:22:20.363 回答