Reuse dll built in a previous stage

I’m asking this for dotnet, but I guess it’s quite similar in any environment.
I have a library I share in other .net projects.

image: mcr.microsoft.com/dotnet/sdk:5.0

stages:
    - build
    - test

build_shared:
    stage: build
    script:
        - "dotnet build Shared/Shared.csproj"

build_api:
    stage: build
    script:
        - "dotnet build WebAPI/WebAPI.csproj"

build_console:
    stage: build
    script:
        - "dotnet build ConsoleApp/ConsoleApp.csproj"

test:
    stage: test
    script: 
    - "dotnet build UnitTests/UnitTests.csproj"
    - "dotnet test UnitTests/UnitTests.csproj"

The yaml above works fine, but the shared library is built in all the stages which doesn’t look optimal.

Shared -> /builds/.../Shared/bin/Debug/net5.0/Shared.dll

I’ve read about artifacts but I fail to find how to set it up to avoid having to rebuild that library.
I can generate the dll artifact in a prebuild stage, and it’s downloaded in the next stages. Then I tried to reuse it in the dotnet build commands with --no-dependencies but I get this error:

CSC : error CS0006: Metadata file '/builds/.../Shared/bin/Debug/net5.0/ref/Shared.dll' could not be found [/builds/.../WebAPI/WebAPI.csproj]

So the dll is generated at the /builds/.../Shared/bin/Debug/net5.0/Shared.dll but later it’s searched at the
/builds/.../Shared/bin/Debug/net5.0/ref/Shared.dll. Can I change the folder of the artifact?

image: mcr.microsoft.com/dotnet/sdk:5.0

stages:
    - prebuild
    - build
    - test

build_shared:
    stage: prebuild
    script:
        - "dotnet build Shared/Shared.csproj"
    artifacts:
      paths:
        - /builds/.../Shared/bin/Debug/net5.0/Shared.dll

build_api:
    stage: build
    script:
        - "dotnet build WebAPI/WebAPI.csproj --no-dependencies"

build_console:
    stage: build
    script:
        - "dotnet build ConsoleApp/ConsoleApp.csproj --no-dependencies"

test:
    stage: test
    script: 
    - "dotnet build UnitTests/UnitTests.csproj --no-dependencies"
    - "dotnet test UnitTests/UnitTests.csproj --no-build"

Just found I had to generate artifacts of the full /builds/.../Shared/bin/Debug/net5.0/ folder, not just the Shared.dll in it for dotnet build --no-dependencies to work.

1 Like