Has anyone solved this issue yet? I don't think so.
We use more or less the same deploy scripts to push new versions to different environments.
We haven't found a good way to strip and wipe out developing assets from test environments and test assets from production environments.
Our Java staff ships packages which contain all the test stuff like tests and testing libraries. They say it's too difficult to maintain Maven pom files for different environments; 1MB XML file is difficult to maintain. They also ship the code almost untouched. Exposing private attributes only to have them accessible for unit tests. I smell security issues, but Java developers tend to ignore ex-Java and non-Java developers.
Our Python team is almost good, but that's more or less cheated. Because they run integration tests only. That's why they have a clean production deployment.
In our Node.js team, we have some few minor issues. CSS files aren't minified in development env. DB migrations are done differently depending on NODE_ENV variable; in production, we never want to drop any data. In the production environment, we run (aka our Gitlab CI does) npm install --production while on all other environments we run npm install only.
In some projects, we can't establish a test system either. Mocking an entire B2B platform is impossible, and we've signed a contract not to reverse engineer any of its functionality. In such situations, our scripts differ very much.