Compare commits

..

No commits in common. "c51401f7d876b3fc56e7db884ca1bae6dcd74b0e" and "7243be302c773028c63e4cf069d8bf59a97012df" have entirely different histories.

6 changed files with 68 additions and 115 deletions

37
Dockerfile 100644
View File

@ -0,0 +1,37 @@
# syntax = docker/dockerfile:1.2
# We use a multi-stage build to end up with a very small final image
FROM alpine:latest AS builder
ENV PATH "$PATH:/root/.cargo/bin"
WORKDIR /usr/src/app
# Install build dependencies, rustup & rust's nightly build & toolchain
RUN apk update && apk add --no-cache openssl-dev build-base curl && \
{ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain nightly; }
# Copy source code over to builder
COPY Cargo.toml Cargo.lock ./
COPY src/ ./src/
# Run the tests, don't want no broken docker image
# And then finally, build the project
# Thank the lords that this article exists
# https://users.rust-lang.org/t/sigsegv-with-program-linked-against-openssl-in-an-alpine-container/52172
# TODO add what these flags do & why they work
# NOTE: cargo install auto-appends bin to the path
RUN --mount=type=cache,target=/usr/src/app/target RUSTFLAGS="-C target-feature=-crt-static" cargo test && \
RUSTFLAGS="-C target-feature=-crt-static" cargo install --path . --bin fej --root /usr/local
# Now, we create the actual image
FROM alpine:latest
# Install some dynamic libraries needed for everything to work
RUN apk update && apk add --no-cache openssl libgcc
# Copy binary over to final image
COPY --from=builder /usr/local/bin/fej /usr/local/bin/fej
CMD ["/usr/local/bin/fej"]

View File

@ -1,36 +1,35 @@
IMAGE := chewingbever/fej
all: debug all: debug
.PHONY: all .PHONY: all
# Builds # Builds
debug: debug:
@ ./build -m dev @ cargo build
.PHONY: debug .PHONY: debug
release: release:
@ ./build -m rel @ cargo build --release
.PHONY: release .PHONY: release
image: Dockerfile
@ ./build '$(IMAGE)'
.PHONY: image
push: push:
@ ./build -m prod -a push @ ./build '$(IMAGE)' push
.PHONY: push .PHONY: push
# Run # Run
run: run:
@ ./build -m dev -a run @ RUST_BACKTRACE=1 cargo run --bin fej
.PHONY: run .PHONY: run
stop:
@ docker stop -t 2 fej
.PHONY: stop
logs:
@ docker logs -f fej
.PHONY: logs
# Testing # Testing
test: test:
@ ./build -m dev -a run -l -- test --no-fail-fast @ cargo test --no-fail-fast
.PHONY: test .PHONY: test
format: format:

74
build
View File

@ -1,27 +1,18 @@
#!/usr/bin/env bash #!/usr/bin/env bash
image="chewingbever/fej" # Simple guard to check input args
# Should be either dev or rel [[ $# -eq 1 ]] || [[ $# -eq 2 ]] || {
mode="dev" >&2 echo "Usage: ./build IMAGE [ACTION]"
action="" exit 1
attach="--detach" }
while getopts ":i:m:a:l" c; do
case $c in
i ) image="$OPTARG" ;;
m ) mode="$OPTARG" ;;
a ) action="$OPTARG" ;;
l ) attach="" ;;
? ) exit 1 ;;
esac
done
shift $((OPTIND-1))
# Extract current version from Cargo.toml & get current branch # Extract current version from Cargo.toml & get current branch
patch_version=`grep -Po '(?<=version = ").*(?=")' Cargo.toml | head -n1` patch_version="$(grep -Po '(?<=version = ").*(?=")' Cargo.toml | head -n1)"
major_version=`echo "$patch_version" | sed -E 's/([0-9]+)\.([0-9]+)\.([0-9]+)/\1/'` major_version="$(echo "$patch_version" |
minor_version=`echo "$patch_version" | sed -E 's/([0-9]+).([0-9]+).([0-9]+)/\1.\2/'` sed -E 's/([0-9]+)\.([0-9]+)\.([0-9]+)/\1/')"
branch=`git rev-parse --abbrev-ref HEAD` minor_version="$(echo "$patch_version" |
sed -E 's/([0-9]+).([0-9]+).([0-9]+)/\1.\2/')"
branch="$(git branch --show-current)"
if [[ "$branch" = "master" ]]; then if [[ "$branch" = "master" ]]; then
tags=("$patch_version" "$minor_version" "$major_version" "latest") tags=("$patch_version" "$minor_version" "$major_version" "latest")
@ -34,58 +25,31 @@ else
fi fi
# First, we build the builder
DOCKER_BUILDKIT=1 docker build -f docker/Dockerfile.builder -t "$image-builder:latest" .
# Run the actual build command # Run the actual build command
if [ "$mode" = "rel" ]; then DOCKER_BUILDKIT=1 docker build -t "$1:$tags" .
DOCKER_BUILDKIT=1 docker build -t "$image:$tags" -f docker/Dockerfile.rel .
elif [[ "$mode" = "dev" ]]; then if [[ "$2" = push ]]; then
DOCKER_BUILDKIT=1 docker build -t "$image-dev:$tags" -f docker/Dockerfile.dev .
else
>&2 echo "Invalid mode."
exit 1
fi
if [[ "$action" = push ]]; then
[[ "$branch" =~ ^develop|master$ ]] || { [[ "$branch" =~ ^develop|master$ ]] || {
>&2 echo "You can only push from develop or master." >&2 echo "You can only push from develop or master."
exit 2 exit 2
} }
[[ "$mode" = "rel" ]] || {
>&2 echo "You can only push release builds."
exit 3
}
for tag in "${tags[@]}"; do for tag in "${tags[@]}"; do
# Create the tag # Create the tag
docker tag "$image:$tags" "$image:$tag" docker tag "$1:$tags" "$1:$tag"
# Push the tag # Push the tag
docker push "$image:$tag" docker push "$1:$tag"
# Remove the tag again, if it's not the main tag # Remove the tag again, if it's not the main tag
[[ "$tag" != "$tags" ]] && docker rmi "$image:$tag" [[ "$tag" != "$tags" ]] && docker rmi "$1:$tag"
done done
elif [[ "$action" = run ]]; then elif [[ "$2" = run ]]; then
if [[ "$mode" = "dev" ]]; then docker run \
# Create caching volumes if needed (they need to be named)
docker volume create fej_build-cache
docker volume create fej_registry-cache
flags="-v fej_build-cache:/usr/src/app/target -v fej_registry-cache:/root/.cargo/registry"
fi
docker run $attach $flags \
--rm \ --rm \
--interactive \ --interactive \
--tty \ --tty \
--publish 8000:8000 \ --publish 8000:8000 \
--name fej \ "$1:$tags"
"$image$([[ "$mode" != "rel" ]] && echo "-dev"):$tags" "$@"
fi fi

View File

@ -1,19 +0,0 @@
# We use a multi-stage build to end up with a very small final image
FROM alpine:latest AS builder
ARG MODE
ARG RUN_TESTS
ENV PATH "$PATH:/root/.cargo/bin"
# Needed for proper compiling of openssl-dev
ENV RUSTFLAGS="-C target-feature=-crt-static"
WORKDIR /usr/src/app
# Install build dependencies, rustup & rust's nightly build & toolchain
RUN apk update && apk add --no-cache openssl-dev build-base curl && \
{ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain nightly; }
# Copy source code over to builder
COPY Cargo.toml Cargo.lock ./
COPY src/ ./src/

View File

@ -1,6 +0,0 @@
FROM chewingbever/fej-builder:latest
ENV RUST_BACKTRACE=1
ENTRYPOINT ["cargo"]
CMD ["run"]

View File

@ -1,22 +0,0 @@
FROM chewingbever/fej-builder:latest AS builder
# And then finally, build the project
# Thank the lords that this article exists
# https://users.rust-lang.org/t/sigsegv-with-program-linked-against-openssl-in-an-alpine-container/52172
# TODO add what these flags do & why they work
# NOTE: cargo install auto-appends bin to the path
RUN --mount=type=cache,target=/usr/src/app/target \
--mount=type=cache,target=/root/.cargo/registry \
cargo install --path . --bin fej --root /usr/local
# Now, we create the actual image
FROM alpine:latest
# Install some dynamic libraries needed for everything to work
RUN apk update && apk add --no-cache openssl libgcc
# Copy binary over to final image
COPY --from=builder /usr/local/bin/fej /usr/local/bin/fej
CMD ["/usr/local/bin/fej"]