From 9f1512e7991bdc7feaec051ac51d2612428890ec Mon Sep 17 00:00:00 2001 From: Carson Yang Date: Fri, 10 May 2024 16:28:19 +0800 Subject: [PATCH] Add workflow to build docker image Signed-off-by: Carson Yang --- .github/workflows/docker.yml | 59 ++++++++++++++++++++++++++++++++++++ Dockerfile | 43 ++++++++++++-------------- 2 files changed, 78 insertions(+), 24 deletions(-) create mode 100644 .github/workflows/docker.yml diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..f9facd6 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,59 @@ +name: Build docker images +on: + workflow_dispatch: + push: + paths: + - '**' + branches: + - 'main' + +jobs: + build-docs-images: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Get current date and time + id: datetime + run: echo "datetime=$(date +'%Y%m%d%H%M%S')" >> "$GITHUB_OUTPUT" + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + # list of Docker images to use as base name for tags + images: | + ${{ secrets.DOCKER_USERNAME }}/moneyprinterturbo + ghcr.io/${{ github.repository_owner }}/moneyprinterturbo + tags: | + ${{ steps.datetime.outputs.datetime }} + flavor: latest=false + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to DockerHub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Login to ghcr.io + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GH_PAT }} + + - name: Build and push Docker images to ghcr.io and DockerHub + uses: docker/build-push-action@v5 + with: + context: . + file: ./Dockerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index ebb0063..338b5ed 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,24 @@ # Use an official Python runtime as a parent image -FROM python:3.10-slim-bullseye +FROM python:3.10-slim-bullseye AS base -# Set the working directory in the container +# Set the working directory in the container WORKDIR /MoneyPrinterTurbo -# 设置/MoneyPrinterTurbo目录权限为777 -RUN chmod 777 /MoneyPrinterTurbo +# Copy only the requirements.txt first to leverage Docker cache +COPY requirements.txt . + +# Install Python dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Use a multi-stage build for a smaller final image +FROM python:3.10-slim-bullseye + +WORKDIR /MoneyPrinterTurbo + +COPY --from=base /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages + +# Copy the rest of the codebase +COPY . . ENV PYTHONPATH="/MoneyPrinterTurbo" @@ -19,26 +32,8 @@ RUN apt-get update && apt-get install -y \ # Fix security policy for ImageMagick RUN sed -i '/