首页 > 解决方案 > 通过 Azure Pipelines 中的 FTP 任务将单个静态内容文件部署到 Azure 应用服务

问题描述

我刚刚将经典 ASP 网站升级到 ASP.NET Core MVC Razor Page 框架。我没有 CMS 系统,我的所有静态内容文件 (.xml)、PDF 和图像都包含在我的网站项目中。为了部署我的静态内容文件,我在 Azure 管道中使用基于目录的 FTP 任务。当我的发布管道运行时,它们会删除我的应用服务上指定内容目录中的所有内容,然后重新复制与部署关联的目录中的所有内容。使用 Classic ASP,我能够使用 Web Deploy 将单个文件发布到我的本地服务器,但是,由于从本地发布到云的安全问题,Web Deploy 不再是一种选择。我想部署单独的内容文件,而不是我的发布管道中的整个内容目录。部署增量的能力将是一个额外的好处。是否有可用的脚本或其他功能允许我将单个静态内容文件部署到我的应用服务?请注意,由于审核标准,我无法直接在 Kudu 控制台中编辑文件。

标签: azurewebftpazure-pipelinesweb-deployment

解决方案


您可以使用 Kudu api 将单个内容文件部署到您的 azure 应用服务器。

您可以尝试在发布管道中添加脚本任务来调用 kudu api。对于以下 powershell 中的示例脚本:

# User name from WebDeploy Publish Profile. Use backtick while assigning variable content  
$userName = "{userName}"  
# Password from WebDeploy Publish Profile  
$password = "{Password}"  
# Encode username and password to base64 string  
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $userName, $password)))

#deploy the static files to azure app server.
$filePath = "$(system.defaultworkingdirectory)\content\staticfiles.html";
$apiUrl = "https://websitename.scm.azurewebsites.net/api/vfs/site/wwwroot/staticfiles.html";
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method PUT -InFile $filePath -ContentType "multipart/form-data";

您可以在发布配置文件中获取用户名和密码。您可以从 Azure Web App 下载发布配置文件。并参考 publishProfile 部分中的 userName 和 userPWD 值。

您还可以通过Azure PowerShell任务中的脚本获取用户名和密码。请参见下面的示例:

$ResGroupName = ""
$WebAppName = ""

# Get publishing profile for web application
$WebApp = Get-AzWebApp -Name $WebAppName -ResourceGroupName $ResGroupName
[xml]$publishingProfile = Get-AzWebAppPublishingProfile -WebApp $WebApp

# Create Base64 authorization header
$username = $publishingProfile.publishData.publishProfile[0].userName
$password = $publishingProfile.publishData.publishProfile[0].userPWD
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

#deploy the static files to azure app server.
$filePath = "$(system.defaultworkingdirectory)\content\staticfiles.html";
$apiUrl = "https://websitename.scm.azurewebsites.net/api/vfs/site/wwwroot/staticfiles.html";
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method PUT -InFile $filePath -ContentType "multipart/form-data";

请参阅此处了解更多信息。


推荐阅读