Skip to content

Commit 3f4a99b

Browse files
authored
chore(internal/librariangen): optimize Docker build for caching (#12699)
* Optimize the builder stage to cache Go module dependencies separately. * Enable remote layer caching in cloudbuild.yaml and cloudbuild-exitgate.yaml by using the --cache-from flag. * Correct the docker tag command in cloudbuild-exitgate.yaml to properly tag the latest image for the cache.
1 parent 5c507d4 commit 3f4a99b

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

internal/librariangen/Dockerfile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,18 @@ RUN wget https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz -O go.tar.gz && \
3737
tar -C /usr/local -xzf go.tar.gz && \
3838
rm go.tar.gz
3939
ENV PATH /usr/local/go/bin:$PATH
40+
RUN go version
4041

4142
WORKDIR /src
4243

44+
# Copy go.mod and go.sum to download dependencies before copying all the source.
45+
# This allows Docker to cache the dependencies layer.
46+
COPY go.mod go.sum ./
47+
RUN go mod download
48+
4349
# Copy all source code.
4450
COPY . .
4551

46-
RUN go version
47-
4852
# Build the librariangen binary.
4953
RUN CGO_ENABLED=0 GOOS=linux go build -v -o /librariangen .
5054

@@ -55,6 +59,7 @@ RUN CGO_ENABLED=0 GOOS=linux go build -v -o /librariangen .
5559
FROM marketplace.gcr.io/google/debian12:latest
5660

5761
# Set environment variables for tool versions for easy updates.
62+
ENV GO_VERSION=1.23.0
5863
ENV PROTOC_VERSION=25.7
5964
ENV GO_PROTOC_PLUGIN_VERSION=1.35.2
6065
ENV GO_GRPC_PLUGIN_VERSION=1.3.0
@@ -77,6 +82,7 @@ RUN apt-get update && \
7782
rm -rf /var/lib/apt/lists/*
7883

7984
# Install the specific Go version required for compatibility.
85+
# Note: This is needed for the go install commands below.
8086
RUN wget https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz -O go.tar.gz && \
8187
tar -C /usr/local -xzf go.tar.gz && \
8288
rm go.tar.gz

internal/librariangen/cloudbuild-exitgate.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,19 @@
1717

1818
timeout: 7200s # 2 hours
1919
steps:
20+
# Attempt to pull the latest image to use as a cache source.
21+
- name: gcr.io/cloud-builders/docker
22+
entrypoint: 'bash'
23+
args:
24+
- -c
25+
- |
26+
docker pull us-central1-docker.pkg.dev/cloud-sdk-production-pipeline/images-dev/librarian-go:infrastructure-public-image-latest || exit 0
2027
- name: gcr.io/cloud-builders/docker
2128
args:
2229
[
2330
"build",
31+
"--cache-from",
32+
"us-central1-docker.pkg.dev/cloud-sdk-production-pipeline/images-dev/librarian-go:infrastructure-public-image-latest",
2433
"-t",
2534
"us-central1-docker.pkg.dev/cloud-sdk-production-pipeline/images-dev/librarian-go:infrastructure-public-image-$COMMIT_SHA",
2635
"-f",
@@ -29,14 +38,17 @@ steps:
2938
]
3039
dir: internal/librariangen
3140
- name: gcr.io/cloud-builders/docker
32-
args:
41+
args:
3342
[
3443
"tag",
3544
"us-central1-docker.pkg.dev/cloud-sdk-production-pipeline/images-dev/librarian-go:infrastructure-public-image-$COMMIT_SHA",
45+
"us-central1-docker.pkg.dev/cloud-sdk-production-pipeline/images-dev/librarian-go:infrastructure-public-image-latest",
3646
]
3747
options:
3848
machineType: 'E2_HIGHCPU_8'
3949
requestedVerifyOption: VERIFIED # For provenance attestation generation
4050

4151
images:
4252
- us-central1-docker.pkg.dev/cloud-sdk-production-pipeline/images-dev/librarian-go:infrastructure-public-image-$COMMIT_SHA
53+
- us-central1-docker.pkg.dev/cloud-sdk-production-pipeline/images-dev/librarian-go:infrastructure-public-image-latest
54+

internal/librariangen/cloudbuild.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,19 @@
1717

1818
timeout: 7200s # 2 hours
1919
steps:
20+
# Attempt to pull the latest image to use as a cache source.
21+
- name: gcr.io/cloud-builders/docker
22+
entrypoint: 'bash'
23+
args:
24+
- -c
25+
- |
26+
docker pull gcr.io/cloud-devrel-public-resources/librarian-go:infrastructure-public-image-latest || exit 0
2027
- name: gcr.io/cloud-builders/docker
2128
args:
2229
[
2330
"build",
31+
"--cache-from",
32+
"gcr.io/cloud-devrel-public-resources/librarian-go:infrastructure-public-image-latest",
2433
"-t",
2534
"gcr.io/cloud-devrel-public-resources/librarian-go:infrastructure-public-image-$COMMIT_SHA",
2635
"-f",
@@ -29,7 +38,7 @@ steps:
2938
]
3039
dir: internal/librariangen
3140
- name: gcr.io/cloud-builders/docker
32-
args:
41+
args:
3342
[
3443
"tag",
3544
"gcr.io/cloud-devrel-public-resources/librarian-go:infrastructure-public-image-$COMMIT_SHA",
@@ -41,3 +50,4 @@ options:
4150
images:
4251
- gcr.io/cloud-devrel-public-resources/librarian-go:infrastructure-public-image-$COMMIT_SHA
4352
- gcr.io/cloud-devrel-public-resources/librarian-go:infrastructure-public-image-latest
53+

0 commit comments

Comments
 (0)