# ---------------- Builder Image ----------------------------------------------
FROM teachable/builder-cms:v1.5 AS builder

# Source:
COPY --chown=app:app . /src
WORKDIR /src

# Install Gems:
RUN set -eux \
 && bundle install --frozen --jobs 4 --retry 4 --without development test \
 && rm -rf /usr/local/bundle/cache

# Install node modules and build assets:
ARG NPM_TOKEN
USER app
RUN set -eux \
 && yarn install --frozen-lockfile \
 && NODE_ENV=production npm run build:prod \
 && RAILS_ENV=production AAA_FAKE_MODE=true bundle exec rake assets:precompile \
 && rm -rf node_modules


# ---------------- Final Image ------------------------------------------------
FROM teachable/ruby:v1.5

# Copy gems from builder image:
RUN apk-install postgresql-dev nodejs libxslt-dev libxml2-dev
COPY --from=builder /usr/local/bundle /usr/local/bundle

# Copy source from builder image:
COPY --from=builder --chown=app:app /src /src
USER app
WORKDIR /src

# Tag with version:
ARG APP_TAG
ARG APP_VERSION
LABEL AppName=cms \
      AppTag=$APP_TAG \
      AppVersion=$APP_VERSION
ENV APP_TAG=$APP_TAG \
    APP_VERSION=$APP_VERSION
RUN echo $APP_VERSION > VERSION

# Configure web server:
EXPOSE 8080
ENV PORT=8080 \
    MAX_THREADS=7 \
    WEB_CONCURRENCY=3

# Warm the bootsnap cache:
RUN APP_ENV=production AAA_FAKE_MODE=true ruby config/environment.rb

ENTRYPOINT ["env-vars"]
