gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:39:31 27 4
gpt4 key购买 nike

一直在查看 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com