Skip to content

Commit

Permalink
Remove dependency on satackey/action-docker-layer-caching
Browse files Browse the repository at this point in the history
closes #145
  • Loading branch information
allada committed Jul 11, 2023
1 parent 1185876 commit 0801e26
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 24 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
@@ -0,0 +1,6 @@
target/
*/*/.terraform-turbo-cache-builder
*/*/.update_scheduler_ips.zip
*/*/terraform.tfstate
*/*/.terraform
.git
17 changes: 11 additions & 6 deletions .github/workflows/main.yml
Expand Up @@ -33,12 +33,17 @@ jobs:
with:
fetch-depth: 0

- uses: satackey/action-docker-layer-caching@v0.0.11
continue-on-error: true
- uses: docker/setup-buildx-action@v2
- uses: docker/build-push-action@v4
with:
key: ${{ runner.os }}-{hash}-v1
restore-keys: |
docker-compose-cache-
context: .
file: ./deployment-examples/docker-compose/Dockerfile
load: true # This brings the build into `docker images` from buildx.
tags: allada/turbo-cache:latest
cache-from: |
type=gha,scope=$GITHUB_REF_NAME
type=gha,scope=master
cache-to: type=gha,scope=$GITHUB_REF_NAME,mode=max

- name: Run tests
run: sudo ./run_integration_tests.sh
run: ./run_integration_tests.sh
91 changes: 75 additions & 16 deletions deployment-examples/docker-compose/Dockerfile
Expand Up @@ -12,39 +12,98 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM ubuntu:22.04 AS builder
# Override this if you want to run on a different version of ubuntu.
ARG OS_VERSION=22.04
# `--compilation_mode` to pass into bazel (eg: opt, dbg, fastbuild).
ARG OPT_LEVEL=opt
# Compiler to use.
ARG CC=clang


# Install bazel and needed deps.
RUN apt update && \
DEBIAN_FRONTEND=noninteractive apt install --no-install-recommends -y \
# Builder that contains the OS dependencies.
FROM ubuntu:${OS_VERSION} AS builder-deps
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
npm \
git \
pkg-config \
libssl-dev \
clang \
python3 && \
npm install -g @bazel/bazelisk
npm install -g @bazel/bazelisk@1.12.1
SHELL [ "/bin/bash", "-euo", "pipefail", "-c" ]


# Builder that only contains the downloaded externals.
FROM builder-deps as builder-with-externals
WORKDIR /root/turbo-cache
ADD . .
ARG CC
# Only xfer the minimum needed to query external packages..
COPY [ "WORKSPACE", ".bazel*", "Cargo.*", ".rustfmt.toml", "BUILD", "./" ]
# Download external dependencies. This is to improve docker layer caching.
RUN bazel fetch //...

ARG CC=clang

# Compile `cas` binary.
RUN bazel build --compilation_mode=opt //cas && \
# Builder that only contains the files related to bazel, but none of the source code.
# This layer improves the cache hits of the following layer, since it'll bypass the next
# layer if only the code files changed.
FROM builder-with-externals as builder-only-bazel-and-build-files
WORKDIR /root/turbo-cache
COPY . .
RUN find . -type f -regextype egrep ! -regex '.*/(BUILD|WORKSPACE|\.bazel.*|Cargo\..*|\.rustfmt\.toml)' -delete && \
find . -type d -empty -delete


# Builder to get a list of externals and targets rarely changed that take a while to build
# and push them into a file. This action takes a few seconds, but most of the time the
# output file will not change between builds, which dramatically improves our layer cache
# hits.
FROM builder-with-externals AS builder-external-deps
WORKDIR /root/turbo-cache
ARG OPT_LEVEL
ARG CC
# Copy only the BUILD files and other bazel related files.
COPY --from=builder-only-bazel-and-build-files /root/turbo-cache/ .
# Special case for `//proto/... because we have a bit of generated code here that runs under
# a different platform (host platform). Since this folder rarely changes we build it as well
# at this cache layer.
RUN bazel cquery -c ${OPT_LEVEL} \
'filter("^@", deps(attr(testonly, 0, //...), 1))' \
--universe_scope=//... \
--implicit_deps=false \
--tool_deps=false \
| cut -d' ' -f1 \
| sort \
| uniq > /root/external-targets.txt && \
echo "//proto/..." >> /root/external-targets.txt


# Builder for our externals and targets rarely changed that take a while to build.
FROM builder-with-externals as builder-externals-built
WORKDIR /root/turbo-cache
ARG OPT_LEVEL
ARG CC
COPY proto/ proto/
COPY --from=builder-external-deps /root/external-targets.txt /root/external-targets.txt
RUN bazel build -c ${OPT_LEVEL} --target_pattern_file=/root/external-targets.txt


# Builder to do the remaining build. At this point most of our externals should be built.
FROM builder-externals-built as builder
ARG OPT_LEVEL
ARG CC
COPY . .
RUN bazel build -c ${OPT_LEVEL} //cas && \
cp ./bazel-bin/cas/cas /root/turbo-cache-bin


# Go back to a fresh ubuntu container and copy only the compiled binary.
FROM ubuntu:22.04
FROM ubuntu:${OS_VERSION}
COPY --from=builder /root/turbo-cache-bin /usr/local/bin/turbo-cache

# Install runtime packages.
RUN apt update && \
DEBIAN_FRONTEND=noninteractive apt install --no-install-recommends -y \
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
libssl-dev

RUN mkdir -p /root/.cache/turbo-cache

EXPOSE 50051/tcp 50052/tcp

CMD ["turbo-cache"]
4 changes: 2 additions & 2 deletions run_integration_tests.sh
Expand Up @@ -41,8 +41,8 @@ EOT
esac
done

if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root due to docker permission issues"
if ! docker --version; then
echo "This script must be run as root due to docker permission issues (try with 'sudo')"
exit 1
fi

Expand Down

0 comments on commit 0801e26

Please sign in to comment.