gitlabgitlab-ci

How to iterate over variables list in Gitlab pipeline?


I'm trying to deploy my Docker image and I would like to create .env file with all environment variables. Is there some way to iterate over all pipeline variables?

.gitlab-ci.yaml:

variables:
  DOCKER_DRIVER: overlay2
  SECURE_FILES_DOWNLOAD_PATH: './secure_files'
  PROJECT_NAME: magicfeedback/django
  ...
build:
  stage: build
  script:
    for variable in ${variables}
    do
      echo "$variable" >> .env
      echo "export $variable" >> .env_export
    done

Solution

  • You can put all your variables using env command

    env > .env
    env | sed  's/\(.*\)/export \1/' > .env
    

    You would have to much variables though. So if you want to filter it a bit, you can make your variable start with MYCI

    env | grep MYCI > .env
    env | grep MYCI | sed  's/\(.*\)/export \1/' > .env