简体   繁体   中英

Gitlab CI Lint Api

Lately, I have been trying to access the Gitlab CI Lint Api via bash using curl. for that I have been trying to strictly follow the documentation on Gitlab . According to the Gitlab documentation this fragment should return the formatted yml:

jq --null-input --arg yaml "$(<example-gitlab-ci.yml)" '.content=$yaml' \
| curl "https://gitlab.com/api/v4/ci/lint?include_merged_yaml=true" \
--header 'Content-Type: application/json' --data @- \
| jq --raw-output '.merged_yaml | fromjson'

I hove modified the Request with my credentials and I get a valid response, when I shorten the jq query to

jq --raw-output '.merged_yaml'

and I recieve

"---
\".api_test\":
  rules:
  - if: $CI_PIPELINE_SOURCE==\"merge_request_event\"
    changes:
    - src/api/*
deploy:
  rules:
  - when: manual
    allow_failure: true
  extends:
  - \".api_test\"
  script:
  - echo \"hello world\"
","includes":[],"jobs":[{"name":"deploy","stage":"test","before_script":[],"script":["echo \"hello world\""],"after_script":[],"tag_list":[],"only":null,"except":null,"environment":null,"when":"on_success","allow_failure":false,"needs":null}],"status":"valid"}

When i let this code run with the fromjson option I get an error:

jq: error (at <stdin>:0): Invalid numeric literal at line 2, column 0 (while parsing '---
".api_test":
  rules:
[...]

I am at the End of my wits, pls help me!

jq is a JSON processor. A YAML document is not JSON (but a JSON document is a YAML document). YAML is a superset of JSON.

There are other tools to process YAML, such as yq , which uses the familiar syntax from jq, but doesn't support every feature of jq.

So you will probably need run something similar to the following:

curl … | yq '.merged_yaml' | yq '…'

or perhaps (but that shouldn't be required, since yq can handle JSON files just fine):

curl … | jq '.merged_yaml' | yq '…'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM