Cake (of http://cakebuild.net) 可以用于部署 Azure WebApps

Can Cake (of http://cakebuild.net) be used for deploying Azure WebApps

一直在检查 Cake(在 http://cakebuild.net),想知道它是否可以用于部署 webapps and/or 访问虚拟服务器以部署发布包。

我喜欢蛋糕作为 C# 部署框架的想法,因此与核心开发使用相同的语言。

是否有任何我可以访问的 Azure 部署示例?

有几种方法可以使用 Cake 部署 Azure,或者通过使用某些 CI 服务(例如 VSTS/AppVeyor 预构建站点,然后使用网络部署发布工件,git 或 ftp(有一些 Cake 插件可以帮助 Cake.WebDeploy, Cake.Git or Cake.FTP 或使用 Azure 内置部署引擎 Kudu 和使用 Cake 的自定义部署脚本。

要协助 Kudu deploy/build 环境,您可以使用 Cake.Kudu 插件。

第一步是告诉 Kudu 您有一个自定义部署脚本,您可以通过将“.deployment”文件添加到存储库的根目录来执行此操作,内容为

[config]
command = deploy.cmd

deploy.cmd 安装和启动 Cake 可能看起来像这样

@echo off

IF NOT EXIST "Tools" (md "Tools")

IF NOT EXIST "Tools\Addins" (MD "Tools\Addins")

nuget install Cake -ExcludeVersion -OutputDirectory "Tools" -Source https://www.nuget.org/api/v2/

Tools\Cake\Cake.exe deploy.cake -verbosity=Verbose

而 deploy.cake 可能看起来像这样:

#tool "nuget:https://www.nuget.org/api/v2/?package=xunit.runner.console"

#tool "nuget:https://www.nuget.org/api/v2/?package=KuduSync.NET"

#addin "nuget:https://www.nuget.org/api/v2/?package=Cake.Kudu"

///////////////////////////////////////////////////////////////////////////////

// ARGUMENTS

///////////////////////////////////////////////////////////////////////////////


var target = Argument<string>("target", "Default");

var configuration = Argument<string>("configuration", "Release");

///////////////////////////////////////////////////////////////////////////////

// GLOBAL VARIABLES

///////////////////////////////////////////////////////////////////////////////

var webRole = (EnvironmentVariable("web_role") ?? string.Empty).ToLower();

var solutionPath = MakeAbsolute(File("./src/MultipleWebSites.sln"));

string outputPath = MakeAbsolute(Directory("./output")).ToString();

string testsOutputPath = MakeAbsolute(Directory("./testsOutputPath")).ToString();


DirectoryPath websitePath,

                websitePublishPath,

                testsPath;


FilePath projectPath,

            testsProjectPath;

switch(webRole)

{

    case "api":

        {

            websitePath = MakeAbsolute(Directory("./src/Api"));

            projectPath = MakeAbsolute(File("./src/Api/Api.csproj"));

            testsPath = MakeAbsolute(Directory("./src/Api.Tests"));

            testsProjectPath = MakeAbsolute(File("./src/Api.Tests/Api.Tests.csproj"));

            websitePublishPath = MakeAbsolute(Directory("./output/_PublishedWebsites/Api"));

            break;

        }

    case "frontend":

        {

            websitePath = MakeAbsolute(Directory("./src/Frontend"));

            projectPath = MakeAbsolute(File("./src/Frontend/Frontend.csproj"));

            testsPath = MakeAbsolute(Directory("./src/Frontend.Tests"));

            testsProjectPath = MakeAbsolute(File("./src/Frontend.Tests/Frontend.Tests.csproj"));

            websitePublishPath = MakeAbsolute(Directory("./output/_PublishedWebsites/Frontend"));

            break;

        }

    case "backoffice":

        {

            websitePath = MakeAbsolute(Directory("./src/Backoffice"));

            projectPath = MakeAbsolute(File("./src/Backoffice/Backoffice.csproj"));

            testsPath = MakeAbsolute(Directory("./src/Backoffice.Tests"));

            testsProjectPath = MakeAbsolute(File("./src/Backoffice.Tests/Backoffice.Tests.csproj"));

            websitePublishPath = MakeAbsolute(Directory("./output/_PublishedWebsites/Backoffice"));

            break;

        }

    default:

        {

            throw new Exception(

            string.Format(

                    "Unknown web role {0}!",

                    webRole

                )

            );

        }

}


if (!Kudu.IsRunningOnKudu)

{

    throw new Exception("Not running on Kudu");

}


var deploymentPath = Kudu.Deployment.Target;

if (!DirectoryExists(deploymentPath))

{

    throw new DirectoryNotFoundException(

        string.Format(

            "Deployment target directory not found {0}",

            deploymentPath

            )

        );

}


///////////////////////////////////////////////////////////////////////////////

// SETUP / TEARDOWN

///////////////////////////////////////////////////////////////////////////////


Setup(() =>

{

    // Executed BEFORE the first task.

    Information("Running tasks...");

});


Teardown(() =>

{

    // Executed AFTER the last task.

    Information("Finished running tasks.");

});


///////////////////////////////////////////////////////////////////////////////

// TASK DEFINITIONS

///////////////////////////////////////////////////////////////////////////////


Task("Clean")

    .Does(() =>

{

    //Clean up any binaries

    Information("Cleaning {0}", outputPath);

    CleanDirectories(outputPath);


    Information("Cleaning {0}", testsOutputPath);

    CleanDirectories(testsOutputPath);


    var cleanWebGlobber = websitePath + "/**/" + configuration + "/bin";

    Information("Cleaning {0}", cleanWebGlobber);

    CleanDirectories(cleanWebGlobber);


    var cleanTestsGlobber = testsPath + "/**/" + configuration + "/bin";

    Information("Cleaning {0}", cleanTestsGlobber);

    CleanDirectories(cleanTestsGlobber);

});


Task("Restore")

    .Does(() =>

{

    // Restore all NuGet packages.

    Information("Restoring {0}...", solutionPath);

    NuGetRestore(solutionPath);

});


Task("Build")

    .IsDependentOn("Clean")

    .IsDependentOn("Restore")

    .Does(() =>

{

    // Build target web & tests.

    Information("Building web {0}", projectPath);

    MSBuild(projectPath, settings =>

        settings.SetPlatformTarget(PlatformTarget.MSIL)

            .WithProperty("TreatWarningsAsErrors","true")

            .WithProperty("OutputPath", outputPath)

            .WithTarget("Build")

            .SetConfiguration(configuration));


    Information("Building tests {0}", testsProjectPath);

    MSBuild(testsProjectPath, settings =>

        settings.SetPlatformTarget(PlatformTarget.MSIL)

            .WithProperty("TreatWarningsAsErrors","true")

            .WithProperty("ReferencePath", outputPath)

            .WithProperty("OutputPath", testsOutputPath)

            .WithTarget("Build")

            .SetConfiguration(configuration));

});


Task("Run-Unit-Tests")

    .IsDependentOn("Build")

    .Does(() =>

{

    XUnit2(testsOutputPath + "/**/*.Tests.dll", new XUnit2Settings {

        NoAppDomain = true

        });

});


Task("Publish")

    .IsDependentOn("Run-Unit-Tests")

    .Does(() =>

{

    Information("Deploying web from {0} to {1}", websitePublishPath, deploymentPath);

    Kudu.Sync(websitePublishPath);

});



Task("Default")

    .IsDependentOn("Publish");



///////////////////////////////////////////////////////////////////////////////

// EXECUTION

///////////////////////////////////////////////////////////////////////////////


RunTarget(target);

在上面的场景中,它支持具有 3 个不同网站的解决方案,并且发布的是基于应用程序设置的解决方案。

对于 .NET Core Web 应用程序,流程类似,基本上如下所示:

  1. DotNetCoreRestore
  2. DotNetCoreBuild
  3. DotNetCorePublish
  4. Kudu.Sync

有几篇关于使用 Cake 部署到 Azure 的好博文: