Your plan looks right. Discourse can match Google logins to existing accounts through secondary emails, so users can keep their personal emails for notifications.
For the domain restriction, use google oauth2 hd rather than allowed email domains. Also, leave auth overrides email disabled to keep their primary email addresses unchanged.
For the script, UserEmail doesn’t have a confirmed_at column, so remove that. The Attach the domain email... line also needs a # in front of it.
The create step can be:
user.user_emails.create!(email: domain_email, primary: false)
I’d take a backup and test with a couple of users first, especially since the script skips email verification. Check that Google login takes them to their existing accounts and that personal Google accounts are blocked. Once that’s working, you can disable local logins.