Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Gitlab CI Trigger Curl to Powershell Invoke-RestMethod

Does anyone know if it's possible to convert the curl command used to trigger builds in Gitlab-CI to a Powershell equivalent using Invoke-RestMethod?

Example curl command:

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

This was taken from Gitlab's documentation page.

I found quite a few postings about converting a curl script for Powershell but I haven't had any luck in getting it to work. Here are some of the links I referenced:

  • How to send multipart/form-data with PowerShell Invoke-RestMethod
  • PowerShell equivalent of curl
  • Running curl via powershell - how to construct arguments?

Any help would be appreciated.

like image 592
Jay Soyer Avatar asked Aug 18 '16 13:08

Jay Soyer


2 Answers

You can pass the token and the branch parameters directly in the URL. As for variables, putting it into the body variable should do the trick.

$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
like image 161
Fairy Avatar answered Nov 05 '22 21:11

Fairy


Alternatively you can pass all arguments in the body parameter:

$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
like image 44
Klepto Avatar answered Nov 05 '22 21:11

Klepto