通过HTTP请求将文件上传到Jenkins

9bfwbjaz  于 2023-04-05  发布在  Jenkins
关注(0)|答案(2)|浏览(296)

我有一个Jenkins作业,需要一个文件作为参数(我使用的是文件参数插件)。当我调用Jenkins时,我注意到文件参数没有发送,但其他参数(CHOICE和string),我做错了什么?

$credPair = "user:token";
$encodedCredentials = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($credPair));
$headers = @{
    "Authorization" = "Basic $encodedCredentials";
    "Content-Type" = "multipart/form-data";
}

$choiceParam = "option"
$stringParam = "value"
$file = "file_path"
$Body = @{ "File" = [IO.File]::ReadAllText($file); }

$url = "https://server/job/buildWithParameters?CHOICE=" + $choiceParam + '&string' + $stringParam

try {
    Invoke-RestMethod -Uri $url -Headers $headers -Method Post -Body $Body
}
catch {
    Write-Host "StatusCode: " $_.Exception.Response.StatusCode.value__
    Write-Host "StatusDescription: " $_.Exception.Response.StatusDescription
    write-host "Error details: " $_.ErrorDetails.Message
    exit
}

我已经试过下一个问题,但没有成功multipart/form-data file upload with PowerShellpowershell invoke-restmethod multipart/form-dataHow to send multipart/form-data with PowerShell Invoke-RestMethodUsing PowerShell Invoke-RestMethod to POST large binary multipart/form-data
我想使用PowerShell通过HTTP Post请求向Jenkins发送文件参数

kpbpu008

kpbpu0081#

@zett42我解决了,显然只有在你把文件的形式,其他参数都放在网址

$credPair = "user:token";
$encodedCredentials = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($credPair));
$headers = @{
    "Authorization" = "Basic $encodedCredentials";
}

$choiceParam = "option"
$stringParam = "value"
$file = "file_path"

$form = @{
    INSERT_NAME_OF_FILE_PARAM = Get-Item $file
}

$url = "https://server/job/INSERT_JOBNAME/buildWithParameters?INSERT_NAME_OF_CHOICE_PARAM=" + $choiceParam + '&INSERT_NAME_OF_STRING_PARAM=' + $stringParam

try {
    Invoke-RestMethod -Uri $url -Headers $headers -Method Post -Form $form
}
catch {
    Write-Host "StatusCode: " $_.Exception.Response.StatusCode.value__
    Write-Host "StatusDescription: " $_.Exception.Response.StatusDescription
    write-host "Error details: " $_.ErrorDetails.Message
    exit
}
42fyovps

42fyovps2#

**注意:**实际上,下面的解决方案只适用于 freestyle 作业,如果您需要 pipeline 作业的解决方案,可以使用this solution(需要文件参数plugin)。

我已经使用Invoke-RestMethod和参数-Form成功地将文件上传到Jenkins。
这样的东西应该是可行的:

$credPair = "user:token";
$encodedCredentials = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($credPair));
$headers = @{
    "Authorization" = "Basic $encodedCredentials";
    # NOTE: no Content-Type header, Invoke-RestMethod adds it automatically when using -Form
}

$choiceParam = "option"
$stringParam = "value"
$file = "file_path"

$form = @{
   json = ConvertTo-Json -Compress @{
       parameter = @(
           @{ name = "INSERT_NAME_OF_CHOICE_PARAM"; value = $choiceParam }
           @{ name = "INSER_NAME_OF_STRING_PARAM"; value = $stringParam }

           # IMPORTANT: value of file must be equal to file key used to assign FileInfo below
           @{ name = "INSERT_NAME_OF_FILE_PARAM"; file = "file1" }
       )
   }

   # Don't read the file on your own, Invoke-RestMethod will do it
   file1 = [IO.FileInfo] $file
}

# NOTE: /build instead of /buildWithParameters
$url = "https://server/job/INSERT_JOBNAME/build"

try {
    Invoke-RestMethod -Uri $url -Headers $headers -Method Post -Form $form
}
catch {
    Write-Host "StatusCode: " $_.Exception.Response.StatusCode.value__
    Write-Host "StatusDescription: " $_.Exception.Response.StatusDescription
    write-host "Error details: " $_.ErrorDetails.Message
    exit
}

作为参考,我得到了从this answer上传文件到Jenkins作业的基本原理。

相关问题