简体   繁体   English

Gitlab CI/CD - 向 gitlab UI 发送评论/警报?

[英]Gitlab CI/CD - sending comments/alerts to the gitlab UI?

Currently I have this line in my .gitlab-ci.yml file:目前我的 .gitlab-ci.yml 文件中有这一行:

if (( $coverage < $MIN_COVERAGE )) ; then echo "$coverage% of code coverage below threshold of $MIN_COVERAGE%" && exit 1 ; else exit 0 ; fi

$coverage is the test coverage of the code, determined with pytest-cov $coverage是代码的测试覆盖率,由 pytest-cov 确定

$MIN_COVERAGE is a specified minimum level of test coverage which $coverage shouldn't drop below $MIN_COVERAGE是指定的最低测试覆盖率水平,$coverage 不应低于该水平

Currently, this causes the pipeline to fail if, for instance, coverage is 70% and min_coverage is 80%.目前,如果覆盖率为 70% 且 min_coverage 为 80%,则会导致管道失败。 A message is also printed to the terminal: "$coverage% of code coverage below threshold of $MIN_COVERAGE%"还会向终端打印一条消息:“$coverage% of code coverage below the threshold of $MIN_COVERAGE%”

However, this message is only displayed in the terminal of the gitlab job, so if someone wanted to see why and by how much their pipeline failed they would need to go into the job terminal and look at the output.但是,此消息仅显示在 gitlab 作业的终端中,因此如果有人想了解他们的管道失败的原因和程度,他们需要进入作业终端并查看输出。

Instead of having this echo to the job terminal, is there a way to get this message to output somewhere on the gitlab UI?有没有办法让这个消息输出到 gitlab UI 的某个地方,而不是让这个回显到工作终端?

If you have a GitLab Premium subscription or higher, you can use metrics reports to expose any metric, including coverage percentage, in the MR UI.如果您订阅了 GitLab Premium 或更高版本,则可以使用指标报告在 MR UI 中公开任何指标,包括覆盖百分比。
In all tiers of GitLab, coverage visualization is also available, but it's unclear to me if this displays the overall coverage percentage.在 GitLab 的所有层级中, 覆盖可视化也是可用的,但我不清楚这是否显示了整体覆盖百分比。

Alternatively, you can use the API to add comments to the merge request (you can get the MR ID from predefined variables in the job).或者,您可以使用 API 向合并请求添加注释(您可以从作业中的预定义变量中获取 MR ID)。 However, you will need to supply an API token to the CI job -- you cannot use the builtin job token to add comments.但是,您需要为 CI 作业提供 API 令牌——您不能使用内置作业令牌来添加注释。

Here's how to create a new Merge Request Note/Comment using the GitLab API.以下是如何使用 GitLab API 创建新的合并请求注释/评论。

  script:
    # Project -> Settings -> Access Tokens, Create token with API scope.
    # Project -> Settings -> CI/CD -> Variables, Store as CI_API_TOKEN
    # GET /merge_requests?scope=all&state=opened&source_branch=:branch_name
    - |
      merge_request_iid=$( \
        curl --request GET \
          --header "PRIVATE-TOKEN: ${CI_API_TOKEN}" \
          "${CI_API_V4_URL}/merge_requests?scope=all&state=opened&source_branch=${CI_COMMIT_REF_NAME}" | \
        jq .[0].iid \
      )
    # POST /projects/:id/merge_requests/:iid/notes
    - json_data='{"body":"Your message, here"}'
    - |
      echo $json_data |
      curl --request POST \
        --header "PRIVATE-TOKEN: ${CI_API_TOKEN}" \
        --header "Content-Type: application/json" \
        --data @- \
        "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/merge_requests/${merge_request_iid}/notes"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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