Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use curl in gitlab-ci file?

Tags:

curl

gitlab-ci

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?

like image 291
user3822342 Avatar asked Aug 10 '18 16:08

user3822342


4 Answers

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

The example CI pipeline uses curl to trigger the remote application build and deploy on Digital Ocean App Platform. 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 the command's argument in a variable. Then eval the variable. Note the sigle 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"    

like image 79
Valentine Shi Avatar answered Nov 08 '22 13:11

Valentine Shi


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 ;)

like image 18
oliver Avatar answered Nov 08 '22 14:11

oliver


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
like image 4
Talha Junaid Avatar answered Nov 08 '22 14:11

Talha Junaid


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.*$/
like image 2
Andreas Avatar answered Nov 08 '22 13:11

Andreas