From 370276a7b68d9d2c1841368b92dd501dcfa11f6a Mon Sep 17 00:00:00 2001 From: Blake Blackshear Date: Wed, 23 Nov 2022 20:00:45 -0600 Subject: [PATCH] Faster image builds with caching (#4491) * fix makefile variable * add branch for testing * fix arm32 build * use amd64 for web build * install wheels in a separate layer for better parallel builds * try build-push-action * try using gh context * use short sha * cleanup --- .github/workflows/ci.yml | 16 ++++++++++++---- Dockerfile | 23 +++++++++-------------- Makefile | 2 +- docker/install_deps.sh | 3 +-- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7818a52a6..0216b1d59 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,7 +26,15 @@ jobs: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - name: Build image - run: make push - env: - IMAGE_REPO: ghcr.io/${{ github.actor }}/frigate + - name: Create short sha + run: echo "SHORT_SHA=${GITHUB_SHA::7}" >> $GITHUB_ENV + - name: Build and push + uses: docker/build-push-action@v3 + with: + context: . + push: true + platforms: linux/amd64,linux/arm64,linux/arm/v7 + tags: | + ghcr.io/blakeblackshear/frigate:${{ github.ref_name }}-${{ env.SHORT_SHA }} + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/Dockerfile b/Dockerfile index 61d95821f..86cebc1e4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -41,6 +41,7 @@ RUN --mount=type=bind,source=docker/install_s6_overlay.sh,target=/deps/install_s FROM base AS wheels ARG DEBIAN_FRONTEND +ARG TARGETARCH # Use a separate container to build wheels to prevent build dependencies in final image RUN apt-get -qq update \ @@ -106,9 +107,11 @@ ENV PATH="/usr/lib/btbn-ffmpeg/bin:/usr/local/go2rtc/bin:/usr/local/nginx/sbin:$ # Install dependencies RUN --mount=type=bind,source=docker/install_deps.sh,target=/deps/install_deps.sh \ - --mount=type=bind,from=wheels,source=/wheels,target=/deps/wheels \ /deps/install_deps.sh +RUN --mount=type=bind,from=wheels,source=/wheels,target=/deps/wheels \ + pip3 install -U /deps/wheels/*.whl + COPY --from=deps-rootfs / / EXPOSE 5000 @@ -118,8 +121,8 @@ EXPOSE 8555 ENTRYPOINT ["/init"] -# Frigate deps with Node.js and NPM -FROM deps AS deps-node +# Frigate deps with Node.js and NPM for devcontainer +FROM deps AS devcontainer # Install Node 16 RUN apt-get update \ @@ -129,9 +132,6 @@ RUN apt-get update \ && rm -rf /var/lib/apt/lists/* \ && npm install -g npm@9 -# Devcontainer -FROM deps-node AS devcontainer - WORKDIR /workspace/frigate RUN apt-get update \ @@ -145,7 +145,8 @@ CMD ["sleep", "infinity"] # Frigate web build -FROM deps-node AS web-build +# force this to run on amd64 because QEMU is painfully slow +FROM --platform=linux/amd64 node:16 AS web-build WORKDIR /work COPY web/package.json web/package-lock.json ./ @@ -154,19 +155,13 @@ RUN npm install COPY web/ ./ RUN npm run build -# Frigate web dist files -FROM scratch AS web-dist - -COPY --from=web-build /work/dist/ / - # Collect final files in a single layer FROM scratch AS rootfs WORKDIR /opt/frigate/ COPY frigate frigate/ COPY migrations migrations/ -COPY --from=web-dist / web/ - +COPY --from=web-build /work/dist/ web/ # Frigate final container FROM deps diff --git a/Makefile b/Makefile index 269647714..431b82ed0 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ default_target: local COMMIT_HASH := $(shell git log -1 --pretty=format:"%h"|tail -1) VERSION = 0.12.0 -IMAGE_OWNER ?= ghcr.io/blakeblackshear/frigate +IMAGE_REPO ?= ghcr.io/blakeblackshear/frigate CURRENT_UID := $(shell id -u) CURRENT_GID := $(shell id -g) diff --git a/docker/install_deps.sh b/docker/install_deps.sh index ccde3a6ec..4929d2dd2 100755 --- a/docker/install_deps.sh +++ b/docker/install_deps.sh @@ -28,7 +28,6 @@ sed -i -e's/ main/ main contrib non-free/g' /etc/apt/sources.list apt-get -qq update apt-get -qq install --no-install-recommends --no-install-suggests -y \ libedgetpu1-max python3-tflite-runtime python3-pycoral -pip3 install -U /deps/wheels/*.whl # btbn-ffmpeg -> amd64 / arm64 if [[ "${TARGETARCH}" == "amd64" || "${TARGETARCH}" == "arm64" ]]; then @@ -69,7 +68,7 @@ if [[ "${TARGETARCH}" == "arm" ]]; then libgtk-3-dev \ libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \ libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \ - gfortran openexr libatlas-base-dev libssl-devlibtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev \ + gfortran openexr libatlas-base-dev libtbb-dev libdc1394-22-dev libopenexr-dev \ libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev fi