From 59dfc33e8f8994cf75b6069e5d4ed0e47dddcba5 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sun, 21 Mar 2021 16:29:53 +0100 Subject: [PATCH 1/6] Updated Makefile to add pushing of image --- Makefile | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 69f4a4a..76b08f0 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ -IMAGE := rust-api:latest +IMAGE := chewingbever/fej +TAG := 0.1-dev shell := /bin/bash @@ -15,9 +16,12 @@ release: .PHONY: release image: Dockerfile - @ docker build -t '$(IMAGE)' . + @ docker build -t '$(IMAGE):$(TAG)' . .PHONY: image +push: image + @ docker push '$(IMAGE):$(TAG)' +.PHONY: push # Run run: From 35fc9be5dafa4cc8bf9ec9a988754bed3625283e Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Mon, 22 Mar 2021 16:36:01 +0100 Subject: [PATCH 2/6] Failed attempt at fixing docker build --- Dockerfile | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/Dockerfile b/Dockerfile index a5148fe..9de43f3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,34 +1,28 @@ -FROM rust:alpine AS builder +FROM alpine:latest AS builder + +ENV PATH "$PATH:/root/.cargo/bin" # Switch to the nightly build -RUN rustup default nightly - WORKDIR /usr/src/app -# Install build dependencies & create a new dummy project -# that we use as a build cache -RUN apk update && \ - apk add --no-cache openssl openssl-dev musl-dev && \ - cargo init --bin +# Install build dependencies, install rustup & create dummy project +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; } && \ + rustup target add x86_64-unknown-linux-musl # Build the dependencies # This is done separately to reduce average compile time # Afterwards, we remove the dummy src directory -COPY Cargo.toml Cargo.lock ./ -RUN cargo build --release && \ - rm src/*.rs - # Now, we build our own source code -COPY src/ ./src -RUN cargo install --path . +COPY Cargo.toml Cargo.lock ./ +COPY src/ ./src/ +RUN cargo build --release --target x86_64-unknown-linux-musl # Now, we create the actual image -FROM alpine:latest +FROM scratch # Install dependencies -RUN apk update && apk add --no-cache openssl +COPY --from=builder /usr/src/app/target/x86_64-unknown-linux-musl/release/rust-api /rust-api -COPY --from=builder /usr/local/cargo/bin/rust-api /usr/local/bin/rust-api - -CMD ["rust-api"] +CMD ["/rust-api"] From 82b2927b3393db44d80bc396eb4a997a062fa956 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Tue, 23 Mar 2021 00:08:54 +0100 Subject: [PATCH 3/6] Fixed broken Docker image --- Cargo.toml | 4 ++-- Dockerfile | 30 +++++++++++++++++------------- Makefile | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c994ff9..a34a58e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "rust-api" -version = "0.1.0" +name = "fej" +version = "0.0.1" authors = ["Jef Roosens "] edition = "2018" diff --git a/Dockerfile b/Dockerfile index 9de43f3..4a3ed8b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,28 +1,32 @@ +# 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" -# Switch to the nightly build WORKDIR /usr/src/app -# Install build dependencies, install rustup & create dummy project +# 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; } && \ - rustup target add x86_64-unknown-linux-musl + { curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain nightly; } -# Build the dependencies -# This is done separately to reduce average compile time -# Afterwards, we remove the dummy src directory -# Now, we build our own source code +# Copy source code over to builder COPY Cargo.toml Cargo.lock ./ COPY src/ ./src/ -RUN cargo build --release --target x86_64-unknown-linux-musl + +# 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 +RUN RUSTFLAGS="-C target-feature=-crt-static" cargo build --release # Now, we create the actual image -FROM scratch +FROM alpine:latest -# Install dependencies -COPY --from=builder /usr/src/app/target/x86_64-unknown-linux-musl/release/rust-api /rust-api +# Install some dynamic libraries needed for everything to work +RUN apk update && apk add --no-cache openssl libgcc -CMD ["/rust-api"] +# Copy binary over to final image +COPY --from=builder /usr/src/app/target/release/fej /usr/local/bin/fej + +CMD ["/usr/local/bin/fej"] diff --git a/Makefile b/Makefile index 76b08f0..603ad5d 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ IMAGE := chewingbever/fej -TAG := 0.1-dev +TAG := 0.0.1-dev shell := /bin/bash From cedcc3fc9f7ac0ec9ccd9f0fece1b5fbd9843e3e Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Tue, 23 Mar 2021 09:37:18 +0100 Subject: [PATCH 4/6] Started build script --- Makefile | 6 +++--- build | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100755 build diff --git a/Makefile b/Makefile index 603ad5d..e878d28 100644 --- a/Makefile +++ b/Makefile @@ -16,11 +16,11 @@ release: .PHONY: release image: Dockerfile - @ docker build -t '$(IMAGE):$(TAG)' . + @ bash ./build '$(IMAGE)' .PHONY: image -push: image - @ docker push '$(IMAGE):$(TAG)' +push: + @ bash ./build '$(IMAGE)' push .PHONY: push # Run diff --git a/build b/build new file mode 100755 index 0000000..480b6f1 --- /dev/null +++ b/build @@ -0,0 +1,50 @@ +#!/usr/bin/env bash + +# Simple guard to check input args +[[ $# -eq 1 ]] || [[ $# -eq 2 ]] || { + >&2 echo "Usage: ./build IMAGE [ACTION]" + exit 1 +} + +# Extract current version from Cargo.toml & get current branch +patch_version="$(grep -Po '(?<=version = ").*(?=")' Cargo.toml | head -n1)" +major_version="$(echo "$patch_version" | + sed -E 's/([0-9]+)\.([0-9]+)\.([0-9]+)/\1/')" +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 + tags=("$patch_version" "$minor_version" "$major_version" ) + +elif [[ "$branch" = "develop" ]]; then + tags=("$patch_version-dev" "$minor_version-dev" "$major_version-dev" ) + +else + tags=("$branch") + +fi + +tag_flags=() + +for tag in "${tags[@]}"; do + tag_flags+=("-t '$1:$tag'") + +done + +# Run the actual build command +docker build $tag_flags . + +if [[ "$2" = push ]]; then + for tag in "${tags[@]}"; do + docker push "$1:$tag" + done + + elif [[ "$2" = run ]]; then + docker run \ + --rm \ + --interactive \ + --tty \ + --publish 8000:8000 \ + "$1:$tags" +fi From ab94583e62fae1db9f300b702335057260b52525 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Tue, 23 Mar 2021 10:42:20 +0100 Subject: [PATCH 5/6] Fixed wrinkles in build script --- Cargo.lock | 20 ++++++++++---------- Makefile | 6 ++---- build | 9 +++++++-- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 85dbcb8..d7b6fcf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -313,6 +313,16 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "fej" +version = "0.0.1" +dependencies = [ + "reqwest", + "rocket", + "rocket_contrib", + "serde", +] + [[package]] name = "filetime" version = "0.2.14" @@ -1254,16 +1264,6 @@ dependencies = [ "unicode-xid 0.1.0", ] -[[package]] -name = "rust-api" -version = "0.1.0" -dependencies = [ - "reqwest", - "rocket", - "rocket_contrib", - "serde", -] - [[package]] name = "rustc_version" version = "0.2.3" diff --git a/Makefile b/Makefile index e878d28..4017a77 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,4 @@ IMAGE := chewingbever/fej -TAG := 0.0.1-dev -shell := /bin/bash all: debug @@ -16,11 +14,11 @@ release: .PHONY: release image: Dockerfile - @ bash ./build '$(IMAGE)' + @ ./build '$(IMAGE)' .PHONY: image push: - @ bash ./build '$(IMAGE)' push + @ ./build '$(IMAGE)' push .PHONY: push # Run diff --git a/build b/build index 480b6f1..85adacf 100755 --- a/build +++ b/build @@ -15,7 +15,7 @@ minor_version="$(echo "$patch_version" | branch="$(git branch --show-current)" if [[ "$branch" = "master" ]]; then - tags=("$patch_version" "$minor_version" "$major_version" ) + tags=("$patch_version" "$minor_version" "$major_version" "latest" ) elif [[ "$branch" = "develop" ]]; then tags=("$patch_version-dev" "$minor_version-dev" "$major_version-dev" ) @@ -28,7 +28,7 @@ fi tag_flags=() for tag in "${tags[@]}"; do - tag_flags+=("-t '$1:$tag'") + tag_flags+=("-t $1:$tag") done @@ -36,6 +36,11 @@ done docker build $tag_flags . if [[ "$2" = push ]]; then + [[ "$branch" =~ ^develop|master$ ]] || { + >&2 echo "You can only push from develop or master." + exit 2 + } + for tag in "${tags[@]}"; do docker push "$1:$tag" done From e81205bef73cfbb88e4b98e34cde51e4004f5695 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Tue, 23 Mar 2021 10:55:00 +0100 Subject: [PATCH 6/6] Added working auto-tagging --- build | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/build b/build index 85adacf..0117f6a 100755 --- a/build +++ b/build @@ -15,25 +15,18 @@ minor_version="$(echo "$patch_version" | branch="$(git branch --show-current)" if [[ "$branch" = "master" ]]; then - tags=("$patch_version" "$minor_version" "$major_version" "latest" ) + tags=("$patch_version" "$minor_version" "$major_version" "latest") elif [[ "$branch" = "develop" ]]; then - tags=("$patch_version-dev" "$minor_version-dev" "$major_version-dev" ) + tags=("$patch_version-dev" "$minor_version-dev" "$major_version-dev" "dev") else tags=("$branch") fi -tag_flags=() - -for tag in "${tags[@]}"; do - tag_flags+=("-t $1:$tag") - -done - # Run the actual build command -docker build $tag_flags . +docker build -t "$1:$tags" . if [[ "$2" = push ]]; then [[ "$branch" =~ ^develop|master$ ]] || { @@ -42,7 +35,14 @@ if [[ "$2" = push ]]; then } for tag in "${tags[@]}"; do + # Create the tag + docker tag "$1:$tags" "$1:$tag" + + # Push the tag docker push "$1:$tag" + + # Remove the tag again, if it's not the main tag + [[ "$tag" != "$tags" ]] && docker rmi "$1:$tag" done elif [[ "$2" = run ]]; then