I am trying to build my .net core app using Docker. And I want to override my app version during build. To display it somewhere later in runtime.
I have my Docker file looks like this:
FROM microsoft/dotnet:2.2-sdk AS build-env
WORKDIR /src
RUN apt-get update && \
apt-get install -y libgit2-dev && \
ln -s /usr/lib/x86_64-linux-gnu/libgit2.so /lib/x86_64-linux-gnu/libgit2-15e1193.so
COPY ..
WORKDIR /src/API
RUN dotnet restore
RUN dotnet tool install -g GitVersion.Tool --version=5.0.0-beta1-72
RUN export PATH="$PATH:/root/.dotnet/tools" && \
version="$(dotnet gitversion /output json /showvariable NuGetVersion)" && \
dotnet build --no-restore /property:Version=$version && \
dotnet publish --output "/app" --no-build
FROM microsoft/dotnet:2.2-aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app .
ENTRYPOINT ["dotnet", "API.dll"]
When I tried the same commands like in RUN instructions on my Windows machine - everything is OK. I also tried to run the same commands on WSL (Ubuntu 18) and it's also fine - I have assembly version from my build command. But I can't figure out why it is not working inside Docker.
I've also tried to remove all my GitVersion magic and use just this:
FROM microsoft/dotnet:2.2-sdk AS build-env
WORKDIR /src
RUN apt-get update && \
apt-get install -y libgit2-dev && \
ln -s /usr/lib/x86_64-linux-gnu/libgit2.so /lib/x86_64-linux-gnu/libgit2-15e1193.so
COPY ..
WORKDIR /src/API
RUN dotnet restore
RUN dotnet build --no-restore /property:Version=1.1.1.0-beta1
RUN dotnet publish --output "/app" --no-build
FROM microsoft/dotnet:2.2-aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app .
ENTRYPOINT ["dotnet", "API.dll"]
Anyway, my results are the same.
I use this code to check:
public static void Main(string[] args)
{
Console.WriteLine(typeof(Program).Assembly.GetName());
}
Every time my build is a success but I have version defined in .csproj file without override.
Yes, it should be a trivial task but I also ran into a few issues before I finally could do it. Spent a few hours to solve the issue, hope you can save time. I recommend to read this post it helped.
My final solution was to use a Docker ARG, you must be declare it before the first FROM:
#Declare it at the beginning of the Dockerfile
ARG BUILD_VERSION=1.0.0
...
#Publish your project with "-p:Version=${BUILD_VERSION}" it works also with "dotnet build"
RUN dotnet publish "<xy.csproj>" -c Release -o /app/publish --no-restore -p:Version=${BUILD_VERSION}
One very important thing to Note: is if you are using multistage build (multiple FROM in your file) you have to "re-declare" the ARG in that stage. See a similar question here.
Finally you can call your Docker build with:
docker build --build-arg BUILD_VERSION="1.1.1.1"
In order for GitVersion to work correctly, it needs more information from the git repository which isn't available inside your Docker image.
You could try, inside of your docker image, cloning the single branch from the git repo being built:
git clone -b <branch> <remote-repo>
However, when doing this, DO NOT use the --single-branch
parameter. The command above, in addition to cloning a specific branch from a remote repository, also fetches all the heads of other branches, which GitVersion requires in order for it to perform its version calculations. (See the Agent Setup heading in the TeamCity Setup documentation for GitVersion which explains this).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With