From 124da02a42c333b5970e09d530707f584dec8127 Mon Sep 17 00:00:00 2001 From: alyssa Date: Fri, 25 Oct 2024 03:27:58 +0900 Subject: [PATCH] feat(ci): move rust docker target bash to shell script --- .github/workflows/rust.yml | 20 +++--- Dockerfile.bin | 5 -- Dockerfile.rust => ci/Dockerfile.rust | 3 +- ci/rust-docker-target.sh | 42 +++++++++++++ services/avatars/Dockerfile | 88 +++++++++++++++++++++++++++ 5 files changed, 139 insertions(+), 19 deletions(-) delete mode 100644 Dockerfile.bin rename Dockerfile.rust => ci/Dockerfile.rust (93%) create mode 100755 ci/rust-docker-target.sh create mode 100644 services/avatars/Dockerfile diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index d7e55963..ef0a0c69 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -12,8 +12,8 @@ on: - 'services/gateway/**' - 'services/avatars/**' - '.github/workflows/rust.yml' - - 'Dockerfile.rust' - - 'Dockerfile.bin' + - 'ci/Dockerfile.rust' + - 'ci/rust-docker-target.sh' - 'Cargo.toml' - 'Cargo.lock' @@ -40,7 +40,7 @@ jobs: with: # https://github.com/docker/build-push-action/issues/378 context: . - file: Dockerfile.rust + file: ci/Dockerfile.rust push: false cache-from: type=registry,ref=ghcr.io/pluralkit/docker-cache:rust cache-to: type=registry,ref=ghcr.io/pluralkit/docker-cache:rust,mode=max @@ -48,13 +48,7 @@ jobs: # add more binaries here - run: | - for binary in "api" "gateway" "avatars"; do - for tag in latest ${{ env.BRANCH_NAME }} ${{ github.sha }}; do - cat Dockerfile.bin | sed "s/__BINARY__/$binary/g" | docker build -t ghcr.io/pluralkit/$binary:$tag -f - . - done - if [ "${{ github.repository }}" == "PluralKit/PluralKit" ]; then - docker push ghcr.io/pluralkit/$binary:${{ env.BRANCH_NAME }} - docker push ghcr.io/pluralkit/$binary:${{ github.sha }} - [ "${{ env.BRANCH_NAME }}" == "main" ] && docker push ghcr.io/pluralkit/$binary:latest - fi - done + tag=${{ github.sha }} \ + branch=${{ env.BRANCH_NAME }} \ + push=$([ "${{ github.repository }}" == "PluralKit/PluralKit" ] && echo true || echo false) \ + ci/rust-docker-target.sh diff --git a/Dockerfile.bin b/Dockerfile.bin deleted file mode 100644 index f12cd703..00000000 --- a/Dockerfile.bin +++ /dev/null @@ -1,5 +0,0 @@ -FROM alpine:latest - -COPY /.docker-bin/__BINARY__ /bin/__BINARY__ - -CMD ["/bin/__BINARY__"] diff --git a/Dockerfile.rust b/ci/Dockerfile.rust similarity index 93% rename from Dockerfile.rust rename to ci/Dockerfile.rust index 2d94fe66..f019273e 100644 --- a/Dockerfile.rust +++ b/ci/Dockerfile.rust @@ -34,8 +34,9 @@ RUN cargo build --bin api --release --target x86_64-unknown-linux-musl RUN cargo build --bin gateway --release --target x86_64-unknown-linux-musl RUN cargo build --bin avatars --release --target x86_64-unknown-linux-musl -FROM scratch +FROM scratch COPY --from=binary-builder /build/target/x86_64-unknown-linux-musl/release/api /api COPY --from=binary-builder /build/target/x86_64-unknown-linux-musl/release/gateway /gateway COPY --from=binary-builder /build/target/x86_64-unknown-linux-musl/release/avatars /avatars +COPY --from=binary-builder /build/target/x86_64-unknown-linux-musl/release/avatars /avatars diff --git a/ci/rust-docker-target.sh b/ci/rust-docker-target.sh new file mode 100755 index 00000000..0bd8f038 --- /dev/null +++ b/ci/rust-docker-target.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +set -e + +#tag= +#branch= +#push= + +build() { + bin=$1 + extra=$2 + + f=$(mktemp) + + cat > $f << EOF +FROM alpine:latest +COPY .docker-bin/$bin /bin/$bin +$extra +CMD ["/bin/$bin"] +EOF + + echo "building $dockerfile" + + $dockerfile | docker build -t ghcr.io/pluralkit/$bin:$tag -f $f . + + rm $f + + if [ "$push" == "true" ]; then + docker push ghcr.io/pluralkit/$bin:$tag + docker image tag ghcr.io/pluralkit/$bin:$tag ghcr.io/pluralkit/$bin:$branch + docker push ghcr.io/pluralkit/$bin:$branch + if [ "$branch" == "main" ]; then + docker image tag ghcr.io/pluralkit/$bin:$tag ghcr.io/pluralkit/$bin:latest + docker push ghcr.io/pluralkit/$bin:latest + fi + fi +} + +# add rust binaries here to build +build api +build gateway +build avatars diff --git a/services/avatars/Dockerfile b/services/avatars/Dockerfile new file mode 100644 index 00000000..21864cc2 --- /dev/null +++ b/services/avatars/Dockerfile @@ -0,0 +1,88 @@ +# syntax=docker/dockerfile:1 + +# Comments are provided throughout this file to help you get started. +# If you need more help, visit the Dockerfile reference guide at +# https://docs.docker.com/go/dockerfile-reference/ + +ARG RUST_VERSION=1.75.0 +ARG APP_NAME=pluralkit-avatars + +################################################################################ +# xx is a helper for cross-compilation. +# See https://github.com/tonistiigi/xx/ for more information. +FROM --platform=$BUILDPLATFORM tonistiigi/xx:1.3.0 AS xx + +################################################################################ +# Create a stage for building the application. +FROM --platform=$BUILDPLATFORM rust:${RUST_VERSION}-alpine AS build +ARG APP_NAME +WORKDIR /app + +# Copy cross compilation utilities from the xx stage. +COPY --from=xx / / + +# Install host build dependencies. +RUN apk add --no-cache clang lld musl-dev git file + +# This is the architecture you’re building for, which is passed in by the builder. +# Placing it here allows the previous steps to be cached across architectures. +ARG TARGETPLATFORM + +# Install cross compilation build dependencies. +RUN xx-apk add --no-cache musl-dev gcc + +# 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. +# XXX: removed `id` from target mount, see: https://github.com/reproducible-containers/buildkit-cache-dance/issues/12 +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/$TARGETPLATFORM/ \ + --mount=type=cache,target=/usr/local/cargo/git/db \ + --mount=type=cache,target=/usr/local/cargo/registry/ \ + <