fix: Avoid unnecessary re-work in docker build

This commit is contained in:
Robert Prehn 2021-06-03 17:28:09 -05:00
parent 021fc32e6e
commit 26714d6751
2 changed files with 13 additions and 31 deletions

View file

@ -29,28 +29,22 @@ test:
build_image_for_commit:
stage: test
image: "docker:20.10"
only:
- master
cache:
paths:
- _build/
- deps/
services:
- name: docker:20.10-dind
script:
before_script:
- docker login "https://${CI_REGISTRY}" -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD
- docker pull $CI_REGISTRY_IMAGE:latest || true
script:
# This enables fast parallel builds
- export DOCKER_BUILDKIT=1
- mkdir -p _build/prod
- docker build --cache-from $CI_REGISTRY_IMAGE:latest -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
# Push the commit SHA tagged version to registry. We will later choose to tag that as stable
# if everything passes.
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
# Pull out the built _build/prod directory so we can cache it!
- CONTAINER_HANDLE=`docker create $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA`
- docker cp $CONTAINER_HANDLE:/root/app/_build/prod _build
- docker cp $CONTAINER_HANDLE:/root/app/deps deps
# If tests pass, tag the commit and update package versions
deploy_to_tags:

View file

@ -1,10 +1,5 @@
FROM elixir:1.10.4-alpine AS elixir1
RUN apk add make gcc libc-dev
ENV CC=gcc
ENV MIX_HOME=/opt/mix
RUN mix local.hex --force \
&& mix local.rebar --force
@ -21,23 +16,18 @@ WORKDIR /root/app
# We load these things one by one so that we can load the deps first and
# cache those layers, before we do the app build itself
ADD ./config /root/app/config
ADD ./mix.exs /root/app/
ADD ./mix.lock /root/app/
ADD ./apps/admin/mix.exs /root/app/apps/admin/
ADD ./apps/app/mix.exs /root/app/apps/app/
ADD ./apps/content/mix.exs /root/app/apps/content/
ADD ./apps/core/mix.exs /root/app/apps/core/
ADD ./_build/${MIX_ENV}/ /root/app/_build/${MIX_ENV}/
ADD ./deps/ /root/app/deps/
ADD ./script/ /root/app/script/
RUN script/restore-timestamps
ADD ./mix.exs ./mix.lock ./
ADD ./config ./config
ADD ./apps/admin/mix.exs ./apps/admin/
ADD ./apps/app/mix.exs ./apps/app/
ADD ./apps/content/mix.exs ./apps/content/
ADD ./apps/core/mix.exs ./apps/core/
RUN mix deps.get
RUN mix deps.compile
# Leave off here so that we can built assets and compile the elixir app in parallel
FROM node:15.0
FROM node:15.0 AS asset-builder
# Build assets in a node container
ADD ./apps/app/assets/ /root/app/apps/app/assets/
@ -49,17 +39,15 @@ RUN npm run deploy
FROM elixir1
RUN mix deps.compile
ADD ./apps /root/app/apps
# Resume compilation of the elixir app
RUN MAKE=cmake mix compile
# Copy in the built assets & fingerprint them
COPY --from=1 /root/app/apps/app/priv/static/ /root/app/apps/app/priv/static
COPY --from=asset-builder /root/app/apps/app/priv/static/ /root/app/apps/app/priv/static
RUN mix phx.digest
RUN script/restore-timestamps
RUN mkdir -p /root/app/priv/
CMD elixir --name ${NAME:=legendary}@$(hostname -f) -S mix phx.server