Annotations

Hello

Problem to solve

I’d like to add some links to the merge request ui and it looks like GitLab CI/CD artifacts reports types | GitLab Docs can help doing this but unfortunately, that does not seem to work. The upload of the annotation json file fails. Thanks for your help.

Steps to reproduce

Here’s what the job looks like:

buildStaging:
  stage: test
  environment: staging
  script:
    - yarn build
    - yarn analyze --html bundle-analysis/index.html
  artifacts:
    paths:
      - build/
      - bundle-analysis/index.html
      - annotation.json
    reports:
      annotations: annotation.json
  pages:
    publish: bundle-analysis
    path_prefix: "$CI_MERGE_REQUEST_IID-bundle-analysis/"
    expire_in: 2 weeks

(To easily test, annotation.json is for now in the repository, once this works I’ll generate it)

Here is the content of annotation.json:

{
  "bundle_analysis": [
    { "external_link": "https://example.com", "label": "Bundle analysis" }
  ]
}

I also tried with something like (suggested by Mistral AI)

[
  {
    "description": "Message affiché dans la MR",
    "external_link": "https://lien-externe.com"
  }
]

in both case, the job ends with something like:

Uploading artifacts as "archive" to coordinator... 201 Created  id=13203047111 responseStatus=201 Created token=glcbt-6c
Uploading artifacts...
annotation.json: found 1 matching artifact files and directories 
WARNING: Uploading artifacts as "annotations" to coordinator... POST https://gitlab.com/api/v4/jobs/13203047111/artifacts: 400 Bad Request (Annotations files must be a JSON object)  id=13203047111 responseStatus=400 Bad Request status=400 token=glcbt-6c
WARNING: Retrying...                                context=artifacts-uploader error=invalid argument
WARNING: Uploading artifacts as "annotations" to coordinator... POST https://gitlab.com/api/v4/jobs/13203047111/artifacts: 400 Bad Request (Annotations files must be a JSON object)  id=13203047111 responseStatus=400 Bad Request status=400 token=glcbt-6c
WARNING: Retrying...                                context=artifacts-uploader error=invalid argument
WARNING: Uploading artifacts as "annotations" to coordinator... POST https://gitlab.com/api/v4/jobs/13203047111/artifacts: 400 Bad Request (Annotations files must be a JSON object)  id=13203047111 responseStatus=400 Bad Request status=400 token=glcbt-6c
FATAL: invalid argument                            
Cleaning up project directory and file based variables 00:01
ERROR: Job failed: exit code 1

Versions

  • GitLab.com SaaS

Gitlab runner: Running with gitlab-runner 17.1.0 (fe451d5a)

What might happen is that the passed content from Git checkout is an empty artifact leading to the upload error. Or the encoding is wrong, or otherwise broken characters (crlf on Linux/Windows are different for example)

Try to generate the json blob inside the CI/CD job, like

test-annotation:
 stage: test
 script:
   - |
     cat > annotation.json <<'EOF'
     {
       "bundle_analysis": [
         { "external_link": "https://example.com", "label": "Bundle analysis" }
       ]
     }
     EOF
   - cat annotation.json  # verify it looks right
 artifacts:
   reports:
     annotations: annotation.json

Or shorter,


script:
  - echo '{"bundle_analysis":[{"external_link":"https://example.com","label":"Bundle analysis"}]}' > annotation.json

If that json still throws an error, the problem might be on the server or runner side (too old, feature flag, beta/experimental settings).

Hi @dnsmichi thanks for your reply.

For the record, after looking at my issue with fresh eyes, it happens the issue was that I misunderstood the documentation and my JSON was just wrong :grimacing: It should be something like:


{
  "bundle_analysis": [
    {
      "external_link": {
        "label": "Bundle analysis",
        "url": "https://example.com"
      }
    }
  ]
}

and now with that, it’s working fine. (even if I’m a bit disappointed by the fact that the link appears in the job view and not in the MR UI)

1 Like