From 7355ffa75c882a0572d077e2981e8961bf3a9abc Mon Sep 17 00:00:00 2001 From: Bo Jeanes Date: Sun, 11 Sep 2022 18:43:55 +1000 Subject: [PATCH] Build docker images --- .github/workflows/ci.yml | 91 +++++++++++++++++++++++++++++++++++++++- Dockerfile | 16 +++++-- Dockerfile.alpine | 34 +++++++++++++++ 3 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 Dockerfile.alpine diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ad3b883..1c3f88d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -242,7 +242,9 @@ jobs: prerelease: name: Create a pre-release if: github.ref == 'refs/heads/main' - needs: build + needs: + - build + - docker runs-on: ubuntu-latest steps: - name: Checkout code @@ -310,4 +312,89 @@ jobs: name: "Unstable (built from master)" body: ${{ steps.changelog_reader.outputs.changes }} files: ${{ steps.artifacts.outputs.files }} - gzip: false \ No newline at end of file + gzip: false + + docker: + 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: + flavour: + 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 }} + + - 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 }} + - name: Build and push + uses: docker/build-push-action@v3 + with: + context: . + platforms: | + linux/arm/v7 + linux/arm64 + linux/amd64 + 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 b14a550..7e7e864 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,11 @@ # -*- 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 RUN USER=rust cargo new /home/rust/sungrow-winets @@ -22,8 +25,13 @@ ADD --chown=rust:rust . ./ 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 +FROM debian:bullseye-slim + +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 \ /home/rust/modbus-mqtt/target/release/modbus-mqtt \ 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"]