I have a .NET 6.0 Web API and want it to know its version. To publish the Docker images I use Github Actions docker/[email protected]
. I tried using MinVer which would exactly meet my needs as it is using the latest git version tag and adds the number of commits since that version as last digit. But as the docker/[email protected]
does not have the full Git environment, Minver can not extract the version number of the Git tags while the image is built. It would be possible to use a CLI tool to extract the version before creating the Docker image:
- name: install minver-cli
run: dotnet tool install --global minver-cli
- name: get version
run: echo "MINVERVERSIONOVERRIDE=$(minver --tag-prefix v --verbosity e)" >> $GITHUB_ENV
- name: echo environementvariable
run: echo ${{ env.MINVERVERSIONOVERRIDE }}
If MINVERVERSIONOVERRIDE
does exist as environment variable at build time, MinVer does not try to extract the version of the git history but just use that version. But it does not look like this environment variable exists in the docker/[email protected]
.
Is there any other way to use version numbers based on the git tags when creating a Docker image?
Your issue is that the Github Actions checkout action uses
1
as the default value for thefetch-depth
argument which means that your workflow has only the latest commit available. If the latest tag is not on this commit, minver will not find it.The solution is to set
fetch-depth
to0
(for fetching all commits in the repo) or any other appropriate value. See here for more details.Edited on Aug 26th 2022 as per the below comments:
First, you write that if
MINVERBUILDMETADATA
is set, MinVer will not try to calculate the version again. This is wrong. It's theMINVERVERSIONOVERRIDE
environment variable which has to be set for this purpose.Said this, this is how you can pass the calculated version value into the container image:
Put this step somewhere above the
docker/build-push-action
action in your workflow yml file:Then pass the version as a build argument to the docker file like this:
Finally use the value in your Dockerfile by adding the following line between the
FROM
and where you build your project:Background:
ARG
sets an environment variable which is available when building the docker image.