我试图通过power shell脚本访问Kudu。链接看起来像:https://adc-dev.scm.azurewebsites.net
。我需要复制位于上方链接war
位置的D:\home\site\wwwroot\bin\apache-tomcat-8.0.33\webapps
文件。
目前,我正在通过添加FTP任务使用VSTS部署war
文件。但在部署最新的war
之前,我想在Azure Kudu位置的某个位置备份旧的war
,例如:D:\home\site\wwwroot\bin\apache-tomcat-8.0.33
(war
位置的根文件夹)。因此,之后我可以删除war
并在Kudu中部署最新的war
文件。
怎么做?我的意思是如何使用power shell脚本访问kudu。请建议我。
答案 0 :(得分:1)
您可以参考下面的这个主题,了解如何在VSTS构建/发布中通过Azure PowerShell调用Kudu API:
Remove files and foldes on Azure before a new deploy from VSTS
关于通过Kudu的复制文件,您可以使用Command Kudu API(Post / api / command):
更新:
通过Kudu API调用Command的简单示例:
function RunCommand($dir,$command,$resourceGroupName, $webAppName, $slotName = $null){
$kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName
$kuduApiUrl="https://$webAppName.scm.azurewebsites.net/api/command"
$Body =
@{
"command"=$command;
"dir"=$dir
}
$bodyContent=@($Body) | ConvertTo-Json
Write-Host $bodyContent
Invoke-RestMethod -Uri $kuduApiUrl `
-Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} `
-Method POST -ContentType "application/json" -Body $bodyContent
}
RunCommand "site\wwwroot\bin\apache-tomcat-8.0.33\webapps" "copy xx.war ..\xx.war /y" "[resource group]" "[web app]"
答案 1 :(得分:1)
要访问Kudu API,请获取您的WebApp:
$app = Get-AzWebApp -ResourceGroupName "your RG" -Name "your App"
然后获取该应用程序的发布凭据:
$resourceName = "$($app.Name)/publishingcredentials";
$resourceType = "Microsoft.Web/sites/config";
$publishingCredentials = Invoke-AzResourceAction `
-ResourceGroupName $app.ResourceGroup `
-ResourceType $resourceType `
-ResourceName $resourceName `
-Action list `
-ApiVersion $apiVersion `
-Force;
格式化适合HTTP请求的用户名/密码:
$user = $publishingCredentials.Properties.PublishingUserName;
$pass = $publishingCredentials.Properties.PublishingPassword;
$creds = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("${user}:${pass}")));
最后,您可以访问Kudu API:
$header = @{
Authorization = "Basic $creds"
};
$kuduApiBaseUrl = "https://$($app.Name).scm.azurewebsites.net";
例如,检查是否安装了扩展程序:
$extensionName = "Microsoft.AspNetCore.AzureAppServices.SiteExtension";
$kuduApiUrl = "$kuduApiBaseUrl/api/siteextensions/$extensionName";
$response = Invoke-RestMethod -Method 'Get' -Uri $kuduApiUrl -Headers $header;
示例,获取可用扩展的列表:
$kuduApiUrl = "$kuduApiBaseUrl/api/extensionfeed";
$response = Invoke-RestMethod -Method 'Get' -Uri $kuduApiUrl -Headers $header;
示例,安装扩展程序:
$kuduApiUrl = "$kuduApiBaseUrl/api/siteextensions";
$response = Invoke-RestMethod -Method 'Put' -Uri $kuduApiUrl -Headers $header;
API的详细信息位于https://github.com/projectkudu/kudu/wiki/REST-API
还可以访问部署插槽。需要为该广告位获取应用配置,并且需要对基本URL进行少量修改。
答案 2 :(得分:0)
您可以使用以下代码从Powershell访问kudu api-
//function to Get webapp's publishing credentials
function Get-AzWebAppPublishingCredentials($resourceGroupName, $webAppName, $slotName = $null) {
if ([string]::IsNullOrWhiteSpace($slotName) -or $slotName.ToLower() -eq "production") {
$resourceType = "Microsoft.Web/sites/config"
$resourceName = "$webAppName/publishingcredentials"
}
else {
$resourceType = "Microsoft.Web/sites/slots/config"
$resourceName = "$webAppName/$slotName/publishingcredentials"
}
$publishingCredentials = Invoke-AzResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force
return $publishingCredentials
}
//function to get authorization header from publishing credentials
function Get-KuduApiAuthorisationHeaderValue($resourceGroupName, $webAppName, $slotName = $null) {
$publishingCredentials = Get-AzWebAppPublishingCredentials $resourceGroupName $webAppName $slotName
$ret = @{ }
$ret.header = ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
$ret.url = $publishingCredentials.Properties.scmUri
return $ret
}
//function to call kudu api e.g. to get a file from webapp
function Get-FileFromWebApp($resourceGroupName, $webAppName, $slotName = "", $kuduPath) {
$KuduAuth = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName
$kuduApiAuthorisationToken = $KuduAuth.header
$kuduApiUrl = $KuduAuth.url + "/api/vfs/$kuduPath"
Write-Host " Downloading File from WebApp. Source: '$kuduApiUrl'." -ForegroundColor DarkGray
$tmpPath = "$($env:TEMP)\$([guid]::NewGuid()).json"
$null = Invoke-RestMethod -Uri $kuduApiUrl `
-Headers @{"Authorization" = $kuduApiAuthorisationToken; "If-Match" = "*" } `
-Method GET `
-ContentType "application/json" `
-OutFile $tmpPath
$ret = (Get-Content $tmpPath) | Out-String | ConvertFrom-Json
Remove-Item $tmpPath -Force
return $ret
}