Generating CI_PROJECT_PATH_SLUG outside GitLab CI (aka how do I slugify the CI_PROJECT_PATH with only Git + regexp)

I want to reproduce the generation of CI_PROJECT_PATH_SLUG variable on GitLab CI locally through getting the remote URL via git remote get-url origin, slugify the URL (while removing http(s)://instance.hostname.tld with sed. What’s the regular expression on slugifying the repository URL while omitting the URL protocol and hostname?

Hi,

I’d suggest peeking into the source code, and search for the variable name: CI_PROJECT_PATH_SLUG · Search · GitLab

From there, navigate into the scope where it is being set, and follow the internal variable. app/models/project.rb · master · GitLab.org / GitLab · GitLab

This leads to full_path_slug calling a util function in app/models/project.rb · master · GitLab.org / GitLab · GitLab

Let’s look where slugify is defined: slugify · Search · GitLab

Leading to its definition where the string operations take place. lib/gitlab/utils.rb · master · GitLab.org / GitLab · GitLab

Note that the implementation of slugify could change, or the variable being deprecated in the future. I’d suggest documenting the regex with an URL to the source code, so you know in the future, if for some reason your own slugify fails.

I’m using the above search strategy with all OSS projects, even if I don’t know the language. Algorithms and data structures are very similar :slight_smile: Works very well on my iPad too :wave:

Cheers,
Michael

2 Likes