如何使用 Azure CLI 为使用 ARM 模板的新 created/updated 应用服务获取发布凭据

How to get publishingcredentials using Azure CLI for newly created/updated appservice using ARM template

我正在尝试使用 ARM 模板自动创建 Java 应用服务,然后利用 FTP 发布方法为 [=26] 上传 Java WAR 文件=].

以下在 Azure CLI 中不起作用 -

$ azure resource show <resource-group> <appservice-name>/publishingcredentials Microsoft.Web/sites/config 2015-08-01

error:   The resource type could not be found in the namespace 'Microsoft.Web' for api version '2015-08-01'

但这在 Azure PowerShell 中有效 -

$resource = Invoke-AzureRmResourceAction -ResourceGroupName <resource-group> -ResourceType Microsoft.Web/sites/config -ResourceName <appservice-name>/publishingcredentials -Action list -ApiVersion 2015-08-01 -Force
$resource.Properties

我一直在参考资源浏览器 https://resources.azure.com 中的名称空间,但无法找到正确的语法来获取 FTP 用户名和密码,稍后我可以在脚本中使用它们来上传 WAR 文件.

Azure CLI azure resource show 类似于 Azure PowerShell Get-AzureRmResource,它始终使用 "GET" 方法。如果您添加“-vv”选项,您将看到它正在使用的 REST API。另一方面,PowerShell Invoke-AzureRmResourceAction 使用的是 "POST" 方法。如果您向 Invoke-AzureRmResourceAction 添加“-debug”选项,您将能够看到 REST API.

https://management.azure.com/subscriptions/<your subscription id>/resourceGroups/<resource group>/providers/Microsoft.Web/sites/<app service>/config/publishingcredentials/list?api-version=2015-08-01

这是调用此 REST 的 PowerShell 脚本 API:

Add-Type -Path 'C:\Program Files\Microsoft Azure Active Directory Connect\Microsoft.IdentityModel.Clients.ActiveDirectory.dll'

# The tenant ID of you Subscription. You can use tenant name instead.
$tenantID = "<the tenant ID of your Subscription>"

# You can leave the variables as what they are, if you are under Azure Cloud Environment.
$loginEndpoint = "https://login.windows.net/"
$managementResourceURI = "https://management.core.windows.net/"
$redirectURI = New-Object System.Uri ("urn:ietf:wg:oauth:2.0:oob")
$clientID = "1950a258-227b-4e31-a9cf-717495945fc2"

# Fill in the below variables.
$subscriptionID = "<your subscription id>"
$resouceGroup = "<your resource group>"
$appService = "<your app service>"
$username = "<your Azure account>"


# Constructing the authentication string.
$authString = $loginEndpoint + $tenantID

# Use the above authentication string to create an authentication context.
$authenticationContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext ($authString, $false)

$promptBehaviour = [Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior]::Auto

$userIdentifierType = [Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifierType]::RequiredDisplayableId

$userIdentifier = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifier ($username, $userIdentifierType)


# Prompt for signing in.
$authenticationResult = $authenticationContext.AcquireToken($managementResourceURI, $clientID, $redirectURI, $promptBehaviour, $userIdentifier); 

# construct authorization header for the REST API.
$authHeader = $authenticationResult.AccessTokenType + " " + $authenticationResult.AccessToken
$headers = @{"Authorization"=$authHeader; "Content-Type"="application/json"}

# Invoke the REST API.
Invoke-RestMethod -Method POST -Uri "https://management.azure.com/subscriptions/$subscriptionID/resourceGroups/$resourceGroup/providers/Microsoft.Web/sites/$appService/config/publishingcredentials/list?api-version=2015-08-01" `
                  -Headers $headers

如果您在 Linux 系统下,您可以将 curl 与 OAuth 2 一起使用。下面的 bash 脚本将为您提供网络应用程序的发布凭据。但是,为了使用我的脚本,您需要 create a service principle and install curl in you Linux system.

#!/bin/bash

tenantID="<the tenant id of your subscription>"

client_id="<the client id of your AD application>"

client_secret="<a key you added to your AD application>"

body="grant_type=client_credentials&client_id=$client_id&client_secret=$client_secret&resource=https%3A%2F%2Fmanagement.core.windows.net%2F"

authorization=$(curl -X POST -H "Content-Type: application/x-www-form-urlencoded" --data-ascii "$body" "https://login.microsoftonline.com/$tenantID/oauth2/token" | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["token_type"]+" "+obj["access_token"])')


subscriptionID="<your subscription id>"
resourceGroup="<the resource group of you web app>"
appService="<your web app>"

curl -X POST -H "Authorization: $authorization" --data-ascii "" "https://management.azure.com/subscriptions/$subscriptionID/resourceGroups/$resourceGroup/providers/Microsoft.Web/sites/$appService/config/publishingcredentials/list?api-version=2015-08-01"

我找到了替代方案。

如果资源组是手动创建的,并且 FTP 手动设置资源组下任何网站的发布凭据。然后这将为资源组设置一个备用 FTP 凭据 - 不特定于任何网站(或者它可能是整个帐户的全局)。

每个网站一旦创建,就会在 FTP 服务器上设置不同的域,备用凭证适用于任何网站 - ftp:///username@waws-prod-sn1-033.ftp.azurewebsites.windows.net.

现在从 Jenkin 开始,如果我们在同一资源组下基于 git 分支名称创建一个新的站点名称,我们也可以使用上面为新站点名称预配置的 FTP 凭据上传申请档案。

我希望有一种方法可以使用 Azure CLI 至少检索备用凭据。资源浏览器显示在 - https://management.azure.com/providers/Microsoft.Web/publishingUsers/web?api-version=2015-08-01.

或者即使 "siteConfig" ARM 模板下的属性可以设置来配置 publishingUsername 和 publishingPassword。我试过这个选项,但这些属性被忽略了。