docker - 如何使用 docker 在 Gitlab CI 中执行 gitversion?

标签 docker gitlab gitlab-ci

我的 gitlab-ci.yml 中有这个:

variables:
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: "/certs"
  DOCKER_HOST: "tcp://docker:2375"

determine version:
  stage: preparation
  tags: 
    - docker
  image: gittools/gitversion
  script:
    - pwd
    - echo $(pwd)
    - gitversion /output json /showvariable FullSemVer > version.txt
    - cat version.txt
  artifacts:
    paths:
      - version.txt
    expire_in: 1 hr

当它在我的运行者身上运行时,我得到了这个:

1 Running with gitlab-runner 12.6.0 (ac8e767a)
2   on gitlab-runner-01 efEDOrEf
3
Using Docker executor with image gittools/gitversion ...
4 Pulling docker image gittools/gitversion ...
5 Using docker image sha256:2d1d36c0807eaeedc8d4a81b72ae3ee16f7c6a1d25bdce22c8e1983ac6c98dcb for gittools/gitversion ...
7
Running on runner-efEDOrEf-project-475-concurrent-0 via gitlab-runner-01...
9
Fetching changes...
10 Reinitialized existing Git repository in /builds/applications/myapplicationname/.git/
11 From https://path/to/my/git
12  * [new ref]         refs/pipelines/10548 -> refs/pipelines/10548
13    638ed79..9c57f34  feature/3442   -> origin/feature/3442
14 Checking out 9c57f347 as feature/3442...
15 Skipping Git submodules setup
19
INFO [01/10/20 16:05:44:94] Applicable build agent found: 'GitLabCi'.WARN [01/10/20 16:05:44:95] The working directory 'sh' does not exist.INFO [01/10/20 16:05:44:95] IsDynamicGitRepository: FalseERROR [01/10/20 16:05:44:97] An unexpected error occurred:
20 System.IO.DirectoryNotFoundException: Can't find the .git directory in 
21    at GitVersion.GitPreparer.GetProjectRootDirectoryInternal() in D:\a\1\s\src\GitVersionCore\GitPreparer.cs:line 92
22    at GitVersion.GitPreparer.GetProjectRootDirectory() in D:\a\1\s\src\GitVersionCore\GitPreparer.cs:line 27
23    at GitVersion.Configuration.ConfigFileLocator.Verify(IGitPreparer gitPreparer) in D:\a\1\s\src\GitVersionCore\Configuration\ConfigFileLocator.cs:line 61
24    at GitVersion.GitVersionExecutor.VerifyArgumentsAndRun(Arguments arguments) in D:\a\1\s\src\GitVersionExe\GitVersionExecutor.cs:line 105INFO [01/10/20 16:05:44:97] INFO [01/10/20 16:05:44:97] Attempting to show the current git graph (please include in issue): INFO [01/10/20 16:05:44:97] Showing max of 100 commits
26
INFO [01/10/20 16:05:46:26] Applicable build agent found: 'GitLabCi'.WARN [01/10/20 16:05:46:26] The working directory 'sh' does not exist.INFO [01/10/20 16:05:46:26] IsDynamicGitRepository: FalseERROR [01/10/20 16:05:46:28] An unexpected error occurred:
27 System.IO.DirectoryNotFoundException: Can't find the .git directory in 
28    at GitVersion.GitPreparer.GetProjectRootDirectoryInternal() in D:\a\1\s\src\GitVersionCore\GitPreparer.cs:line 92
29    at GitVersion.GitPreparer.GetProjectRootDirectory() in D:\a\1\s\src\GitVersionCore\GitPreparer.cs:line 27
30    at GitVersion.Configuration.ConfigFileLocator.Verify(IGitPreparer gitPreparer) in D:\a\1\s\src\GitVersionCore\Configuration\ConfigFileLocator.cs:line 61
31    at GitVersion.GitVersionExecutor.VerifyArgumentsAndRun(Arguments arguments) in D:\a\1\s\src\GitVersionExe\GitVersionExecutor.cs:line 105INFO [01/10/20 16:05:46:28] INFO [01/10/20 16:05:46:28] Attempting to show the current git graph (please include in issue): INFO [01/10/20 16:05:46:28] Showing max of 100 commits
34
Uploading artifacts...
35 WARNING: version.txt: no matching files            
36 ERROR: No files to upload                          
38 Job succeeded

如何让 Gitversion 确定我的构建版本? 我将它用于所有不是使用 Docker 构建的代码,我可以使用它,但我觉得它也应该在容器中工作。

另外,pwdecho $(pwd) 都没有出现在输出中,我怎样才能在输出中得到一些东西?

最佳答案

both pwd and echo $(pwd) do not show up in the output,

这是因为在容器内实际上没有执行任何命令,因为这张图片 gittools/gitversion包含以下入口点:

ENTRYPOINT ["dotnet", "/app/GitVersion.dll"]

https://hub.docker.com/r/gittools/gitversion 中建议的单行兼容:

docker run --rm -v "$(pwd):/repo" gittools/gitversion:latest-linux-netcoreapp2.2 /repo

当没有提供命令行参数时会失败,因此会因 docker exec - 暗示使用 GitLab CI 而失败。

此外,该图像似乎不支持 gitversion 命令,而是支持 dotnet/app/GitVersion.dll

解决方案 1

作为一种解决方法,您可能想要覆盖入口点并尝试:

variables:
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: "/certs"
  DOCKER_HOST: "tcp://docker:2375"

determine version:
  stage: preparation
  tags: 
    - docker
  image:
    name: gittools/gitversion
    entrypoint: /bin/bash

  script:
    - pwd
    - dotnet /app/GitVersion.dll /output json /showvariable FullSemVer > version.txt
    - cat version.txt
  artifacts:
    paths:
      - version.txt
    expire_in: 1 hr

解决方案 2

使用手工制作的 docker run 命令,using Docker-in-Docker (dind):

variables:
  DOCKER_DRIVER: overlay2
  DOCKER_TLS_CERTDIR: "/certs"
  DOCKER_HOST: "tcp://docker:2375"

determine-version:
  stage: preparation
  tags: 
    - docker
  image: docker:latest
  services:
    - docker:dind

  script:
    - pwd
    - docker run --rm -v "$(pwd):/repo" gittools/gitversion:latest-linux-netcoreapp2.2 /repo /output json /showvariable FullSemVer > version.txt
    - cat version.txt
  artifacts:
    paths:
      - version.txt
    expire_in: 1 hr

关于docker - 如何使用 docker 在 Gitlab CI 中执行 gitversion?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59685314/

相关文章:

kubernetes - 如果发布了较大的json,则用于自动审查应用程序(gitlab-managed-apps)的NGINX入口会非常慢

Gitlab:如何将原始日志收集到工件中?

git - android studio checkout Gitlab 私有(private)项目时认证失败

asp.net - 亚特实验室 CI : how to load vars from JSON file

docker - 如何通过另一个容器的172.17.42.1:53:53/udp(docker0)通过暴露的端口访问容器(SkyDns)? Docker 1.7.1,Centos 7

java - 如何使用谷歌云数据存储模拟器和java实现docker镜像

docker - LINKERD:无法从linkerd构建docker镜像

linux - CI Gitlab 因 docker 守护进程系统故障而失败

terraform - 如何从命令行获取 Gitlab 运行者注册 token ?

postgresql - Docker Compose PostgreSQL 复制功能正常但在数据库中找不到关系