FROM 475275892927.dkr.ecr.us-east-1.amazonaws.com/python:3.10-aws2-builder as builder
ARG PACKAGE
COPY . /${PACKAGE}
COPY ./Makefile.inc /Makefile.inc
WORKDIR /${PACKAGE}
USER ${ROOT_USER}
RUN chown -R ${APP_USER}:${APP_GROUP} /${PACKAGE}
USER ${APP_USER}
RUN make upload && \
    ./venv/bin/pip3 wheel -r requirements/base.pip --wheel-dir ./dist

FROM 475275892927.dkr.ecr.us-east-1.amazonaws.com/python:3.10-aws2-builder
ARG PACKAGE
ARG APP

ARG REVISION
ARG BUILDTIME
LABEL REVISION=${REVISION}
LABEL BUILDTIME=${BUILDTIME}

COPY --from=builder /${PACKAGE}/pip.conf /app/pip.conf
COPY --from=builder /${PACKAGE}/.pypirc /app/.pypirc
COPY --from=builder /${PACKAGE}/dist/ /app/dist
COPY ./entrypoint.sh /app/entrypoint.sh
USER ${ROOT_USER}
RUN chown -R ${APP_USER}:${APP_GROUP} /app
USER ${APP_USER}
WORKDIR /app

ENV PACKAGE=${PACKAGE}
ENV APP=${APP}

RUN chmod +x /app/entrypoint.sh

RUN python3 -m venv venv && \
    source ./venv/bin/activate && \
        cp pip.conf ./venv/ && \
        pip3 install -U pip && \
        pip3 install ${PACKAGE} --no-index -f /app/dist && \
        rm -rf /app/dist

ENTRYPOINT ["/bin/sh", "/app/entrypoint.sh"]
