how to dump gitlab ci environment variables to file

Viewed 7652

the question

How to dump all Gitlab CI environment variables (with variables set in the project or group CI/CD settings) to a file, but only them, without environment variables of the host on which gitlab runner is executed?

Background

We are using gitlab CI/CD to deploy our projects to a docker server. Each project contains a docker-compose.yml file which uses various environment variables, eg db passwords. We are using .env file to store this variables, so one can start/restart the containers after deployment from command line, without accessing gitlab.

Our deployments script looks something like this:

deploy: 
  script:
    #...
    - cp docker-compose.development.yml {$DEPLOY_TO_PATH}/docker-compose.yml
    - env > variables.env
    - docker-compose up -d 
    #...

And the docker-compose.yml file looks like this:

version: "3"
services:
  project:
    image: some/image
    env_file:
      - variables.env
    ...

The problem is now the .env file contains both gitlab variables and hosts system environment variables and in the result the PATH variable is overwritten.

I have developed a workaround with grep:

env | grep -Pv "^PATH" > variables.env

It allowed us to keep this working for now, but I think that the problem might hit us again with another variables which would be set to different values inside a container and on the host system.

I know I can list all the variables in docker-compose and similar files, but we already have quite a few of them in a few projects so it is not a solution.

2 Answers

You need to add to script next command

script:
  ...
  # Read certificate stored in $KUBE_CA_PEM variable and save it in a new file
  - echo "$KUBE_CA_PEM" > variables.env
  ...

This might be late, but I did something like this:

  script:
    - env |grep -v "CI"|grep -v "FF"|grep -v "GITLAB"|grep -v "PWD"|grep -v "PATH"|grep -v "HOME"|grep -v "HOST"|grep -v "SH" > application.properties
    - cat application.properties

It's not the best, but it works. The one problem with this is you can have variables with a string containing one of the exclusions, ie. "CI","FF","GITLAB","PWD","PATH","HOME","HOME","SH"

Related