0

I don't know how to do cache dependency in gitlab-ci -> docker. My project has 82 dependencies and they get very slow.. (vendor is in gitignore)

Full process:

change local file -> comit and push to repo remote -> run gitlab-ci -> build docker image -> push image to other server -> publish image

My example project:

  • app -> my files (html, img, php, css, anything)

  • gitlab-ci.yml

  • composer.json
  • composer.lock
  • Makefile
  • Dockerfile

Dockerfile:

FROM hub.myserver.test/image:latest
ADD . /var/www
CMD cd /var/www
RUN composer install --no-interaction
RUN echo "#done" >> /etc/sysctl.conf

gitlab-ci:

build:
    script: 
        - make build
    only:
        - master

Makefile:

all: build

build:
    docker build hub.myserver.test/new_image .

How I can caching dependencies (composer.json)? I do not want to download libraries from scratch.

Arkadiusz G.
  • 1,024
  • 10
  • 24
  • This is for Travis but should be useful for your case. https://blog.wyrihaximus.net/2015/07/composer-cache-on-travis/ Remember that you can only cache items (files/directory) that are inside the build folder in gitlab-ci – gcharbon Nov 05 '18 at 20:38
  • I didn't notice that it is more complicated to use cache with docker. But this [stack overflow answer](https://stackoverflow.com/a/39772423/7786148) might help you ? – gcharbon Nov 05 '18 at 20:49

1 Answers1

1

Usually it's not a good idea to run composer install inside your image. I assume you need eventually to run your php app not composer itself, so you can avoid having it in production.

One possible solution is to split app image creation into 2 steps:

  1. Install everything outside image
  2. Copy ready-made files to image

.gillab-ci.yml

stages:
  - compose
  - build

compose:
  stage: compose
  image: composer       # or you can use your hub.myserver.test/image:latest
  script:
    - composer install  # install packages
  artifacts:
    paths:
      - vendor/         # save them for next job

build:
  stage: build
  script:
    - docker build -t hub.myserver.test/new_image .
    - docker push hub.myserver.test/new_image

So in Dockerfile you just copy files from artifacts dir from first stage to image workdir:

# you can build from your own image
FROM php

COPY . /var/www

WORKDIR /var/www
# optional, if you want to replace CMD of base image
CMD [ "php", "./index.php" ]

Another good consideration is that you can test your code before building image with it. Just add test job between compose and build.

Live example @ gitlab.com

vladkras
  • 16,483
  • 4
  • 45
  • 55