无法通过 Jenkins 使用 PowerShell 在 azure 上上传 zip

Unable to upload zip on azure using PowerShell through Jenkins

我正在尝试在 Azure 上自动部署构建。为此,我使用 powershell 脚本在 azure 上上传 zip。脚本分为两部分 -> 首先清理 wwwroot 文件夹,第二部分是将 zip 上传到 wwwroot。当我通过 Powershell exe 运行 脚本时,它 运行 成功,但当通过 Jenkins 运行 时出错。奇怪的是,第一部分成功 运行 但第二部分出错。 Powershell 脚本:

$username = "`7testpass"
$password = "xyz"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))
$userAgent = "powershell/1.0"

#Clean wwwroot folder
$apiUrl1 = "https://347testpass.scm.azurewebsites.net/api/command"
$commandBody = @{
    command = "rmdir D:\home\site\wwwroot /Q /S"
  }
Invoke-RestMethod -Uri $apiUrl1 -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST ` -ContentType "application/json" -Body (ConvertTo-Json $commandBody) | Out-Null

#Upload zip file
$apiUrl = "https://347testpass.scm.azurewebsites.net/api/zip/site/wwwroot/"
$filePath = "D:\AzureWeb\Upload\qwerty.zip"
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method PUT -InFile $filePath -ContentType "multipart/form-data"

Jenkins 控制台中的错误显示:

Invoke-RestMethod : The request was aborted: The request was canceled.
At C:\Users\Harsh.Sharma\AppData\Local\Temp\hudson8158442147919891501.ps1:34 
char:1
+ Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f 
$base64A ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-RestMethod], WebExcept 
   ion
    + FullyQualifiedErrorId : System.Net.WebException,Microsoft.PowerShell.Com 
   mands.InvokeRestMethodCommand

尝试将 -DisableKeepAliveInvoke-RestMethod 结合使用。

在添加超时、更改方法类型、安全协议 tls12 等几项后,它终于可以工作了。

$apiUrl = "https://347testpass.scm.azurewebsites.net/api/zipdeploy"
$filePath = "C:\BuildDeploymentAzureEnterprise3.4\Unzipped\AzureBuildFiles.zip"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-RestMethod -Uri $apiUrl -DisableKeepAlive -TimeoutSec 1000 -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method POST -InFile $filePath -ContentType "multipart/form-data"