Gitlab CI/CD - sending comments/alerts to the gitlab UI?
Asked Answered
N

3

5

Currently I have this line in my .gitlab-ci.yml file:

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

$MIN_COVERAGE is a specified minimum level of test coverage which $coverage shouldn't drop below

Currently, this causes the pipeline to fail if, for instance, coverage is 70% and min_coverage is 80%. A message is also printed to the terminal: "$coverage% of code coverage below 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.

Instead of having this echo to the job terminal, is there a way to get this message to output somewhere on the gitlab UI?

Niel answered 11/5, 2022 at 21:10 Comment(0)
E
7

Here's how to create a new Merge Request Note/Comment using the 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"

Encroachment answered 17/5, 2022 at 14:49 Comment(0)
G
4

In addition, you can use the GitLab CLI tool (glab) in your CI pipeline:

comment-mr:
  image: registry.gitlab.com/gitlab-org/cli:latest
  variables:
    GIT_STRATEGY: none
    GITLAB_TOKEN: $MR_AUTOMATION_TOKEN # Project -> Settings -> Access Tokens (api, api_read scopes)
  script:
    - if [ -z "$CI_OPEN_MERGE_REQUESTS" ]; then echo "No opened MR"; exit 0; fi
    - glab mr --repo "$CI_PROJECT_PATH" comment $(echo "$CI_OPEN_MERGE_REQUESTS" | cut -d '!' -f2) --unique=true
      --message "🐋 Branch-based docker image - \`$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-branch\`"
Gryphon answered 10/2, 2023 at 10:16 Comment(2)
I can't seem to find any documentation for glab mr comment, can you help me out? It should be in the documentation repo, right?Tradespeople
Nevermind, it's an alias to note gitlab.com/gitlab-org/cli/-/blob/main/docs/source/mr/note.mdTradespeople
F
0

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.
In all tiers of GitLab, coverage visualization is also available, but it's unclear to me if this displays the overall coverage percentage.

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). However, you will need to supply an API token to the CI job -- you cannot use the builtin job token to add comments.

Far answered 11/5, 2022 at 21:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.