0

I try to make a release with a GitLab-CI job by using GitLab API and cURL (cURL.exe within PowerShell).

But the GitLab variables converting are failed... (I tried several formats without any success)

release_job:
  stage: release
  tags:
    - windows-powershell
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - curl.exe --data '{\"tag_name\":\"'"$CI_COMMIT_TAG"'\", \"name\":\"'"$CI_COMMIT_TAG"'\", \"ref\":\"'"$CI_COMMIT_SHORT_SHA"'\"}' --header "Content-Type:application/json" --header "PRIVATE-TOKEN:<my_private_token>" --request POST "https://<my_gitlab>/api/v4/projects/388/releases"

When I convert myself the GitLab variables the script status is success.

release_job:
  stage: release
  tags:
    - windows-powershell
  rules:
    - if: $CI_COMMIT_TAG
  script:
    - curl.exe --data '{\"tag_name\":\"v0.1\", \"name\":\"v0.1\", \"ref\":\"05974ba7\"}' --header "Content-Type:application/json" --header "PRIVATE-TOKEN:<my_private_token>" --request POST "https://<my_gitlab>/api/v4/projects/388/releases"

Can you help me to understand please. In advance, thank you.

2 Answers 2

1

This is a lack of understanding about character escaping with PowerShell.

When using PowerShell, a double quotes character (") inside a string that enclosed by double quotes must be escaped first by a backtick `.

And with PowerShell requires that the backtick ` character be escaped if it appears in any string that is quoted with double quotes.

Source: Escaping in PowerShell by Hilltop Lab

Therefore the solution is:

curl.exe --data "{\`"tag_name\`":\`"$CI_COMMIT_TAG\`", \`"name\`":\`"$CI_COMMIT_TAG\`", \`"ref\`":\`"$CI_COMMIT_SHORT_SHA\`"}" --header "Content-Type:application/json" --header "PRIVATE-TOKEN:<my_private_token>" --request POST "https://<my_gitlab>/api/v4/projects/388/releases"
Sign up to request clarification or add additional context in comments.

5 Comments

The question was please help me to understand, giving a fish doesn't teach anybody to fish, perhaps add some explaining words since asker is not looking only for a solution but to comprehend, it looks like you could also explain comfortably.
And sometimes we don't have time to understand fine to complete an job... The mistake is about the character escaping and I think that depend of your system. Finally thank you for your useful comment.
Yes i get where you're coming from @purJuS and would only invite perhaps, that you join the review queues. top right corner, third icon. A critieria we are asked to consider is the level of explanation and understanding and despite finding the comment 'useful' i didn't see an update to the proposed solution, even if You actually just explained the problem. I think that when You spot the problem and suggest a fix which will work you deserve to get the accepted answer, also to help others searching on indexed stuff in the future. Perhaps consider including this explanation above in Your answer?
@T. Nielsen are you agree with my explanation? Next time, please consider my case. PowerShell and cURL aren't my expertise domain. It's just tools to complete my main jobs and unfortunately I don't have always time to understand everything... But through you I understand now something new :)
totally, that was exactly what I asked for, it was clear your reasoning looked spot on, I didn't doubt it. Must admit I didn't verify. Just noticed the please help we understand and it look like you could and now you did, thanks, sharing is caring :) to be honest it was my fault i realize now, I didn't see You answered Yourself :D sorry actually.
0

PRIVATE-TOKEN:$env:TOKEN_VAR_NAME

1 Comment

<my_private_token> and <my_gitlab> in my post is to avoid to expose and pollute. My lack of understanding is about the $CI_COMMIT_TAG and $CI_COMMIT_SHORT_SHA within data field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.