From 5bc464dc53db14e222a955b2e9ae1960089b4d84 Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Tue, 30 Mar 2021 15:01:51 +0300 Subject: [PATCH 1/2] chore(ci): cache dependencies in Docker build --- Dockerfile | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 9898d02db..50ddddfcc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,10 +9,23 @@ RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y WORKDIR /meilisearch -COPY . . +COPY Cargo.lock . +COPY Cargo.toml . + +COPY meilisearch-error/Cargo.toml meilisearch-error/ +COPY meilisearch-http/Cargo.lock meilisearch-http/ +COPY meilisearch-http/Cargo.toml meilisearch-http/ ENV RUSTFLAGS="-C target-feature=-crt-static" +# Create dummy main.rs files for each workspace member to be able to compile all the dependencies +RUN find . -type d -name "meilisearch-*" | xargs -I{} sh -c 'mkdir {}/src; echo "fn main() { }" > {}/src/main.rs;' +# Use `cargo build` instead of `cargo vendor` because we need to not only download but compile dependencies too +RUN $HOME/.cargo/bin/cargo build --release +# Cleanup dummy main.rs files +RUN find . -path "*/src/main.rs" -delete + +COPY . . RUN $HOME/.cargo/bin/cargo build --release # Run From 3d51db59292d96f27b8360b229e4416413a29e3e Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Tue, 30 Mar 2021 20:03:13 +0300 Subject: [PATCH 2/2] fix(ci, http): commit_sha and commit_date in docker builds chore(ci): cache dependencies in Docker build --- .github/workflows/publish_to_docker.yml | 6 ++++++ Dockerfile | 5 +++++ meilisearch-http/src/routes/stats.rs | 15 ++++++++++++--- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/.github/workflows/publish_to_docker.yml b/.github/workflows/publish_to_docker.yml index d724f7253..60422a88e 100644 --- a/.github/workflows/publish_to_docker.yml +++ b/.github/workflows/publish_to_docker.yml @@ -11,10 +11,16 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 + - name: Set COMMIT_DATE env variable + run: | + echo "COMMIT_DATE=$( git log --pretty=format:'%ad' -n1 --date=short )" >> $GITHUB_ENV - name: Publish to Registry uses: elgohr/Publish-Docker-Github-Action@master + env: + COMMIT_SHA: ${{ github.sha }} with: name: getmeili/meilisearch username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} tag_names: true + buildargs: COMMIT_SHA,COMMIT_DATE diff --git a/Dockerfile b/Dockerfile index 50ddddfcc..a45d2f7e5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,6 +25,11 @@ RUN $HOME/.cargo/bin/cargo build --release # Cleanup dummy main.rs files RUN find . -path "*/src/main.rs" -delete +ARG COMMIT_SHA +ARG COMMIT_DATE +ENV COMMIT_SHA=${COMMIT_SHA} +ENV COMMIT_DATE=${COMMIT_DATE} + COPY . . RUN $HOME/.cargo/bin/cargo build --release diff --git a/meilisearch-http/src/routes/stats.rs b/meilisearch-http/src/routes/stats.rs index 172b5dde9..108c67ca9 100644 --- a/meilisearch-http/src/routes/stats.rs +++ b/meilisearch-http/src/routes/stats.rs @@ -56,9 +56,18 @@ struct VersionResponse { #[get("/version", wrap = "Authentication::Private")] async fn get_version() -> HttpResponse { - HttpResponse::Ok().json(VersionResponse { - commit_sha: env!("VERGEN_SHA").to_string(), - build_date: env!("VERGEN_BUILD_TIMESTAMP").to_string(), + let commit_sha = match option_env!("COMMIT_SHA") { + Some("") | None => env!("VERGEN_SHA"), + Some(commit_sha) => commit_sha + }; + let commit_date = match option_env!("COMMIT_DATE") { + Some("") | None => env!("VERGEN_COMMIT_DATE"), + Some(commit_date) => commit_date + }; + + HttpResponse::Ok().json(VersionResponse { + commit_sha: commit_sha.to_string(), + build_date: commit_date.to_string(), pkg_version: env!("CARGO_PKG_VERSION").to_string(), }) }