Allow the service container to be started after the Runner completes the git clone of the project repo
The fact that Gitlab services don't have access to the project is very limiting:
Scenario 1 - Keycloak: I am running Keycloak as a Gitlab service. I need to import some Keycloak realm information which is contained in a test-realm.json file in the project. The location of this file is passed to Keycloak in an environment variable. Because the Keycloak service doesn't have access to this file (since git pull hasn't happened yet), this is not possible. I had to resort to configuring a mounted volume in the gitlab-runner config; copying the test-realm.json to that directory on our build machine; and then passing the path to the volume in the Keycloak environment variable. I'll now have to remember to copy that file over every time it changes. Not optimal.
Scenario 2 - PostgresQL I am also running PostgresQL as a Gitlab service. The PostgresQL image allows you to place initialization scripts in the docker-entrypoint-initdb.d directory. So my initialization scripts are in a directory in git. Once again, since the service is started before the project is pulled, my Postgres container doesn't have access to those files.
If there's a workaround for this, I'd appreciate any input.
Example job config to reproduce the issue:
job:
services:
- name: ubuntu
command: [/bin/bash, -c, 'while [[ ! -f /builds/root/test/.gitlab-ci.yml ]]; do date; sleep 1; done; echo "Cloned files are available!"; sleep 60; touch /builds/root/test/service.done']
script:
- while [[ ! -f /builds/root/test/service.done ]]; do date; sleep 1; done; echo "Service done!"; rm /builds/root/test/service.done; sleep 60;
Adding this job to a new/empty project (named test
and in the root
namespace, replace paths accordingly) will result in the "Cloned files are available!" being the only output in the service container log – the git clone happens very fast and we never once enter the while
loop. After committing about 2GB of files into the repository however, the service container log however will contain a lot of date
command outputs, due to the git clone taking much longer.
(The sleep 60
statements at the end are to allow easier access to to the service container logs on the Runner host using docker logs
, as they are currently not easy to access otherwise.)
Reference
The services keyword defines a Docker image that runs during a job linked to the Docker image that the image keyword defines. This allows you to access the service image during build time.