CodeWeed
CodeWeed

Reputation: 1071

How do I use gitlab-runner exec docker correctly?

I am trying to run the gitlab pipeline jobs locally in order to test and debug. Here is what I did:

  1. Installed gitlab-runner on my local machine.
  2. sudo gitlab-runner exec docker --docker-privileged --builds-dir /tmp/builds --docker-volumes /home/fox/Work/docker/core-application:/core-application Rspec

This gives:

Runtime platform arch=amd64 os=linux pid=632331 revision=8fa89735 version=13.6.0
Running with gitlab-runner 13.6.0 (8fa89735)
Preparing the "docker" executor
Using Docker executor with image docker:19.03.6 ...
Starting service docker:19.03.6-dind ...
Pulling docker image docker:19.03.6-dind ...
Using docker image sha256:a33335bfe8302f4d8a7688bc1fa539f2aba787ec724119be53adc4681702a3e7 for docker:19.03.6-dind with digest docker@sha256:a4f33d003b7ec9133c2a1ff61f4e80305b329c0fa8b753140b9ab2808f28328c ...
WARNING: Service docker:19.03.6-dind is already created. Ignoring.
Waiting for services to be up and running...

*** WARNING: Service runner--project-0-concurrent-0-aef5122f9d27e6f0-docker-0 probably didn't start properly.

Health check error:
service "runner--project-0-concurrent-0-aef5122f9d27e6f0-docker-0-wait-for-service" timeout

Health check container logs:

....
*********
Pulling docker image docker:19.03.6 ...
Using docker image sha256:6512892b576811235f68a6dcd5fbe10b387ac0ba3709aeaf80cd5cfcecb387c7 for docker:19.03.6 with digest docker@sha256:3eb67443c54436650bd4f1e97ddf9ab1797d75e15d685c791f6c6397edaa6d82 ...
Preparing environment
Running on runner--project-0-concurrent-0 via fox...
Getting source from Git repository
Fetching changes...
Initialized empty Git repository in /tmp/builds/project-0/.git/
Created fresh repository.
fatal: not a git repository: /home/fox/Work/docker/core-application/../.git/modules/core-application
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
ERROR: Failed to cleanup volumes
ERROR: Job failed: exit code 1
FATAL: exit code 1 

           

Then I tried to do it with a gitlab-runner image on the local machine:

docker run --name=runner --privileged -t --rm -v /var/run/docker.sock:/var/run/docker.sock -v /tmp/.gitlab-runner/:/etc/gitlab-runner -v ${PWD}:${PWD} --workdir $PWD gitlab/gitlab-runner exec docker --builds-dir /tmp/builds/ Rspec

I get:

Runtime platform arch=amd64 os=linux pid=7 revision=8fa89735 version=13.6.0
fatal: not a git repository: /home/fox/Work/docker/core-application/../.git/modules/core-application
WARNING: You most probably have uncommitted changes. 
WARNING: These changes will not be tested.         
fatal: not a git repository: /home/fox/Work/docker/core-application/../.git/modules/core-application
FATAL: exit status 128 

Here is what the gitlab documentation says:

If you want to use the docker executor with the exec command, use that in context of docker-machine shell or boot2docker shell. This is required to properly map your local directory to the directory inside the Docker container.

There are no examples. I googled it, but nothing turned out around docker+machine and gitlab-runner.

Can someone tell me how to do it correctly? Any sample?

Thanks.

Upvotes: 2

Views: 8465

Answers (2)

MarkBarry
MarkBarry

Reputation: 341

I also came across a similar issue when using VS Code with remote containers and Docker Desktop (WSL2).

I debugged the problem by adding in a --pre-get-sources-script=ls -l "repo_directory", i.e. in the OP's case:

# docker run --name=runner --privileged -t --rm \
 -v /var/run/docker.sock:/var/run/docker.sock  \
 -v /tmp/.gitlab-runner/:/etc/gitlab-runner \
 -v ${PWD}:${PWD} \
 --workdir $PWD \
 --pre-get-sources-script=ls -l /home/fox/Work/docker/core-application/../.git/modules/core-application \
 gitlab/gitlab-runner exec docker \
  --builds-dir /tmp/builds/ Rspec

In my case, the directory listing (ls -l ...) came up empty! I'm assuming that it mounts the directory as a volume within the container as the same name (/fubar:/fubar), which in my case isn't going to work as it is Docker Desktop with vscode remote containers.

My fix was was to add my named volume as a parameter to a secondary directory, and then setting the --clone-url to point to that directory.

Something like:

# pwd
/workspace/fubar
# gitlab-runner --debug exec docker \
  --docker-volumes vscode-workspace:/workspace2 \
  --clone-url=file:///workspace2/fubar \
  validate;
[output truncated]
...
Starting container 3d49d8e8b977177f34beeae3ae5f09eba288332e109ad79f133e0f160377908b ...  job=1 project=0
Fetching changes...
Initialized empty Git repository in /builds/project-0/.git/
Created fresh repository.
Checking out d1a39a4d as detached HEAD (ref is feature/FU-123)...

Skipping Git submodules setup
Executing build stage                               build_stage=restore_cache job=1 project=0
Skipping stage (nothing to do)                      build_stage=restore_cache job=1 project=0
Executing build stage                               build_stage=download_artifacts job=1 project=0
Skipping stage (nothing to do)                      build_stage=download_artifacts job=1 project=0
Executing build stage                               build_stage=step_script job=1 project=0
Executing "step_script" stage of the job script
...

I had originally tried changing the --pre-get-sources-script to be cp -R /workspace2/fubar /workspace/ but that complained that the target directory was read-only. Using the --clone-url=file:///workspace2/fubar worked, so I've left it that way.

Upvotes: 0

13013SwagR
13013SwagR

Reputation: 611

You have to execute the command from the root folder of your git repository/project:

 $ ls -a
 ./
 ../
 .git/
 .gitignore
 .gitlab-ci.yml
 Makefile

 $ gitlab-runner exec docker <job_name>

You are using docker commands which you shouldn't as gitlab-runner already uses those under the hood. See $ gitlab-runner exec docker --help for more info on the command.

Upvotes: 2

Related Questions