Auto-incrementing build numbers causing random Xcode build failures

I have added that build numbers are auto-incremented using

agvtool next-version -all && xcrun agvtool new-marketing-version \"1.$(agvtool what-version | sed -n 2p |tr -d ' ')\"

I believe this causes random build failures in Xcode where the build fails without proper error messages and I have to retry the build. What is a correct solution for auto-incrementing build numbers?