From c3c5296cdba356ef8661fe072d34811fc0946337 Mon Sep 17 00:00:00 2001 From: Eden Reich Date: Wed, 19 Feb 2025 20:34:36 +0000 Subject: [PATCH] ci: Refactor Kaniko build step and Dockerfile for improved environment variable usage and caching Signed-off-by: Eden Reich --- .github/workflows/release.yml | 13 +++++++++---- Dockerfile | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a739902..91bf88a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -251,17 +251,22 @@ jobs: EOF - name: Build and push + env: + REPOSITORY_NAME: coder + REPOSITORY_OWNER: ${{ github.repository_owner }} + CONTAINER_REGISTRY: ghcr.io + VERSION: ${{ needs.github_release.outputs.new_release_version }} run: | /kaniko/executor \ --context="${{ github.repositoryUrl }}#${{ github.ref }}" \ --dockerfile=Dockerfile \ --target=minimal \ - --destination=ghcr.io/${{ github.repository_owner }}/coder:latest \ - --destination=ghcr.io/${{ github.repository_owner }}/coder:minimal \ - --destination=ghcr.io/${{ github.repository_owner }}/coder:minimal-${{ needs.github_release.outputs.new_release_version }} \ + --destination=${{ env.CONTAINER_REGISTRY }}/${{ env.REPOSITORY_OWNER }}/{{ env.REPOSITORY_NAME }}:latest \ + --destination=${{ env.CONTAINER_REGISTRY }}/${{ env.REPOSITORY_OWNER }}/{{ env.REPOSITORY_NAME }}:minimal \ + --destination=${{ env.CONTAINER_REGISTRY }}/${{ env.REPOSITORY_OWNER }}/{{ env.REPOSITORY_NAME }}:minimal-${{ env.VERSION }} \ --build-arg=TARGET_ARCH=${{ matrix.target }} \ --cache=true \ - --cache-repo=ghcr.io/${{ github.repository_owner }}/coder/cache + --cache-repo=${{ env.CONTAINER_REGISTRY }}/${{ env.REPOSITORY_OWNER }}/{{ env.REPOSITORY_NAME }}/cache # build_containers_with_tools: # name: B&P Language specific Containers diff --git a/Dockerfile b/Dockerfile index 7cfd757..7fefcd7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,12 +26,22 @@ RUN apk add --no-cache \ /tmp/* \ /var/tmp/* +# Setup rust target WORKDIR /app +RUN rustup target add ${TARGET_ARCH} +# Cache dependencies COPY Cargo.toml Cargo.lock ./ -COPY . . +COPY src ./src +RUN cargo build --release --no-default-features --target ${TARGET_ARCH} && \ + rm -rf target/${TARGET_ARCH}/release/.fingerprint/coder-* \ + target/${TARGET_ARCH}/release/deps/coder-* \ + target/${TARGET_ARCH}/release/coder* -RUN rustup target add ${TARGET_ARCH} && \ +# Build the actual binary +COPY . . +RUN --mount=type=cache,target=/root/.cargo/registry \ + --mount=type=cache,target=/root/.cargo/git \ cargo build --release --no-default-features --target ${TARGET_ARCH} FROM alpine:3.21.3 AS common