0

I would like to have a file in store in a folder in git named .env with the following content:

a=bananas
b=apples

in my pipeline deployment stage, I would like to include this file and export it to the env variables, so that my docker container would receive it and set the proper values into docker-compose.

I tried without much lucky the following:

Deploy Production:

  stage: deploy

  image: my_image
  variables:
    include: /pr/.env
  script:
      - cat $a
      - echo $a
      - cat $b
      - echo $b
      - export .env

it doesn't seem to work, is it possible in gitlab-ci?

korsain
  • 17
  • 1
  • 6
  • 1
    Have you seen in your stage named **deploy** that you create the variable named **include** but you do not use it within the script? Perhaps that variable name is a bit misleading as it may suggest it would be an include directive of some sort while it is just an ordinary variable? And the environment file from the repository, you're not doing anything with it. You need to pass it to docker compose at least so that docker compose is aware where to find that file (if it is not at he default location that gets auto-loaded). Your yaml does not contain anything related to docker compose yet. – hakre Jun 08 '23 at 12:25
  • yes, the intention was to include that file in /pr/.env. Then the next thing i tried was to access it in the shell env with the cat/echo commands. I understand that instead of including the file i created a variable call include (facepalm) – korsain Jun 08 '23 at 13:48

1 Answers1

2

You can directly source your file while exporting variables in your (shell) script of the deploy stage:

Deploy Production:
  stage: deploy
  image: my_image
  script:
      - set -o allexport; source .env; set +o allexport
      - echo $a
      - echo $b

If you really need variables to be interpreted by Gitlab, you can use dynamic variables with dotenv artifacts:

Generate dotenv artifact:
  stage: .pre
  script: 
    - echo "generating variables" ## script is needed but in fact there is nothing to do here
  artifacts:
    reports:
      dotenv: .env

Deploy Production:
  stage: deploy
  image: my_image
  script:
      - echo $a
      - echo $b
hakre
  • 193,403
  • 52
  • 435
  • 836
Greenmaid
  • 111
  • 3