24

In my gitlab-ci file, I want to use the command curl to get the result of a page and verify its content, but I don't know how to use it.

....................
server:check-quality:
  <<: *all-settings
  stage: check-quality
  <<: *tags_definition
  script:
  - echo "APPEL de CURL"
  - content=($curl http://example.com/sonar/api/qualitygates/project_status?projectKey=com.orange.catalog:feature-m752-conditionequals)
  - echo "content"
  - exit 0
  only:
  - develop
  - /^feature.*$/
  - /^hotfix.*$/

Have you any idea please?

6 Answers 6

23

I'm not so sure this would work; as the YAML interpreter will gobble up various special characters, such as the : in that http. To make it work after hours of struggling this is the solution I found.

    - |
      curl --fail --output "/dev/null" --silent --show-error --write-out "HTTP response: ${http_code}\n\n" \
        --data "{\"tag_name\": \"${CI_COMMIT_TAG}\", \"name\": \"${CI_PROJECT_NAME} ${CI_COMMIT_TAG}\", \"description\": \"${CI_COMMIT_TAG_MESSAGE:-No release notes.}\"}" \
        --header "Content-Type: application/json" \
        --header "Private-Token: ${CI_PRIVATE_TOKEN}" \
        --request POST \
        "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/releases"

This script will generate a release using the gitlab api, so a little more fancyfull then what you are requesting.

Note, that CI_COMMIT_TAG_MESSAGE is my variable, which will hopefully be added to GitLab.

The biggest problem was figuring out all the special characters that need escaping.

Also, you swapped your ( and $ in your content parameter ;)

2
  • in my case i have to put curl -X POST "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/releases" \ --header "PRIVATE-TOKEN:${CI_PRIVATE_TOKEN}" ... and the rest is ok
    – klapinski
    Dec 12, 2019 at 17:21
  • and the next thing i've noticed right now is http instead of https at the begging
    – klapinski
    Dec 12, 2019 at 17:46
21

This is how to use curl in GitLab CI YAML script with no hassle in 2021.

The following example CI pipeline uses curl to trigger the remote application build and deploy on Digital Ocean App Platform for the real use case sake.

Here curl uses URL arguments from GitLab variables and passes a JSON body with the request:

deploy:
  stage: deploy
  variables:
    DEPLOY_CURL_COMMAND_BODY: "'{\"force_build\":true}'"
    DEPLOY_CURL_COMMAND: 'curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $DO_APP_PLATFORM_API_TOKEN" --data $DEPLOY_CURL_COMMAND_BODY https://api.digitalocean.com/v2/apps/$DO_APP_PLATFORM_STAGE_FRONT_APP_ID/deployments'
  script:
    # The following echoes are just a debug output
    - echo "Stage Deploy to DigitalOcean App Platform"
    - echo "$DO_APP_PLATFORM_API_TOKEN"
    - echo "$DO_APP_PLATFORM_STAGE_FRONT_APP_ID"
    - echo $DEPLOY_CURL_COMMAND
    # This line actually executes curl command saved within the variable.
    - 'eval "$DEPLOY_CURL_COMMAND"'

The pipeline puts a command's argument into a variable. Then eval the variable. Note the single quotes wrapping the variable value and eval command. They are principal.

The variable uses as well the extrapolated repository level variables for secrets.

Note that GitLab YAML does not complain about a lone : colon sign. It complains about : - a colon followed by space sign. So the above example is universally applicable for both cases. But simpler implementation as per @Mavichow answer would serve well when there is no space after colon in the command line.

Note as well that you can use curl body parameter that requires being wrapped in single quotas itself. Note there is no space after comma in body variable content.

Some other ways to avoid : (colon followed by space) issues:

  after_script:
    # Note how the single quotes eliminate colon+space issue
    - 'MESSAGE="Tests finished with status: ${CI_JOB_STATUS}"'
    # Here pipe operator allows using colon+space on the next line
    - |
      curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"$MESSAGE\"}" "$SOME_URL"    

1
  • 3
    "Note that GitLab YAML does not complain about a lone : colon sign. It complains about : " was the key for me. Thanks! Apr 13, 2022 at 15:25
4

In script you can use curl like

script:
  - echo "APPEL de CURL"
  - curl http://example.com/sonar/api/qualitygates/project_status?projectKey=com.orange.catalog:feature-m752-conditionequals
  - echo "content"
  - exit 0
2

Can use | (multiline script) to bypass this like:

....................
server:check-quality:
  <<: *all-settings
  stage: check-quality
  <<: *tags_definition
  script:
  - echo "APPEL de CURL"
  - |
    content=($curl http://example.com/sonar/api/qualitygates/project_status?projectKey=com.orange.catalog:feature-m752-conditionequals)
  - echo "content"
  - exit 0
  only:
  - develop
  - /^feature.*$/
  - /^hotfix.*$/
1

I have modify some workaround and CURL with variable and works for me with below scripts:

testing:
  only:
    - master
  script:
    - curl --request GET --header "Authorization:cpanel user:$CPANEL_API_TOKEN" "$CPANEL_URL/execute/VersionControl/update?repository_root=%2Fhome%2Ftesting%2Fpublic_html%2Ftesting.domain.com&branch=master"
1

Here is the official Gitlab docs about curl in script : https://docs.gitlab.com/ee/ci/yaml/script.html

Sometimes, script commands must be wrapped in single or double quotes. For example, commands that contain a colon (:) must be wrapped in single quotes ('). The YAML parser needs to interpret the text as a string rather than a “key: value” pair.

You must sometimes use single quotes.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.