GitLab — How to run & test gitlab-ci.yml file locally?

Robin Ding
1 min readAug 10, 2019

GitLab CI is really flexible. We can add stages and jobs easily. But during daily development, sometimes, we want to change/add new jobs or stages, due to the syntax of Gitlab YML file, it always has some sisues. So find a solution about how to test these jobs in gitlab-ci.yml on local, instead of pushing to origin, will dramatically improve our efficiency of updating gitlab-ci.yml file.

Gitlab-Runner is a great tool to achieve this goal !

Install gitlab-runner

brew install gitlab-runner

Change & Commit then Exec gitlab-runner

gitlab-runner will get your local commit from local branch, then run individual job one by one. (It can run only one job at one time, can’t run the whole pipeline)

  • Create a branch for your changes.
  • Change code or gitlab-ci.yml, then commit.
  • Exec gitlab-runner on the job which you want to test
gitlab-runner exec docker <jobname>

There are multiple commands to run job locally, see https://docs.gitlab.com/runner/executors/shell.html

  • Docker
  • Docker Machine
  • Shell
  • Kubernates
  • SSH
  • Parallels
  • VirtualBox

Different commands have its own pros and cons.

But here, I would like to share more about Docker.

Persitent artifacts generated by jobs with Docker Command

Sometime, we want to see the artifacts which are generated by CI jobs, but by default, Docker command won’t persistent the artifacts. Here is the solution (https://docs.gitlab.com/runner/executors/docker.html#the-builds-and-cache-storage)

gitlab-runner exec docker {jobname} --docker-volumes /temp/gitlab-runner/builds:/builds

We can use this way to mount /builds and /cache folder to local host.

--

--