I feel Dario's pain with asking this question:
how to set ASPNETCORE_ENVIRONMENT to be considered for publishing an asp.net core application?
Loads of cool and interesting ideas, but none of them work unless you do the following :-(
MS documents are excellent for walking through the deployment process but what they forget to mention is on setting up your target website on IIS you need to fiddle with the Application Pool settings a bit more than explicitly stated.
If you scroll down the documents https://learn.microsoft.com/en-us/aspnet/core/publishing/iis to the "Deploy the application" section, follow through the setup instructions, but when you reach the ApplicationPool configuration Set the "No Managed Code" option. Then right-click on the newly created pool for Advanced Settings; in the Process Model section find Load User Profile and set this to true.
This should allow you .net core app to access the Environment Variables that you need...