将 Gitlab CI Trigger Curl 转换为 Powershell Invoke-RestMethod

Convert Gitlab CI Trigger Curl to Powershell Invoke-RestMethod

有谁知道是否可以将用于在 Gitlab-CI 中触发构建的 curl 命令转换为使用 Invoke-RestMethod 的 Powershell 等效命令?

示例 curl 命令:

curl --request POST \
  --form token=TOKEN \
  --form ref=master \
  --form "variables[UPLOAD_TO_S3]=true" \
  https://gitlab.example.com/api/v3/projects/9/trigger/builds

这取自 Gitlab 的 documentation 页面。

我发现了很多关于为 Powershell 转换 curl 脚本的帖子,但我没有任何运气让它工作。以下是我引用的一些链接:

如有任何帮助,我们将不胜感激。

您可以直接在URL中传递令牌和分支参数。至于变量,放到body变量中就可以了。

$Body = @{
    "variables[UPLOAD_TO_S3]" = "true"
}

Invoke-RestMethod -Method Post -Uri "https://gitlab.example.com/api/v3/projects/9/trigger/builds?token=$Token&ref=$Ref" -Body $Body

或者,您可以在正文参数中传递所有参数:

$form = @{token = $CI_JOB_TOKEN;ref = $BRANCH_TO_BUILD; "variables[SERVER_IMAGE_TAG]" = $CI_COMMIT_REF_NAME}
Invoke-WebRequest -Method POST -Body $form -Uri https://gitlab.example.com/api/v4/projects/602/trigger/pipeline -UseBasicParsing