From 692b5e6cd1cdab234b5c6494594d8ce3c7f0a1f5 Mon Sep 17 00:00:00 2001 From: Delta1925 Date: Tue, 25 Apr 2023 14:44:14 +0200 Subject: [PATCH] Improve docker build speed --- .dockerignore | 1 + .github/workflows/docker.yml | 57 +++++++++++++++++++++++++++++------- Dockerfile | 11 ++++--- 3 files changed, 54 insertions(+), 15 deletions(-) diff --git a/.dockerignore b/.dockerignore index 5692dcc..39a501a 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,7 @@ *config.toml Dockerfile README.md +.github/workflows/* backend/data/* backend/logs/* backend/target/* diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 8272a31..971c063 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -5,27 +5,62 @@ on: types: [published] jobs: - build-and-push: + build: runs-on: ubuntu-latest strategy: matrix: - platform: - [linux/amd64, linux/arm64, linux/arm/v7, linux/arm/v6, linux/386] + include: + - platform: linux/amd64 + arch: x86_64 + - platform: linux/arm64 + arch: aarch64 + - platform: linux/arm/v7 + arch: armv7 + - platform: linux/arm/v6 + arch: arm + - platform: linux/386 + arch: i686 steps: - name: Set up QEMU uses: docker/setup-qemu-action@v2 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - - name: Login to Docker Hub - uses: docker/login-action@v2 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Build and push + - name: Build images uses: docker/build-push-action@v4 with: platforms: ${{ matrix.platform }} - push: true - tags: delta1925/simple-wkd:latest,delta1925/simple-wkd:${{ github.ref_name }} + tags: delta1925/simple-wkd:${{ matrix.arch }} + outputs: type=docker,dest=/tmp/simple-wkd-${{ matrix.arch }}.tar cache-from: type=gha cache-to: type=gha,mode=max + - name: Upload artifact + uses: actions/upload-artifact@v3 + with: + name: simple-wkd-${{ matrix.arch }} + path: /tmp/simple-wkd-${{ matrix.arch }}.tar + + push: + runs-on: ubuntu-latest + needs: build + strategy: + matrix: + include: + - tag: latest + - tag: ${{ github.ref_name }} + steps: + - name: Download artifacts + uses: actions/download-artifact@v3 + with: + path: /tmp/simple-wkd + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - name: Login to Docker Hub + run: docker login -p ${{ secrets.DOCKERHUB_TOKEN }} -u ${{ secrets.DOCKERHUB_USERNAME }} + - name: Load images + run: find /tmp/simple-wkd -iname "simple-wkd-*.tar" -exec docker load --input {} \ + - name: Create manifest + run: docker manifest create delta1925/simple-wkd:${{ matrix.tag }} simple-wkd:x86_64 simple-wkd:aarch64 simple-wkd:armv7 simple-wkd:arm simple-wkd:i686 + - name: Push images + run: docker matifest push delta1925/simple-wkd:${{ matrix.tag }} + - name: Logout from Docker hub + run: docker logout \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 40a96b3..abcc78f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,26 +2,29 @@ ARG base="alpine:3.17" FROM ${base} AS bin-builder +# Disable static linking, see: https://users.rust-lang.org/t/sigsegv-with-program-linked-against-openssl-in-an-alpine-container/52172 +# Enable cargo sparse index for faster update times, see: https://blog.rust-lang.org/inside-rust/2023/01/30/cargo-sparse-protocol.html +ENV RUSTFLAGS='-C target-feature=-crt-static' \ + CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse RUN apk add --no-cache cargo openssl-dev musl-dev # This will build all dependencies and store them in docker's cache. # This way, it won't be necessary to recompile everything everytime -# It also disables static linking, see: https://users.rust-lang.org/t/sigsegv-with-program-linked-against-openssl-in-an-alpine-container/52172 COPY backend/Cargo.toml . COPY backend/Cargo.lock . RUN echo '[[bin]]' >> Cargo.toml && \ echo 'name = "cache"' >> Cargo.toml && \ echo 'path = "cache.rs"' >> Cargo.toml && \ echo 'fn main() {eprintln!("Caching crates...")}' > cache.rs && \ - RUSTFLAGS='-C target-feature=-crt-static' cargo build --release + cargo build --release RUN rm cache.rs && \ rm Cargo.toml # Build wimple-wkd COPY backend/Cargo.toml . COPY backend/src src -RUN RUSTFLAGS='-C target-feature=-crt-static' cargo build --release +RUN cargo build --release -FROM ${base} AS webpage-builder +FROM --platform=$BUILDPLATFORM ${base} AS webpage-builder RUN apk add --no-cache npm COPY website .