Azure Devops - Azure powershell 任务查找 release/deployment 目录

Azure Devops - Azure powershell task find release/deployment directory

我是 运行 Azure devops,我有一个管道和一个版本(运行 在自托管代理上),并且该版本设置为发送到 azure 应用程序服务。部署工作正常,唯一的问题是我还希望能够(基于我的一些发布变量)在网站已经部署到 Azure 网站后编辑该网站的 web.config。

我正在使用显示站点所在目录的 Azure Powershell 任务 (https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-powershell?view=azure-devops ) ,and i can't find anywhere in the release variables ( https://docs.microsoft.com/en-us/azure/devops/pipelines/release/variables?view=azure-devops&tabs=batch)。通过 Kudu 看,它非常基本,比如 d:\home\site\wwwroot\ ,但使用它根本不起作用。

我正在寻找的这个 post 配置是不是真的可行,或者我应该以不同的方式接近它?

我认为您可以使用 powershell 任务调用 Kudu api 来获取已部署的 web.config 并在您的发布管道中使用 Magic Chunks task 或文件转换任务对其进行编辑。然后使用 Kudu api 将更改后的 web.config 再次上传到 azure 网站。

1,下面的脚本显示了如何从 Azure 网站获取 web.config。

$srcResGroupName = "Test"
$srcWebAppName = "tstest12"
$outwebconfig="$(System.DefaultWorkingDirectory)\tempfolder\web.config"

# Get publishing profile for SOURCE application
$srcWebApp = Get-AzWebApp -Name $srcWebAppName -ResourceGroupName $srcResGroupName
[xml]$publishingProfile = Get-AzWebAppPublishingProfile -WebApp $srcWebApp
# 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)))

$apiBaseUrl = "https://$($srcWebApp.Name).scm.azurewebsites.net/api/vfs/site/wwwroot/web.config"

# Download the web.config file to $outwebconfig

Invoke-RestMethod -Uri "$apiBaseUrl" `
                    -Headers @{UserAgent="powershell/1.0"; `
                     Authorization=("Basic {0}" -f $base64AuthInfo)} `
                    -Method GET `
                    -OutFile $outwebconfig

以上脚本将从 Azure 网站下载 web.config 并将其保存到 $(System.DefaultWorkingDirectory)\tempfolder\web.config,稍后您可以在其中使用转换任务对其进行编辑。

以上脚本使用脚本获取用户名和密码,您还可以通过转到应用服务上的“概览”边栏选项卡,单击边栏选项卡顶部的 ...更多,然后单击获取发布配置文件

2,然后你可以添加一个配置转换任务来改变你的web.config。

3、最后添加一个powershell任务将修改后的web.config上传到azure网站

$srcResGroupName = "Test"
$srcWebAppName = "tstest12"
$webconfig="$(System.DefaultWorkingDirectory)\tempfolder\web.config"

# Get publishing profile for SOURCE application
$srcWebApp = Get-AzWebApp -Name $srcWebAppName -ResourceGroupName $srcResGroupName
[xml]$publishingProfile = Get-AzWebAppPublishingProfile -WebApp $srcWebApp
# 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)))

 $apiUrl = "https://$($srcWebApp.Name).scm.azurewebsites.net/api/vfs/site/wwwroot/web.config";

Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method PUT -InFile $webconfig -ContentType "application/xml";

有关 Kudu api 的更多用法,您可以查看 here