无法通过jenkins使用powershell在azure上上载zip

2w2cym1i  于 2021-06-20  发布在  Kudu
关注(0)|答案(2)|浏览(364)

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

$username = "`$347testpass"
$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
lrpiutwd

lrpiutwd1#

尝试使用 -DisableKeepAliveInvoke-RestMethod .

cqoc49vn

cqoc49vn2#

最后,在添加超时、更改方法类型、安全协议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"

相关问题