Automaticly persist variables

I want to automaticly update my enviroment variables, then export my environment file and use it in next postman request.

I run request one by one with newman. I group requests in folders, and each folder is my TestScenario. After execution of that test, i log result to my database, and execute next.

What i want to achieve is: Having a jsonfile with url to api, then run special request which will only setVariables in prerequest script, based on this json file. Then i want to export updated env and use in next requests. Unfortunately postman.setEnvironmentVariable and pm.environment.set updated only CurrentValue of variable, so after export value is still the same.

I would like to not do that with some string replace in json file, but do that with postman. How Can i do that?

Collection and environment are local files. They are under git version control. I run collection with newman