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

标签 c# azure continuous-integration cakebuild

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

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

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

最佳答案

有几种方法可以使用 Cake 部署 Azure,或者通过使用一些 CI 服务(如 VSTS/AppVeyor)预构建站点,然后使用 Web 部署、git 或 ftp 发布工件(有一些 Cake 插件可以帮助解决这个问题 Cake.WebDeployCake.GitCake.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 "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. DotNetCore发布
  4. Kudu.Sync

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

关于c# - Cake(http ://cakebuild.net)可以用于部署 Azure WebApps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45228119/

相关文章:

c# - TaskCompletionSource 是否保证等待代码将在调用 TCS.TrySetResult 的线程上恢复?

c# - 您如何确定两个 HashSet 是否相等(按值,而不是按引用)?

c# - 导出到 Excel 不起作用

msbuild - Hudson 在使用 MSBUILD 构建 CSPROJ 文件时遇到问题吗?

tensorflow - 连续机器学习管道因 tensorflow 需求安装而中断

c# - 只对 IntPtr 进行任何类型数组的一种方法

Azure DevOps - 测试运行管理 : resuming in-progress test

azure - 无法删除 Kubernetes 服务 - Azure

azure - 生成静态 IP 地址以在 Azure 上的 Web 角色中托管 wcf 服务

git - 如何确定一个分支是否是 jenkins 文件中的默认分支?