Reputation: 377
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?
Upvotes: 26
Views: 82585
Reputation: 7800
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 (namely DO_APP_PLATFORM_API_TOKEN
, DO_APP_PLATFORM_STAGE_FRONT_APP_ID
).
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"
Upvotes: 23
Reputation: 101
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.
Upvotes: 2
Reputation: 559
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.*$/
Upvotes: 2
Reputation: 1223
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"
Upvotes: 1
Reputation: 811
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 ;)
Upvotes: 24
Reputation: 2405
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
Upvotes: 4