Pass Gitlab Secrets to docker build in CI

I’m currently looking for a way to define ENV variables in CI with Gitlab Secrets so I can override defaults values at build time when I need to deploy my containers.

From what I could find, there is no such way if I don’t specify the -e in the build command. However, this is not very practical for a certain number of arguments.

My current commands are currently looking like this :

# In .gitlab-ci
docker build -t $CI_REGISTRY/$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME/production:${CI_COMMIT_REF_SLUG} -f .docker/production/Dockerfile .

# In Dockerfile
ENV MAILER_URL=${MAILER_URL:-"null://localhost"}

I know I could just use build args, but I have a good number of settings and I would like to avoid putting them in CI

Source: StackOverflow