@Himanshu_Singh found the solution. The error that stopped the rebuild was:
/var/www/discourse/app/assets/javascripts/discourse/discourse/plugins/guest-gate/discourse/templates/modal.js: Assertion Failed: Named outlets were removed in Ember 4.0. See Ember.js - Deprecations for guidance on alternative APIs for named outlet use cases. (‘discourse/plugins/guest-gate/discourse/templates/modal.hbs’ @ L12:C8)
So the solution was to remove the deprecated Guest Gate plugin.