Skip to content

Commit

Permalink
build: reduce container image size
Browse files Browse the repository at this point in the history
Install only executables and needed libraries for:

- Dart
- Rust linters: rustfmt, cargo-clippy, and cargo-fmt
  • Loading branch information
ferrarimarco committed May 10, 2024
1 parent e0c8376 commit 5a386dd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -112,6 +112,10 @@ jobs:
run: |
docker load <"/tmp/${{ env.CONTAINER_IMAGE_OUTPUT_IMAGE_NAME }}.tar"
- name: Print environment info
run: |
make info
- name: Test Local Action (debug log)
uses: ./
env:
Expand Down
53 changes: 46 additions & 7 deletions Dockerfile
Expand Up @@ -29,6 +29,12 @@ FROM dart:3.3.4-sdk as dart
FROM mcr.microsoft.com/dotnet/sdk:8.0.204-alpine3.19 as dotnet-sdk
FROM mcr.microsoft.com/powershell:7.4-alpine-3.17 as powershell

FROM rust:1.78.0-alpine3.19 as rust

RUN apk add --no-cache \
rust-clippy \
rustfmt

FROM python:3.12.3-alpine3.19 as clang-format

RUN apk add --no-cache \
Expand Down Expand Up @@ -353,9 +359,35 @@ COPY --from=clj-kondo /bin/clj-kondo /usr/bin/
####################
# Install dart-sdk #
####################
# These COPY directives may be compacted after --parents is supported
ENV DART_SDK /usr/lib/dart
COPY --from=dart "${DART_SDK}" "${DART_SDK}"
RUN chmod 755 "${DART_SDK}" && chmod 755 "${DART_SDK}/bin"
COPY --from=dart --chmod=0755 \
"${DART_SDK}/version" \
"${DART_SDK}"/
COPY --from=dart --chmod=0755 \
"${DART_SDK}/bin/dart" \
"${DART_SDK}/bin/dart.sym" \
"${DART_SDK}/bin"/
COPY --from=dart --chmod=0755 \
"${DART_SDK}/bin/snapshots/analysis_server.dart.snapshot" \
"${DART_SDK}/bin/snapshots/dartdev.dart.snapshot" \
"${DART_SDK}/bin/snapshots/frontend_server_aot.dart.snapshot" \
"${DART_SDK}/bin/snapshots"/
COPY --from=dart --chmod=0755 \
"${DART_SDK}/lib/_internal" \
"${DART_SDK}/lib/_internal"
COPY --from=dart --chmod=0755 \
"${DART_SDK}/lib/async" \
"${DART_SDK}/lib/async"
COPY --from=dart --chmod=0755 \
"${DART_SDK}/lib/convert" \
"${DART_SDK}/lib/convert"
COPY --from=dart --chmod=0755 \
"${DART_SDK}/lib/core" \
"${DART_SDK}/lib/core"
COPY --from=dart --chmod=0755 \
"${DART_SDK}/lib/io" \
"${DART_SDK}/lib/io"

########################
# Install clang-format #
Expand Down Expand Up @@ -448,11 +480,6 @@ ARG TARGETARCH
ENV ARM_TTK_PSD1="/usr/lib/microsoft/arm-ttk/arm-ttk.psd1"
ENV PATH="${PATH}:/var/cache/dotnet/tools:/usr/share/dotnet"

# Install super-linter runtime dependencies
RUN apk add --no-cache \
rust-clippy \
rustfmt

COPY scripts/clippy.sh /usr/bin/clippy
RUN chmod +x /usr/bin/clippy

Expand Down Expand Up @@ -489,6 +516,18 @@ RUN PS_INSTALL_FOLDER="$(cat /tmp/PS_INSTALL_FOLDER)" \
COPY scripts/install-arm-ttk.sh /
RUN --mount=type=secret,id=GITHUB_TOKEN /install-arm-ttk.sh && rm -rf /install-arm-ttk.sh

# Install Rust linters
COPY --from=rust \
/usr/bin/cargo-clippy \
/usr/bin/cargo-fmt \
/usr/bin/rustfmt \
/usr/bin/
COPY --from=rust \
/usr/lib/libLLVM-17.so \
/usr/lib/librustc_driver-56e2d9f25bcc525c.so \
/usr/lib/libstd-01072ff1a54ef56b.so \
/usr/lib/

# Run to build version file and validate image again because we installed more linters
ENV IMAGE="standard"
COPY scripts/linterVersions.sh /
Expand Down
24 changes: 15 additions & 9 deletions Makefile
Expand Up @@ -14,14 +14,6 @@ ifeq ($(INTERACTIVE), 1)
DOCKER_FLAGS += -t
endif

.PHONY: info
info: ## Gather information about the runtime environment
echo "whoami: $$(whoami)"; \
echo "pwd: $$(pwd)"; \
echo "ls -ahl: $$(ls -ahl)"; \
docker images; \
docker ps

.PHONY: help
help: ## Show help
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Expand Down Expand Up @@ -79,13 +71,27 @@ endif

DEV_CONTAINER_URL := "super-linter/dev-container:latest"


ifeq ($(GITHUB_HEAD_REF),)
RELEASE_PLEASE_TARGET_BRANCH := "$(shell git branch --show-current)"
else
RELEASE_PLEASE_TARGET_BRANCH := "${GITHUB_HEAD_REF}"
endif

.PHONY: info
info: ## Gather information about the runtime environment
echo "whoami: $$(whoami)"; \
echo "pwd: $$(pwd)"; \
echo "ls -ahl: $$(ls -ahl)"; \
docker images; \
docker ps; \
echo "Container image layers size:"; \
docker history \
--human \
--no-trunc \
--format "{{.Size}} {{.CreatedSince}} {{.CreatedBy}}" \
$(SUPER_LINTER_TEST_CONTAINER_URL) \
| sort --human

.PHONY: check-github-token
check-github-token:
@if [ ! -f "${GITHUB_TOKEN_PATH}" ]; then echo "Cannot find the file to load the GitHub access token: $(GITHUB_TOKEN_PATH). Create a readable file there, and populate it with a GitHub personal access token."; exit 1; fi
Expand Down

0 comments on commit 5a386dd

Please sign in to comment.