0

我正在尝试在 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/

4

1 回答 1

0

尝试使用 java 图像而不是docker:latest作业gradle test。然后 Gitlab 应该将存储库克隆到 java 容器中,您会看到它们以ls -la.

目录可能会有所不同,但也许你可以更容易地让它工作......

编辑: 关于您的评论:

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

include:
  - project: '****/sdlc'
    file: '/sdlc.yml'

stages: 
  - test

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

pwd将显示openjdk容器内的工作目录。

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

于 2019-05-19T10:31:17.113 回答