2

For a project we would like to call an API and save the result of this curl in a variable.

The pipeline is built like this:

stages:
  - download

scan:
  stage: download
  image: ubuntu
  variables:
    TOKEN: 
  
  script:

    - apk add curl
    - apk add jq
    
    - TOKEN=$('curl -H "Content-Type: application/json" -d "{\"username\":\"$USER", \"password\":\"$PWD"}" https://example.org/api2/authenticate | jq .token ')
    #- echo $TOKEN

I got this error:

This GitLab CI configuration is invalid: jobs:scan:script config should be a string or a nested array of strings up to 10 levels deep.

The curl command (removed from the $(), but kept the single quotes to wrap the double quotes) works regularly and returns the string with the token inside. The only problem turns out to be encapsulating the result in a variable. What can be done?

Thank you.

1
  • Why did you add single quotes? Those prevent the command from running. Commented Feb 16, 2022 at 19:00

1 Answer 1

8

Try instead of this

- TOKEN=$('curl -H "Content-Type: application/json" -d "{\"username\":\"$USER", \"password\":\"$PWD"}" https://example.org/api2/authenticate | jq .token ')

The following

- |
  TOKEN=$(curl -H "Content-Type: application/json" -d "{\"username\":\"$USER\", \"password\":\"$PWD\"}" https://example.org/api2/authenticate | jq .token)

P.S. I would suggest begin by running

- |
  curl -H "Content-Type: application/json" -d "{\"username\":\"$USER\", \"password\":\"$PWD\"}" https://example.org/api2/authenticate 

In order to debug the curl command output, before running jq

Sign up to request clarification or add additional context in comments.

2 Comments

Super! Thank you, it works. can you explain me the reason?
If it indeed works please approve the answer to help future users, so they could more easily find the solution I will also update the answer with an explanation

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.