如何使用 powershell 轮询 kudu api 响应以获取部署状态

How to poll kudu api response to get the deployment status using powershell

使用 KUDU 部署 Azure 功能api

这是我的 url:

$apiUrl = 'https://myapp.scm.azurewebsites.net/api/zipdeploy?isAsync=true'

调用休息方法脚本:

Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method POST -InFile $zipFile -ContentType "multipart/form-data" -OutFile sample.txt -Verbose 

我得到的输出:

VERBOSE: POST https:// myapp.scm.azurewebsites.net /api/zipdeploy?isAsync=true with -1-byte payload
VERBOSE: received 0-byte response of content type  

我的问题是我没有收到任何回复并且 sample.txt 是一个空文件。 在不使用 isAsync 的情况下使用 url 时,我在 teamcity 中遇到超时异常,这就是我进行异步部署的原因

我可以验证门户看到部署是否成功,但是如何通过 powershell 脚本验证部署状态。

在下面的网站中是这样提到的:

Async support: By adding ?isAsync=true to the URL, the deployment will run asynchronously. The Location header of the response will contain the deployment URL that can be polled for deployment status. Webhooks can be used for fully asynchronous notification of completion.

https://github.com/projectkudu/kudu/wiki/Deploying-from-a-zip-file

在这个document里面是这样提到的-

The Location header of the response will contain a link to a pollable deployment status.

但我不知道如何获取位置header。请帮忙。

太疯狂了,Invoke-RestMethod 似乎 return 没有 headers object,但是 Invoke-WebRequest 有。您应该能够以最小的努力重写您的请求以使用后者。

然后您可以按如下方式访问 headers:

PS> $headers = Invoke-WebRequest -Uri https://jsonplaceholder.typicode.com/posts/1 | 
                   select -expand Headers

PS> $headers

Key                              Value
---                              -----
Connection                       keep-alive
Vary                             Origin, Accept-Encoding
...
Server                           cloudflare
...

PS> $headers.Server
cloudflare

回到 Invoke-RestMethodthis PR 已合并到 PowerShell 中,但我猜它只是在 Core 中而不是在 Windows PowerShell 中(完整的框架之一)。

您可以安装 PowerShell Core 6.0(side-by-side 与 Windows PowerShell 一起工作)以使用新的 -ResposneHeadersVariable 参数,切换到 Invoke-WebRequest 可能更明智.

这是您对 PowerShell Core 6.1.0-preview2 的请求(也应该适用于稳定版本,2017 年 9 月 25 日之后的任何版本):

PS C:\Program Files\PowerShell-preview> Invoke-RestMethod `
        https://jsonplaceholder.typicode.com/posts/1 -ResponseHeadersVariable headers

PS C:\Program Files\PowerShell-preview> $headers

Key                              Value
---                              -----
Cache-Control                    {public, max-age=14400}
Connection                       {keep-alive}
Date                             {Fri, 11 May 2018 07:11:05 GMT}
Pragma                           {no-cache}
...

根据 this blog post Windows PowerShell 没有未来,现在所有的努力都在 PowerShell Core 上。