1

Hello i have the following scenario. I have set up a gitlab runner that with deploy my react application to my ubuntu server using docker. I cannot find a way to get the environmental variables when deploying. The only way it works is if i push .env to the repository ( i dont want to do it ). If i don't push it, when gitlab runner runs docker-compose it throws an error saying that .env is not found. I have also tried creating env variables in gitlab, i can echo them in gitlab-ci.yml, but i don't know how to use them in docker compose later.

image: docker:latest
services:
    - docker:dind

stages:
    - deploy

step-develop:
    stage: deploy
    only:
        - dev
    tags:
        - dev
    script:
        - echo $REACT_APP_BASE_PAGE_URL
        - echo $REACT_APP_CMS_URL
        - sudo docker image prune -f
        - sudo docker-compose -f docker-compose.yml build --no-cache
        - sudo docker-compose -f docker-compose.yml up -d
step-production:
    stage: deploy
    only:
        - prod
    tags:
        - prod
    script:
        - sudo docker image prune -f
        - sudo docker-compose -f docker-compose-prod.yml build --no-cache
        - sudo docker-compose -f docker-compose-prod.yml up -d
#the docker compose file version
version: '3'
# you can run multiple services inside one docker compose file
# define them with their dependencies one after the other
services:
    # service 1 named react-dev
    react-dev:
        # service 1 container name
        container_name: react-dev
        build:
            # the context (working directory) is the current directory
            # change this to the directory containing the dockerfile if in a different place
            context: .
            # the dockerfile to be run
            dockerfile: Dockerfile
        # map the exposed port from the underlying service to a port exposed to the outside
        # in this case  map port 3000 exposed by create react app to also 3000
        # to be used to access the container from the outside
        ports:
            - '3000:80'
        # the mounted volumes (folders which are outside docker but being used by docker)
        # volumes:
        #     - '.:/gt-strapi-react'
        #     - '/gt-strapi-react/node_modules'
        # set the environment to development
        env_file:
            - .env
        environment:
            - NODE_ENV=development
            - REACT_APP_BASE_PAGE_URL=${REACT_APP_BASE_PAGE_URL}
            - REACT_APP_CMS_URL=${REACT_APP_CMS_URL}
Ixam Deirf
  • 425
  • 2
  • 6
  • 14

1 Answers1

0

You should remove the env_file section from your Docker Compose configuration. Any files named .env in the root folder are read by Docker Compose automatically, and the env_file section is for tuning this.

You should also not run docker-compose as sudo.

Finally, you want to be super explicit about environment variables passed to docker-compose, you can execute it like this from the GitLab CI config:

step-production:
    script:
        - env 
        - REACT_APP_CMS_URL=${REACT_APP_CMS_URL} 
        - docker-compose 
        - -f 
        - docker-compose-prod.yml 
        - up 
        - -d
torkel
  • 2,096
  • 3
  • 10
  • 20
  • but if gitlab runner is using the repository, and inside the repository there is no .env file, but it is on my server folder, how is possible for gitlab runner to recognise the file? Im getting : The REACT_APP_CMS_URL variable is not set. Defaulting to a blank string. – Ixam Deirf Dec 09 '20 at 11:53
  • You mentioned that you got environment variables in GitLab working, so you should pass them to the `docker-compose` commands. Does the solution I posted work? If you are able to `echo` them, you could potentially also remove the `env REACT_APP_CMS_URL=...` part as well. – torkel Dec 09 '20 at 12:01
  • yes i can echo the env variables from gitlab , but i dont know how to pass them to the docker-compose commands. the solution you posted doesnt work, if i put everything like you said in separate lines i get an error like the commands are wrong: $ REACT_APP_CMS_URL=${REACT_APP_CMS_URL} $ docker-compose Define and run multi-container applications with Docker. Usage: docker-compose [-f ...] [options] [COMMAND] [ARGS...] docker-compose -h|--help Options: -f, --file FILE Specify an alternate compose file (default: docker-compose.yml) – Ixam Deirf Dec 09 '20 at 12:09
  • Can you pass the exact docker-compose command you are invoking? It looks like you have a syntax error. – torkel Dec 09 '20 at 12:12
  • what do you mean? i just copied what you posted: – Ixam Deirf Dec 09 '20 at 12:21
  • It looks like `REACT_APP_CMS_URL` is empty. I'm not sure how that's get in GitLab, but you need to make sure that has a value when running on CI. – torkel Dec 09 '20 at 12:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225707/discussion-between-ixam-deirf-and-torkel). – Ixam Deirf Dec 09 '20 at 12:32