From 3e88ec037a69ed98160095190a79c0ea3d3636b3 Mon Sep 17 00:00:00 2001 From: Bastian Oppermann Date: Sun, 11 Aug 2024 22:37:18 +0200 Subject: [PATCH] Dockerize application and add CI --- .github/workflows/main.yml | 50 ++++++++++++++++++++++++++++++++++++++ Dockerfile | 32 ++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 .github/workflows/main.yml create mode 100644 Dockerfile diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..4a6c332 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,50 @@ +name: ci + +on: + push: + branches: + - "**" + tags: + - "v*.*.*" + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + toolchain: + - 1.78.0 + steps: + - uses: actions/checkout@v4 + - run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }} + - run: cargo build --verbose + - run: cargo test --verbose + docker: + runs-on: ubuntu-latest + steps: + # See https://web.archive.org/web/20240729104252/https://docs.docker.com/build/ci/github-actions/manage-tags-labels/ + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: | + ghcr.io/Bastian/data-processor + tags: | + type=ref,event=branch + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=sha + # See https://web.archive.org/web/20240709084932/https://docs.github.com/en/actions/creating-actions/creating-a-docker-container-action + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Build and push + uses: docker/build-push-action@v6 + with: + platforms: linux/amd64,linux/arm64 + push: true + tags: ${{ steps.meta.outputs.tags }} + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..861b8bb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,32 @@ +ARG RUST_VERSION=1.79.0 +ARG APP_NAME=data-processor + + +FROM rust:${RUST_VERSION} AS build +ARG APP_NAME +WORKDIR /app + +# Build the application. +# Leverage a cache mount to /usr/local/cargo/registry/ +# for downloaded dependencies, a cache mount to /usr/local/cargo/git/db +# for git repository dependencies, and a cache mount to /app/target/ for +# compiled dependencies which will speed up subsequent builds. +# Leverage a bind mount to the src directory to avoid having to copy the +# source code into the container. Once built, copy the executable to an +# output directory before the cache mounted /app/target is unmounted. +RUN --mount=type=bind,source=src,target=src \ + --mount=type=bind,source=Cargo.toml,target=Cargo.toml \ + --mount=type=bind,source=Cargo.lock,target=Cargo.lock \ + --mount=type=cache,target=/app/target/ \ + --mount=type=cache,target=/usr/local/cargo/git/db \ + --mount=type=cache,target=/usr/local/cargo/registry/ \ +cargo build --locked --release && \ +cp ./target/release/$APP_NAME /bin/server + +FROM debian:stable-slim AS final + +COPY --from=build /bin/server /bin/ + +EXPOSE 8080 + +CMD ["/bin/server"]