3

I have this in my 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 

When this runs on my runner, I get this:

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 

How can I get Gitversion to determine the version for my build? I use it for all code that's not built using Docker and I could use that, but I feel it should work in a container as well.

Also, both pwd and echo $(pwd) do not show up in the output, how can I get something in the output?

2 Answers 2

7

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

This comes from the fact no command is actually executed inside the container, because this image gittools/gitversion contains the following entrypoint:

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

which is compatible with the one-liner suggested in https://hub.docker.com/r/gittools/gitversion:

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

which fails when no command-line argument is provided, and thereby fails with the docker exec-implied use of GitLab CI.

Moreover, it seems the image doesn't support the gitversion command but rather dotnet /app/GitVersion.dll.

Solution 1

As a workaround, you may want to override the entrypoint and try:

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 

Solution 2

Use a handcrafted docker run command, 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 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I'll try that when I get in the office on Monday. In the mean time, is this actually the preferred way of using a tool like Gitversion, or would it make more sense to do an 'apt install gitversion' on the host container?
I guess if you install the gitversion package on the host server that runs the Docker executor, it won't be available from the CI job script (due to Docker's isolation). However instead of applying the "Solution 1" as is, I think you could just as well build and use another image in place of of gittools/gitversion, containing gitversion installed with apt
The second option actually seems the proper way to me (as it also uses the command as proposed by gitversion's site). But ... it complains it cannot find any branches on the repo, It sees the repo, but cannot see any commits related to the branches. It works on my machine, so it seems that gitlab-ci does not do a proper 'fetch' before beginning. Any idea how to solve this?
The way the repo is cloned before running the CI script is governed by several vars, e.g. GIT_STRATEGY (none skipping the clone) & GIT_DEPTH. And the default value of GIT_DEPTH recently changed to 50 so GitLab CI makes shallow clones, without all branches. To avoid this you can just add variables:\n GIT_DEPTH: 0 in .gitlab-ci.yml, or put 0 as default value in https://gitlab.com/username/reponame/-/settings/ci_cd
1

At least as of GitLab version 13.10 and GitVersion 5.6.11, the following can be used to add the version to a variable. In this example I use the "SemVer" output, but this can easily be changed.

Determine version: stage: build image: name: gittools/gitversion:5.6.11-debian.10-x64 entrypoint: [''] script: | echo "VERSION=$(/tools/dotnet-gitversion | sed --silent --regexp-extended 's/^\s*"SemVer": "([^"]+)".*$/\1/p')" >> build.env cat build.env artifacts: reports: dotenv: build.env Publish package: stage: deploy script: | echo "We have a ${VERSION} we can use!" 

I adapted the above from the GitLab documentation.

3 Comments

You may want to add some link to the official documentation, e.g. this one?
BTW, a dependencies: or needs: keyword seems necessary, according to the doc?
Nope, dependencies: is not needed. By default artifacts are applied to all downstream jobs unless explicitly specified via dependencies:. I don't need the needs because they're in separate stages (build and deploy). The documentation is a bit confusing because they name their jobs the same name as their stages. XD

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.