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

mje*_*son 2 gitlab docker 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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

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

Eri*_*kMD 6

两者pwdecho $(pwd)不会出现在输出中,

这是因为容器内实际上没有执行任何命令,因为该映像gittools/gitversion包含以下入口点

ENTRYPOINT ["dotnet", "/app/GitVersion.dll"]
Run Code Online (Sandbox Code Playgroud)

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

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

当未提供命令行参数时会失败,因此会因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
Run Code Online (Sandbox Code Playgroud)

解决方案2

使用手工制作的docker run命令,使用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
Run Code Online (Sandbox Code Playgroud)