1

I have below docker-compose.yml

version: "2"
services:
    api:
        build:
            context: .
            dockerfile: ./build/dev/Dockerfile
        container_name: "project-api"
        volumes:
          # 1. mount your workdir path
          - .:/app
        depends_on:
          - mongodb
        links:
          - mongodb
          - mysql

    nginx:
        image: nginx:1.10.3
        container_name: "project-nginx"
        ports:
            - 80:80
        restart: always
        volumes:
            - ./build/dev/nginx.conf:/etc/nginx/conf.d/default.conf
            - .:/app
        links:
            - api
        depends_on:
            - api
    mongodb:
        container_name: "project-mongodb"
        image: mongo:latest
        environment:
          - MONGO_DATA_DIR=/data/db
          - MONGO_LOG_DIR=/dev/null
        ports:
            - "27018:27017"
        command: mongod --smallfiles --logpath=/dev/null # --quiet
    mysql:
      container_name: "gamestore-mysql"
      image: mysql:5.7.23
      ports:
        - "3306:3306"
      environment:
        MYSQL_DATABASE: project_test
        MYSQL_USER: user
        MYSQL_PASSWORD: user
        MYSQL_ROOT_PASSWORD: root

And below .gitlab-ci.yml

test:
  stage: test
  image: docker:latest
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay2
  before_script:
    - apk add --no-cache py-pip
    - pip install docker-compose
  script:
    - docker-compose up -d
    - docker-compose exec -T api ls -la
    - docker-compose exec -T api composer install
    - docker-compose exec -T api php core/init --env=Development --overwrite=y
    - docker-compose exec -T api vendor/bin/codecept -c core/common run
    - docker-compose exec -T api vendor/bin/codecept -c core/rest run

When i running my gitlab pipeline it's become field because i think gitlab can't work with services runned by docker-compose.

The error says that mysql refuse the connection.

I need this connection because my test written by codeception will test my models and api actions.

I want test my branches every time any one push in them and if pass just in develop deploy into test server and in master deploy on production server.

What is best way to run my test in gitlab ci/cd and then deploy them in my server?

1 Answer 1

1

You should use GitLab CI services instead of docker-compose.

You have to pick one image as your main, in which your commands will be run, and other containers just as services.

Sadly CI services cannot have mounted files in gitlab, you have to be able to configure them with env variables, or you need to create you own image with files in it (you can do that CI stage)

I would suggest you to don't use nginx, and use built-in php server for tests. It that's not possible (you have spicifix nginx config), you will need to build yourself nginx image with copied files inside it.

Also for PHP (the api service in docker-compose.yaml i assume), you need to either build the image ahed or copy command from your dockerfile to script.

So the result should be something like:

test:
  stage: test
  image: custom-php-image #build from ./build/dev/Dockerfile
  services:
    - name: mysql:5.7.23
      alias: gamestore-mysql
    - name: mongo:latest
      alias: project-mongodb
      command: mongod --smallfiles --logpath=/dev/null
  variables:
    MYSQL_DATABASE: project_test
    MYSQL_USER: user
    MYSQL_PASSWORD: user
    MYSQL_ROOT_PASSWORD: root

    MONGO_DATA_DIR: /data/db
    MONGO_LOG_DIR: /dev/null
  script:
    - api ls -la
    - composer install
    - php core/init --env=Development --overwrite=y

    - php -S localhost:8000 # You need to configure your built-in php server probably here 

    - vendor/bin/codecept -c core/common run
    - vendor/bin/codecept -c core/rest run

I don't know your app, so you will probably have to made some tweaks.

More on that:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.