首页 > 解决方案 > 如何在 gitlab-ci 文件中使用 curl?

问题描述

在我的 gitlab-ci 文件中,我想使用命令curl来获取页面的结果并验证其内容,但我不知道如何使用它。

....................
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.*$/

请问你有什么想法吗?

标签: curlgitlab-ci

解决方案


我不太确定这会奏效;因为 YAML 解释器会吞噬各种特殊字符,例如该 http 中的 :。在经过数小时的努力后使其工作,这是我找到的解决方案。

    - |
      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"

这个脚本将使用 gitlab api 生成一个版本,所以比你所要求的更花哨。

请注意,CI_COMMIT_TAG_MESSAGE 是我的变量,希望将其添加到 GitLab。

最大的问题是找出所有需要转义的特殊字符。

此外,您在内容参数中交换了 ( 和 $ ;)


推荐阅读