FROM 475275892927.dkr.ecr.us-east-1.amazonaws.com/python:3.10-aws2-builder as builder
ARG PACKAGE
COPY . /${PACKAGE}
WORKDIR /${PACKAGE}
USER ${ROOT_USER}
RUN chown -R ${APP_USER}:${APP_GROUP} /${PACKAGE}
USER ${APP_USER}
RUN python3 -m venv venv \
    && source ./venv/bin/activate \
    && cp pip.conf ./venv/ \
    && pip3 install -U pip setuptools wheel twine \
    && python3 setup.py bdist_wheel \
    && pip3 wheel -r requirements/base.pip --wheel-dir ./dist \
    && twine upload -r pypi --config-file .pypirc ./dist/* \
    && find ./dist -type f -not -name "${PACKAGE}*.whl" -delete

FROM 475275892927.dkr.ecr.us-east-1.amazonaws.com/python:3.10-aws2
ARG PACKAGE
ARG APP
ENV PACKAGE=${PACKAGE}
COPY --from=builder /${PACKAGE}/pip.conf /app/pip.conf
COPY --from=builder /${PACKAGE}/.pypirc /app/.pypirc
COPY --from=builder /${PACKAGE}/dist/ /app/dist
USER ${ROOT_USER}
RUN chown -R ${APP_USER}:${APP_GROUP} /app
USER ${APP_USER}
WORKDIR /app
RUN echo $' \
        #!/bin/sh \
        set -euxo pipefail \n \
        exec ./venv/bin/${PACKAGE} $@ \n' >> /app/entrypoint.sh
RUN python3 -m venv venv \
    && source ./venv/bin/activate \
    && cp pip.conf ./venv/ \
    && pip3 install -U pip \
    && pip3 install ${PACKAGE} -f /app/dist \
    && rm -rf /app/dist
ENTRYPOINT ["/bin/sh", "/app/entrypoint.sh"]
