After a lot of time and research trying to fix this problem I found a solution that worked for me.
I am using msbuild with /m and /p:BuildInParallel=true and the build was failing in the CI server. It was always failing in the second componen with the error:
error MSB4166: Child node “3” exited prematurely. Shutting down.
Adding /nodeReuse:false fixed the problem.
This is a good reference:
https://blogs.msdn.microsoft.com/msbuild/2007/04/16/node-reuse-in-multiproc-msbuild/