无法在Gitlab Yaml管道中对JSON进行编码



我无法使用PowerShell(Windows运行程序(从YML Gitlab Pipeline提交Dropbox API请求。API需要传递一个JSON作为头参数,并且将该JSON编码到YML Gitlab Pipeline中不起作用:

`--header "Dropbox-API-Arg:{"path":"/$BUILD_FILENAME"}"

完整的脚本步骤如下,因为它现在在YML:中定义

- C:WindowsSystem32curl.exe -X POST https://content.dropboxapi.com/2/files/upload --header "Authorization:Bearer $DROPBOX_TOKEN" --header "Content-Type:application/octet-stream" --header "Dropbox-API-Arg:{"path":"/$BUILD_FILENAME"}" --data-binary @$BUILD_FULLFILENAME

响应错误如下:

Error in call to API function "files/upload": HTTP header "Dropbox-API-Arg": could not decode input as JSONcurl: (3) Port number ended with ''

我可以在运行程序所在的同一台电脑上执行Windows PowerShell中的上述命令,而不会出现任何问题:

curl -X POST https://content.dropboxapi.com/2/files/upload --header "Authorization: Bearer XXX" --header "Dropbox-API-Arg: {"path": "/README.md"}" --header "Content-Type: application/octet-stream" --data-binary @README.md

如何正确编码JSON,以便按预期执行并传递给CURL?

我认为以下内容应该有效。它使用--%,它阻止PowerShell在调用$fullCommand时解析curl命令的参数。这允许在Dropbox-API-Arg标头中保留双引号。

$fullCommand = 'C:WindowsSystem32curl.exe --%'
$fullCommand += ' -X POST https://content.dropboxapi.com/2/files/upload'
$fullCommand += ' --header "Authorization:Bearer ' + $DROPBOX_TOKEN + '"'
$fullCommand += ' --header "Content-Type:application/octet-stream"'
$fullCommand += ' --header "Dropbox-API-Arg:{"path":"' + $BUILD_FILENAME + '"}"'
$fullCommand += ' --data-binary @' + $BUILD_FULLFILENAME
Invoke-Expression $fullCommand

实时演示

.gitlab-ci.yml

stages:
- run_on_windows_test
run_on_windows_test_1:
stage: run_on_windows_test
tags:  
- shared-windows
- windows
- windows-1809
variables:
DROPBOX_TOKEN: "THE_TOKEN"
BUILD_FILENAME: "test.txt"
BUILD_FULLFILENAME: C:WindowsTEMPtest.txt

script:
- fsutil file createnew C:WindowsTEMPtest.txt 128
- $fullCommand = 'C:WindowsSystem32curl.exe --% '
- $fullCommand += '-X POST https://requestinspector.com/inspect/01ems2t2f0r4dkfyzk1d820e1t'
- $fullCommand += ' --header "Authorization:Bearer ' + $DROPBOX_TOKEN + '"'
- $fullCommand += ' --header "Content-Type:application/octet-stream"'
- $fullCommand += ' --header "Dropbox-API-Arg:{"path":"' + $BUILD_FILENAME + '"}"'
- $fullCommand += ' --data-binary @' + $BUILD_FULLFILENAME
- Invoke-Expression $fullCommand
- $fullCommand = 'C:WindowsSystem32curl.exe --% '
- $fullCommand += '-X POST http://requestbin.net/r/1b135t41'
- $fullCommand += ' --header "Authorization:Bearer ' + $DROPBOX_TOKEN + '"'
- $fullCommand += ' --header "Content-Type:application/octet-stream"'
- $fullCommand += ' --header "Dropbox-API-Arg:{"path":"' + $BUILD_FILENAME + '"}"'
- $fullCommand += ' --data-binary @' + $BUILD_FULLFILENAME
- Invoke-Expression $fullCommand

最新更新