如何正确使用gitlab-runner exec docker?

6g8kf2rb  于 2023-03-22  发布在  Docker
关注(0)|答案(2)|浏览(151)

我尝试在本地运行gitlab pipeline作业,以便进行测试和调试。下面是我所做的:
1.在本地机器上安装了gitlab-runner。

  1. sudo gitlab-runner exec docker --docker-privileged --builds-dir /tmp/builds --docker-volumes /home/fox/Work/docker/core-application:/core-application Rspec
    这给出:
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

然后我尝试在本地机器上使用gitlab-runner图像:
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
我得到:

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

下面是gitlab文档的内容:
如果你想在exec命令中使用docker executor,请在docker-machine shell或boot 2docker shell的上下文中使用。这是正确Map本地目录到Docker容器内目录所必需的。
我在谷歌上搜索了一下,但是没有关于docker+machine和gitlab-runner的结果。
有人能告诉我怎么做才正确吗?有没有样品?
谢谢。

dddzy1tm

dddzy1tm1#

你必须从你的git repository/project的根文件夹执行命令:

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

 $ gitlab-runner exec docker <job_name>

你正在使用docker命令,这是不应该的,因为gitlab-runner已经在后台使用了这些命令。有关该命令的更多信息,请参阅$ gitlab-runner exec docker --help

2izufjch

2izufjch2#

我在远程容器和Docker Desktop(WSL 2)中使用VS Code时也遇到了类似的问题。
我通过添加一个--pre-get-sources-script=ls -l "repo_directory"来调试这个问题,即在OP的情况下:

# 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

在我的例子中,目录列表(ls -l ...)显示为空!我假设它将目录作为卷挂载在容器中,并使用相同的名称(/fubar:/fubar),在我的例子中,这不会起作用,因为它是带有vscode远程容器的Docker Desktop。
我的解决方法是将我的命名卷作为参数添加到辅助目录,然后设置--clone-url指向该目录。
比如:

# 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
...

我最初尝试将--pre-get-sources-script更改为cp -R /workspace2/fubar /workspace/,但抱怨目标目录是只读的。使用--clone-url=file:///workspace2/fubar工作,所以我保持这种方式。

相关问题