Help us learn about your current experience with the documentation. Take the survey.

CI 语法检查 API

  • Tier: Free, Premium, Ultimate
  • Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated

使用此 API 来验证您的 GitLab CI/CD 配置

这些端点使用 JSON 编码的 YAML 内容。在某些情况下,在发起请求前,使用像 jq 这样的第三方工具来正确格式化您的 YAML 内容会很有帮助。如果您希望保持 CI/CD 配置的格式,这会很有用。

例如,以下命令使用 JQ 来正确转义指定的 YAML 文件,将其编码为 JSON,并向 API 发出请求。

jq --null-input --arg yaml "$(<example-gitlab-ci.yml)" '.content=$yaml' \
| curl --url "https://gitlab.com/api/v4/projects/:id/ci/lint?include_merged_yaml=true" \
--header 'Content-Type: application/json' \
--data @-
  1. 创建一个名为 example-gitlab-ci.yml 的 YAML 文件:

    .api_test:
      rules:
        - if: $CI_PIPELINE_SOURCE=="merge_request_event"
          changes:
            - src/api/*
    deploy:
      extends:
        - .api_test
      rules:
        - when: manual
          allow_failure: true
      script:
        - echo "hello world"
  2. 要转义并编码一个输入的 YAML 文件 (example-gitlab-ci.yml),然后将其 POST 到 GitLab API,请创建一个结合了 curljq 的单行命令:

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

解析此 API 的响应

要重新格式化 CI 语法检查 API 的响应,您可以:

  • 将 CI 语法检查响应直接通过管道传递给 jq

  • 将 API 响应存储为文本文件,并将其作为参数提供给 jq,如下所示:

    jq --raw-output '.merged_yaml | fromjson' <your_input_here>

例如,这个 JSON 数组:

{"valid":"true","errors":[],"merged_yaml":"---\n.api_test:\n  rules:\n  - if: $CI_PIPELINE_SOURCE==\"merge_request_event\"\n    changes:\n    - src/api/*\ndeploy:\n  rules:\n  - when: manual\n    allow_failure: true\n  extends:\n  - \".api_test\"\n  script:\n  - echo \"hello world\"\n"}

当被解析和重新格式化后,生成的 YAML 文件包含:

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

验证新的 CI/CD 配置

为指定项目验证新的 .gitlab-ci.yml 配置。 此端点在项目上下文中验证 CI/CD 配置,包括:

  • 使用项目的 CI/CD 变量。
  • 在项目文件中搜索 include:local 条目。
POST /projects/:id/ci/lint
属性 类型 必需 描述
content string CI/CD 配置内容。
dry_run boolean 运行流水线创建模拟,或仅执行静态检查。默认值:false
include_jobs boolean 是否应在响应中包含静态检查或流水线模拟中会存在的作业列表。默认值:false
ref string dry_runtrue 时,设置用于验证 CI/CD YAML 配置的分支或标记上下文。未设置时,默认为项目的默认分支。

示例请求:

curl --header "Content-Type: application/json" \
  --url "https://gitlab.example.com/api/v4/projects/:id/ci/lint" \
  --data @- <<'EOF'
{
  "content": "{
    \"image\": \"ruby:2.6\",
    \"services\": [\"postgres\"],
    \"before_script\": [
      \"bundle install\",
      \"bundle exec rake db:create\"
    ],
    \"variables\": {
      \"DB_NAME\": \"postgres\"
    },
    \"stages\": [\"test\", \"deploy\", \"notify\"],
    \"rspec\": {
      \"script\": \"rake spec\",
      \"tags\": [\"ruby\", \"postgres\"],
      \"only\": [\"branches\"]
    }
  }"
}
EOF

示例响应:

  • 有效配置:

    {
      "valid": true,
      "merged_yaml": "---\ntest_job:\n  script: echo 1\n",
      "errors": [],
      "warnings": [],
      "includes": []
    }
  • 无效配置:

    {
      "valid": false,
      "errors": [
        "jobs config should contain at least one visible job"
      ],
      "warnings": [],
      "merged_yaml": "---\n\".job\":\n  script:\n  - echo \"A hidden job\"\n",
      "includes": []
    }

验证现有的 CI/CD 配置

为指定项目验证现有的 .gitlab-ci.yml 配置。 此端点在项目上下文中验证 CI/CD 配置,包括:

  • 使用项目的 CI/CD 变量。
  • 在项目文件中搜索 include:local 条目。
GET /projects/:id/ci/lint
属性 类型 必需 描述
content_ref string CI/CD 配置内容取自此提交 SHA、分支或标记。未设置时,默认为项目默认分支头部的 SHA。
dry_run boolean 运行流水线创建模拟,或仅执行静态检查。
dry_run_ref string 如果 dry_runtrue,则设置用于验证 CI/CD YAML 配置的分支或标记上下文。未设置时,默认为项目的默认分支。
include_jobs boolean 是否应在响应中包含静态检查或流水线模拟中会存在的作业列表。默认值:false
ref string (已弃用) 当 dry_runtrue 时,设置用于验证 CI/CD YAML 配置的分支或标记上下文。未设置时,默认为项目的默认分支。请改用 dry_run_ref
sha string (已弃用) CI/CD 配置内容取自此提交 SHA、分支或标记。未设置时,默认为项目默认分支头部的 SHA。请改用 content_ref

示例请求:

curl --url "https://gitlab.example.com/api/v4/projects/:id/ci/lint"

示例响应:

  • 有效配置,其中 include.yml 作为包含的文件 并且 include_jobs 设置为 true

    {
      "valid": true,
      "errors": [],
      "warnings": [],
      "merged_yaml": "---\ninclude-job:\n  script:\n  - echo \"An included job\"\njob:\n  rules:\n  - if: \"$CI_COMMIT_BRANCH\"\n  script:\n  - echo \"A test job\"\n",
      "includes": [
        {
          "type": "local",
          "location": "include.yml",
          "blob": "https://gitlab.example.com/test-group/test-project/-/blob/ef5014c045873c5c4ffeb7a2f5be021a1d3ed703/include.yml",
          "raw": "https://gitlab.example.com/test-group/test-project/-/raw/ef5014c045873c5c4ffeb7a2f5be021a1d3ed703/include.yml",
          "extra": {},
          "context_project": "test-group/test-project",
          "context_sha": "ef5014c045873c5c4ffeb7a2f5be021a1d3ed703"
        }
      ],
      "jobs": [
        {
          "name": "include-job",
          "stage": "test",
          "before_script": [],
          "script": [
            "echo \"An included job\""
          ],
          "after_script": [],
          "tag_list": [],
          "only": {
            "refs": [
              "branches",
              "tags"
            ]
          },
          "except": null,
          "environment": null,
          "when": "on_success",
          "allow_failure": false,
          "needs": null
        },
        {
          "name": "job",
          "stage": "test",
          "before_script": [],
          "script": [
            "echo \"A test job\""
          ],
          "after_script": [],
          "tag_list": [],
          "only": null,
          "except": null,
          "environment": null,
          "when": "on_success",
          "allow_failure": false,
          "needs": null
        }
      ]
    }
  • 无效配置:

    {
      "valid": false,
      "errors": [
        "jobs config should contain at least one visible job"
      ],
      "warnings": [],
      "merged_yaml": "---\n\".job\":\n  script:\n  - echo \"A hidden job\"\n",
      "includes": []
    }