Pass a variable from script back to GitLab (for environment url)

I’m setting up CI with a pipeline with common stages and one of the jobs is used to publish to a review environment. I would need to get a value back from the script to be used to build the environment url.

To make it more clear it’s because I’m using docker, so the job will start a docker container and leave it up (it will then be stopped by a “environment:on_stop” job).
The issue is that because I can have multiple containers running in parallel for review (various branches before merge) I must leave the port mapped by docker dynamic (so using “docker run -P …”) and wanted to use “docker port container-name 9502” to get the mapped port, assign it to a variable and use that variable to build the environment:url .

Any idea on how to do that OR workarounds you can imagine for that kind of needs?

I’m on GitLab CE 9.1.0 and will use a shell runner for this job (the one starting the docker container), while all the other steps are done using a docker runner directly.