Gitlab CI Lint Api

Viewed 46

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!

1 Answers

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 '…'

EDIT:

I think you found a bug in GitLab's documentation.

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'

Should actually not use the fromjson filter:

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'

Everything in the first part of this answer still holds: jq cannot process YAML, you need yq for that. (But the GitLab docs never process the YAML document, they only print it)

Related