蛋糕(http://cakebuild.net)可以用于部署Azure WebApps吗?



一直在查看 Cake(在 http://cakebuild.net(,我想知道它是否可用于部署 Web 应用程序和/或访问部署发布包的虚拟服务器。

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

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

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

为了帮助Kudu部署/构建环境,您可以使用Cake.Kudu插件。

第一步是告诉 Kudu 你有一个自定义部署脚本,你可以通过将一个".deployment"文件添加到你的仓库的根目录来做到这一点,

其中包含
[config]
command = deploy.cmd

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

@echo off
IF NOT EXIST "Tools" (md "Tools")
IF NOT EXIST "ToolsAddins" (MD "ToolsAddins")
nuget install Cake -ExcludeVersion -OutputDirectory "Tools" -Source https://www.nuget.org/api/v2/
ToolsCakeCake.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. 点网核心发布
  4. Kudu.Sync

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

  • http://cakebuild.net/blog/2015/10/cake-addin-kudu

  • https://hackernoon.com/delivering-functions-with-cake-4b269c50f817

  • https://daveaglick.com/posts/publishing-to-azure-using-cake-and-web-deploy

最新更新