diff --git a/.dockerignore b/.dockerignore index 3d3202d..29c586b 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1,4 @@ +.git Dockerfile -**/README.md \ No newline at end of file +README.md +/target/ \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ad3b883..4e51772 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,6 +3,8 @@ name: CI on: push: + tags-ignore: + - unstable pull_request: env: @@ -28,28 +30,17 @@ jobs: export DEBIAN_FRONTEND=noninteractive sudo apt-get clean && sudo apt-get update sudo apt-get install -y pkg-config libudev-dev - - name: Cache cargo registry - uses: actions/cache@v1 + - name: Cargo cache + uses: actions/cache@v2 with: - path: ~/.cargo/registry - key: ${{ runner.os }}-cargo-registry-toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }} + path: | + ~/.cargo/git + ~/.cargo/registry + ./target + key: ${{ runner.os }}-cargo-${{ steps.rust-toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }} restore-keys: | - ${{ runner.os }}-cargo-registry- - - name: Cache cargo index - uses: actions/cache@v1 - with: - path: ~/.cargo/git - key: ${{ runner.os }}-cargo-index-toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }} - restore-keys: | - ${{ runner.os }}-cargo-index- - - name: Cache cargo build - uses: actions/cache@v1 - with: - path: target - key: ${{ runner.os }}-cargo-build-target-${{ steps.rust-toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }} - restore-keys: | - ${{ runner.os }}-cargo-build-target-${{ steps.rust-toolchain.outputs.rustc_hash }}- - ${{ runner.os }}-cargo-build-target- + ${{ runner.os }}-cargo-${{ steps.rust-toolchain.outputs.rustc_hash }}- + ${{ runner.os }}-cargo - name: Run tests uses: actions-rs/cargo@v1 with: @@ -73,30 +64,17 @@ jobs: export DEBIAN_FRONTEND=noninteractive sudo apt-get clean && sudo apt-get update sudo apt-get install -y pkg-config libudev-dev - - name: Cache cargo registry - uses: actions/cache@v1 + - name: Cargo cache + uses: actions/cache@v2 with: - path: ~/.cargo/registry - key: ${{ runner.os }}-cargo-registry-${{ steps.rust-toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }} + path: | + ~/.cargo/git + ~/.cargo/registry + ./target + key: ${{ runner.os }}-cargo-${{ steps.rust-toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }} restore-keys: | - ${{ runner.os }}-cargo-registry-${{ steps.rust-toolchain.outputs.rustc_hash }}- - ${{ runner.os }}-cargo-registry- - - name: Cache cargo index - uses: actions/cache@v1 - with: - path: ~/.cargo/git - key: ${{ runner.os }}-cargo-index-${{ steps.rust-toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }} - restore-keys: | - ${{ runner.os }}-cargo-index-${{ steps.rust-toolchain.outputs.rustc_hash }}- - ${{ runner.os }}-cargo-index- - - name: Cache cargo build - uses: actions/cache@v1 - with: - path: target - key: ${{ runner.os }}-cargo-build-target-${{ steps.rust-toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }} - restore-keys: | - ${{ runner.os }}-cargo-build-target-${{ steps.rust-toolchain.outputs.rustc_hash }}- - ${{ runner.os }}-cargo-build-target- + ${{ runner.os }}-cargo-${{ steps.rust-toolchain.outputs.rustc_hash }}- + ${{ runner.os }}-cargo - name: Check rustfmt uses: actions-rs/cargo@v1 with: @@ -150,9 +128,16 @@ jobs: uses: actions/cache@v2 with: path: | + ~/.cargo/git ~/.cargo/registry ./target - key: build-cargo-registry-${{matrix.TARGET}} + key: ${{ runner.os }}-cargo-${{matrix.TARGET}}-${{ steps.rust-toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }} + restore-keys: | + ${{ runner.os }}-cargo-${{matrix.TARGET}}-${{ steps.rust-toolchain.outputs.rustc_hash }} + ${{ runner.os }}-cargo-${{matrix.TARGET}}- + ${{ runner.os }}-cargo-${{ steps.rust-toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }} + ${{ runner.os }}-cargo-${{ steps.rust-toolchain.outputs.rustc_hash }}- + ${{ runner.os }}-cargo - name: Setup cross linux toolchain if: contains(matrix.TARGET, '-linux-') && !startsWith(matrix.TARGET, 'x86_64-') run: | @@ -197,6 +182,8 @@ jobs: if: contains(matrix.TARGET, '-linux-') run: | # some additional configuration for cross-compilation on linux + # TODO: can this be done with `RUSTFLAGS += -C linker=$(DEB_HOST_GNU_TYPE)-gcc`? + cat >>~/.cargo/config < /etc/apt/apt.conf.d/keep-cache +RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ + --mount=type=cache,target=/var/lib/apt,sharing=locked \ + apt update && apt-get --no-install-recommends install -y \ + libudev1 COPY --from=builder \ - /home/rust/modbus-mqtt/target/release/modbus-mqtt \ - /usr/local/bin/ + /home/rust/modbus-mqtt/bin \ + /usr/local/bin/modbus-mqtt ENV RUST_LOG=warn,modbus_mqtt=info diff --git a/Dockerfile.alpine b/Dockerfile.alpine new file mode 100644 index 0000000..b14a550 --- /dev/null +++ b/Dockerfile.alpine @@ -0,0 +1,34 @@ +# -*- mode: dockerfile -*- + +FROM rust:1.63-alpine AS builder + +RUN apk add --no-cache musl-dev + +# Build a cacheable layer with project dependencies +RUN USER=rust cargo new /home/rust/sungrow-winets +RUN USER=rust cargo new /home/rust/tokio_modbus-winets +RUN USER=rust cargo new /home/rust/modbus-mqtt +WORKDIR /home/rust/modbus-mqtt +ADD --chown=rust:rust Cargo.lock modbus-mqtt/Cargo.toml ./ +RUN cargo build --release + +# # Delete files & directories which shouldn't exist for the workspace +# RUN rm -rf src + +# Add our source code. +ADD --chown=rust:rust . ./ + +# Build our application. +RUN cargo build --release + +# Now, we need to build our _real_ Docker container, copying in `bump-api`. +FROM alpine:latest +RUN apk --no-cache add ca-certificates + +COPY --from=builder \ + /home/rust/modbus-mqtt/target/release/modbus-mqtt \ + /usr/local/bin/ + +ENV RUST_LOG=warn,modbus_mqtt=info + +ENTRYPOINT ["/usr/local/bin/modbus-mqtt"]