在gitlab CI中作为Docker容器运行作业-Docker虫洞模式

时间:2019-05-16 22:18:32

标签: docker gitlab gitlab-ci gitlab-ci-runner testcontainers

我正在尝试在Docker容器中运行测试,以下是我的gitlab-ci.yml

image: openjdk:11
include:
  - project: 'xxxxxxx/sdlc'
    file: '/sdlc.yml'

services:
  - docker:dind

gradle test:
  stage: test
  image: docker:latest
  script:
    - ls -la
    - docker run -t --rm
      -v /var/run/docker.sock:/var/run/docker.sock
      -v "$(pwd)":"$(pwd)"
      -w "$(pwd)"
      -u 0:0
      openjdk:11 ls -la

在我的脚本中,我使用“ ls -la”作为第一个命令来列出当前目录中的可用内容,如果下一步将当前目录作为卷安装在容器中,则应列出该目录。相同。但是我什么也没看见。有人可以指出我做错了什么吗?

$ ls -la
total 1752
drwxrwxrwx    9 root     root          4096 May 16 21:59 .
drwxrwxrwx    4 root     root          4096 May 16 21:59 ..
drwxrwxrwx    6 root     root          4096 May 16 21:59 .git
-rw-rw-rw-    1 root     root           182 May 16 21:59 .gitignore
-rw-rw-rw-    1 root     root           787 May 16 21:59 .gitlab-ci.yml
-rw-rw-rw-    1 root     root          4406 May 16 21:59 README.md
-rw-rw-rw-    1 root     root          5351 May 16 21:59 build.gradle
-rw-rw-rw-    1 root     root          9176 May 16 21:59 checkstyle-config.xml
drwxrwxrwx    2 root     root          4096 May 16 21:59 docker
drwxrwxrwx    4 root     root          4096 May 16 21:59 dto
drwxrwxrwx    3 root     root          4096 May 16 21:59 gradle
-rwxrwxrwx    1 root     root          5296 May 16 21:59 gradlew
-rw-rw-rw-    1 root     root          2260 May 16 21:59 gradlew.bat
drwxrwxrwx    2 root     root          4096 May 16 21:59 project
-rw-r--r--    1 root     root       1612517 May 16 21:59 sdlc_enforcer
drwxr-xr-x    3 root     root          4096 May 16 21:59 sdlc_enforcer_dir
-rw-rw-rw-    1 root     root            48 May 16 21:59 settings.gradle
drwxrwxrwx    4 root     root          4096 May 16 21:59 src
-rw-rw-rw-    1 root     root           771 May 16 21:59 whitesource-fs-agent.config
$ docker run -t --rm -v /var/run/docker.sock:/var/run/docker.sock -v "$(pwd)":"$(pwd)" -w "$(pwd)" -u 0:0 openjdk:11 ls -la
Unable to find image 'openjdk:11' locally
11: Pulling from library/openjdk
c5e155d5a1d1: Already exists
221d80d00ae9: Already exists
4250b3117dca: Already exists
3b7ca19181b2: Already exists
1eadaf4c0dff: Already exists
3541530b8726: Pulling fs layer
b5e4c938e30a: Pulling fs layer
a116edcafe05: Pulling fs layer
adf32feffaff: Pulling fs layer
adf32feffaff: Waiting
a116edcafe05: Verifying Checksum
a116edcafe05: Download complete
3541530b8726: Verifying Checksum
3541530b8726: Download complete
b5e4c938e30a: Verifying Checksum
b5e4c938e30a: Download complete
3541530b8726: Pull complete
b5e4c938e30a: Pull complete
a116edcafe05: Pull complete
adf32feffaff: Verifying Checksum
adf32feffaff: Download complete
adf32feffaff: Pull complete
Digest: sha256:768387c0f1e7ec229bc53bd9a8dabb7b81b84d366cb3954cf00ea8400ecadb01
Status: Downloaded newer image for openjdk:11
total 12
drwxr-xr-x. 2 root root 4096 May 16 19:43 .
drwxr-xr-x. 3 root root 4096 May 16 21:59 ..
Job succeeded

参考: https://www.testcontainers.org/supported_docker_environment/continuous_integration/dind_patterns/

1 个答案:

答案 0 :(得分:0)

尝试将Java映像而不是docker:latest用于gradle test作业。然后,Gitlab应该将存储库克隆到Java容器中,并且您会在ls -la中看到它们。

虽然目录可能会有所不同,但是也许您可以更轻松地使它那样工作...

编辑: 关于您的评论:

如果您将openjdk:11指定为图像,则不需要docker run command anymore。阶段配置将是:

include:
  - project: 'sofiinc/devops/sdlc'
    file: '/sdlc.yml'

stages: 
  - test

gradle test:
  stage: test
  image: openjdk:11
  script:
    - pwd
    - ./gradlew clean test

pwd将在openjdk容器中显示工作目录。

编辑::更新了上面的管道配置。