Search code examples
gitlabcontinuous-integrationgitlab-cigitlab-ci.yml

Reconstructing GitLab CI URL path to artifacts


I'm trying to construct a CI variable with an URL that points to the web access to GitLab CI artifacts; in order to use that variable for environments (MR previews). In short, for a project in group topgroup, subgroups gr1/gr2, project name myproject and gitlab pages deployed at domain pages.example.org, I want my variable to contain an URL like:

http://topgroup.pages.example.org/-/gr1/gr2/myproject/-/jobs/123456/artifacts/...[artifact path]...

The problematic part is the one between the two /-/ parts, the rest is either static or can be constructed using CI_JOB_ID and CI_PROJECT_ROOT_NAMESPACE. The middle part doesn't seem to be possible to reconstruct from the currently available CI variables (at least not without taking them to the shell and sedding some parts in and out). The closest I got is:

  • CI_PAGES_URL is ALMOST there, it gives the whole http://topgroup.pages.example.org/gr1/gr2/myproject, unfortunately without the /-/ required for artifacts
  • Constructing: $CI_SERVER_PROTOCOL://$CI_PROJECT_ROOT_NAMESPACE.$CI_PAGES_DOMAIN/-/$CI_PROJECT_PATH/-/jobs/$CI_JOB_ID/artifacts/... is almost there but unfortunately includes the topgroup in the CI_PROJECT_PATH, giving http://topgroup.pages.example.org/-/topgroup/gr1/gr2/myproject/-/jobs/123456/artifacts/...

Is there any way to construct this URL cleanly using simple means, without building it in the shell and re-exporting to gitlab variables?


Solution

  • It can be generated with a mix of predefined variables (but also depends on your GitLab Pages setup):

    If you want the Pages URL of the specific repository you can use the following pattern:

    ${CI_PAGES_URL}/-/jobs/${CI_JOB_ID}/artifacts/my-file.txt
    

    In my case that led to a 404 as the artifact link I extracted from the job pipeline is using the root group pages url. Example which was working for the GitLab setup I was working with:

    https://${CI_PROJECT_ROOT_NAMESPACE}.${CI_PAGES_DOMAIN}/-/child-group/${CI_PROJECT_NAME}/-/jobs/${CI_JOB_ID}/artifacts/<ARTIFACT_PATH_OR_NAME>
    

    Assuming you have the following structure for the repository:
    "Root Group" -> "Child Group" -> "My Repository" and want to upload "my-file.txt"

    The above example results in the following:

    https://root-group.pages.my-gitlab.com/-/child-group/my-repository/-/jobs/00000/artifacts/my-file.txt
    

    For child-group/${CI_PROJECT_NAME} maybe $CI_PROJECT_PATH can be used instead but includes the root group.

    https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
    https://docs.gitlab.com/ee/user/project/pages/getting_started_part_one.html (different structures for pages urls)

    To link to artifacts from your merge requests you can also use expose_as on the artifact itself which shows an "artifacts" section in the merge request.