1
0
Fork 0

Merge branch 'docker'

* docker:
  Don't build docker images for branches unless PR
  Give up on ARMv7 builds for now
  Fix typo
  Try again to workaround docker/buildx#395
  Fails with --ofline
  Fix typo in Dockerfile
  Try to workaround docker/buildx#395
  Improve build caching
  Build docker images
main
Bo Jeanes 2022-09-18 14:00:57 +10:00
commit e5e7176bc3
4 changed files with 192 additions and 54 deletions

View File

@ -1,2 +1,4 @@
.git
Dockerfile Dockerfile
**/README.md README.md
/target/

View File

@ -3,6 +3,8 @@
name: CI name: CI
on: on:
push: push:
tags-ignore:
- unstable
pull_request: pull_request:
env: env:
@ -28,28 +30,17 @@ jobs:
export DEBIAN_FRONTEND=noninteractive export DEBIAN_FRONTEND=noninteractive
sudo apt-get clean && sudo apt-get update sudo apt-get clean && sudo apt-get update
sudo apt-get install -y pkg-config libudev-dev sudo apt-get install -y pkg-config libudev-dev
- name: Cache cargo registry - name: Cargo cache
uses: actions/cache@v1 uses: actions/cache@v2
with: with:
path: ~/.cargo/registry path: |
key: ${{ runner.os }}-cargo-registry-toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }} ~/.cargo/git
~/.cargo/registry
./target
key: ${{ runner.os }}-cargo-${{ steps.rust-toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }}
restore-keys: | restore-keys: |
${{ runner.os }}-cargo-registry- ${{ runner.os }}-cargo-${{ steps.rust-toolchain.outputs.rustc_hash }}-
- name: Cache cargo index ${{ runner.os }}-cargo
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-
- name: Run tests - name: Run tests
uses: actions-rs/cargo@v1 uses: actions-rs/cargo@v1
with: with:
@ -73,30 +64,17 @@ jobs:
export DEBIAN_FRONTEND=noninteractive export DEBIAN_FRONTEND=noninteractive
sudo apt-get clean && sudo apt-get update sudo apt-get clean && sudo apt-get update
sudo apt-get install -y pkg-config libudev-dev sudo apt-get install -y pkg-config libudev-dev
- name: Cache cargo registry - name: Cargo cache
uses: actions/cache@v1 uses: actions/cache@v2
with: with:
path: ~/.cargo/registry path: |
key: ${{ runner.os }}-cargo-registry-${{ steps.rust-toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }} ~/.cargo/git
~/.cargo/registry
./target
key: ${{ runner.os }}-cargo-${{ steps.rust-toolchain.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.lock') }}
restore-keys: | restore-keys: |
${{ runner.os }}-cargo-registry-${{ steps.rust-toolchain.outputs.rustc_hash }}- ${{ runner.os }}-cargo-${{ steps.rust-toolchain.outputs.rustc_hash }}-
${{ runner.os }}-cargo-registry- ${{ runner.os }}-cargo
- 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-
- name: Check rustfmt - name: Check rustfmt
uses: actions-rs/cargo@v1 uses: actions-rs/cargo@v1
with: with:
@ -150,9 +128,16 @@ jobs:
uses: actions/cache@v2 uses: actions/cache@v2
with: with:
path: | path: |
~/.cargo/git
~/.cargo/registry ~/.cargo/registry
./target ./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 - name: Setup cross linux toolchain
if: contains(matrix.TARGET, '-linux-') && !startsWith(matrix.TARGET, 'x86_64-') if: contains(matrix.TARGET, '-linux-') && !startsWith(matrix.TARGET, 'x86_64-')
run: | run: |
@ -197,6 +182,8 @@ jobs:
if: contains(matrix.TARGET, '-linux-') if: contains(matrix.TARGET, '-linux-')
run: | run: |
# some additional configuration for cross-compilation on linux # 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 <<EOF cat >>~/.cargo/config <<EOF
[target.aarch64-unknown-linux-gnu] [target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc" linker = "aarch64-linux-gnu-gcc"
@ -242,7 +229,9 @@ jobs:
prerelease: prerelease:
name: Create a pre-release name: Create a pre-release
if: github.ref == 'refs/heads/main' if: github.ref == 'refs/heads/main'
needs: build needs:
- build
- docker
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout code - name: Checkout code
@ -311,3 +300,103 @@ jobs:
body: ${{ steps.changelog_reader.outputs.changes }} body: ${{ steps.changelog_reader.outputs.changes }}
files: ${{ steps.artifacts.outputs.files }} files: ${{ steps.artifacts.outputs.files }}
gzip: false gzip: false
docker:
if: github.ref == 'refs/heads/main' || github.event_name == 'pull_request'
needs:
- tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: |
${{ github.repository }}
ghcr.io/${{ github.repository }}
tags: |
type=sha,format=short
type=edge,branch=main
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}},enable=${{ !contains(github.ref, 'v0.') }}
type=ref,enable=true,priority=600,prefix=br-,suffix=,event=branch
type=ref,enable=true,priority=600,prefix=,suffix=,event=tag
type=ref,enable=true,priority=600,prefix=pr-,suffix=,event=pr
- name: Docker meta
id: meta-alpine
uses: docker/metadata-action@v4
with:
flavor:
suffix=-alpine
images: |
${{ github.repository }}
ghcr.io/${{ github.repository }}
tags: |
type=sha,format=short
type=edge,branch=main
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}},enable=${{ !contains(github.ref, 'v0.') }}
type=ref,enable=true,priority=600,prefix=br-,suffix=,event=branch
type=ref,enable=true,priority=600,prefix=,suffix=,event=tag
type=ref,enable=true,priority=600,prefix=pr-,suffix=,event=pr
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
# Work around https://github.com/docker/buildx/issues/395 (this causes "no space left on device")
# - name: Run Docker on tmpfs
# uses: JonasAlfredsson/docker-on-tmpfs@v1
# with:
# tmpfs_size: 5
# swap_size: 4
- name: Build and push (alpine)
uses: docker/build-push-action@v3
with:
context: .
platforms: |
linux/arm64
linux/amd64
push: true
file: Dockerfile.alpine
tags: ${{ steps.meta-alpine.outputs.tags }}
labels: ${{ steps.meta-alpine.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Build and push
uses: docker/build-push-action@v3
with:
context: .
# Disabled due to https://github.com/docker/buildx/issues/395
# linux/arm/v7
platforms: |
linux/arm64
linux/amd64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max

View File

@ -1,8 +1,11 @@
# -*- mode: dockerfile -*- # -*- mode: dockerfile -*-
FROM rust:1.63-alpine AS builder FROM rust:1.63 AS builder
RUN apk add --no-cache musl-dev 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 \
libudev-dev
# Build a cacheable layer with project dependencies # Build a cacheable layer with project dependencies
RUN USER=rust cargo new /home/rust/sungrow-winets RUN USER=rust cargo new /home/rust/sungrow-winets
@ -10,7 +13,10 @@ RUN USER=rust cargo new /home/rust/tokio_modbus-winets
RUN USER=rust cargo new /home/rust/modbus-mqtt RUN USER=rust cargo new /home/rust/modbus-mqtt
WORKDIR /home/rust/modbus-mqtt WORKDIR /home/rust/modbus-mqtt
ADD --chown=rust:rust Cargo.lock modbus-mqtt/Cargo.toml ./ ADD --chown=rust:rust Cargo.lock modbus-mqtt/Cargo.toml ./
RUN cargo build --release RUN mkdir -p /home/rust/modbus-mqtt/target/release
RUN --mount=type=cache,target=/home/rust/modbus-mqtt/target,sharing=locked \
--mount=type=cache,target=/usr/local/cargo/registry,sharing=locked \
cargo build --release
# # Delete files & directories which shouldn't exist for the workspace # # Delete files & directories which shouldn't exist for the workspace
# RUN rm -rf src # RUN rm -rf src
@ -19,15 +25,22 @@ RUN cargo build --release
ADD --chown=rust:rust . ./ ADD --chown=rust:rust . ./
# Build our application. # Build our application.
RUN cargo build --release RUN --mount=type=cache,target=/home/rust/modbus-mqtt/target,sharing=locked \
--mount=type=cache,target=/usr/local/cargo/registry,sharing=locked \
cargo build --release && mv target/release/modbus-mqtt ./bin
# Now, we need to build our _real_ Docker container, copying in `bump-api`. # Now, we need to build our _real_ Docker container, copying in `bump-api`.
FROM alpine:latest FROM debian:bullseye-slim
RUN apk --no-cache add ca-certificates
RUN rm -f /etc/apt/apt.conf.d/docker-clean; echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /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 \ COPY --from=builder \
/home/rust/modbus-mqtt/target/release/modbus-mqtt \ /home/rust/modbus-mqtt/bin \
/usr/local/bin/ /usr/local/bin/modbus-mqtt
ENV RUST_LOG=warn,modbus_mqtt=info ENV RUST_LOG=warn,modbus_mqtt=info

34
Dockerfile.alpine 100644
View File

@ -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"]