Custom env vars in a build

Right now we’re duplicating the following commands in our build scripts to properly tag our docker image

  stage: build
    - '[ "$CI_BUILD_REF_NAME" = "development" ] && export CONTAINER_TAG=latest'
    - '[ "$CI_BUILD_REF_NAME" = "master" ] && export CONTAINER_TAG=stable'
    - '[ -n "$CI_BUILD_TAG" ] && export CONTAINER_TAG="$CI_BUILD_TAG"'

    - docker build -t my.registry/$CONTAINER_NAME:$CONTAINER_TAG --label gitref=$CI_BUILD_REF .

This is so we can tag our docker containers based off the git branches/tags being pushed up. I noticed that the runners have a pre_build_script option in them. Would this allow us to inject env vars into our running builds? I’d ideally just like this to be a standard that all our test runners get these extra env vars in them based on the CI_BUILD_REF_NAME. Can someone suggest how we could do this automatically or if pre_build_script would allow for this? It’s unclear from the docs where that pre_build_script gets run and if it can export env vars into the container.