- 在Dockerfile的构建步骤中,将相对变化较小的步骤提前。
- 在go build之前先copy go.mod和go.sum并下载依赖。
- 在drone.yml中,先用build_cache步骤,构建builder阶段并且push为some-image:builder
- 在build_cache和build_final步骤中,使用cache_from指定some-image:builder作为构建缓存
- 如果在build_final阶段存在一些耗时的安装依赖等步骤,可以追加some-image:latest构建缓存,否则可以忽略
- 不知为何,在k8s的drone上跑plugins/docker时,默认自动的4个oci label的步骤会特别耗时,如果没有特别需要,使用auto_label = false配置项以关闭该行为
Created
October 11, 2025 09:47
-
-
Save smiley-yoyo/360159add4aff5bd86b59c049b44ae57 to your computer and use it in GitHub Desktop.
加快drone ci中docker构建速度
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| kind: pipeline | |
| type: kubernetes | |
| name: some-pipeline | |
| steps: | |
| - name: build_cache | |
| image: plugins/docker | |
| settings: | |
| registry: some-registry.com | |
| repo: some-registry.com/some-project/some-image | |
| cache_from: | |
| - some-registry.com/some-project/some-image:builder | |
| tags: | |
| - builder | |
| target: builder | |
| auto_label: false | |
| username: | |
| from_secret: DOCKER_USERNAME | |
| password: | |
| from_secret: DOCKER_PASSWORD | |
| dockerfile: Dockerfile | |
| - name: build_final | |
| image: plugins/docker | |
| settings: | |
| registry: some-registry.com | |
| repo: some-registry.com/some-project/some-image | |
| cache_from: | |
| - some-registry.com/some-project/some-image:builder | |
| - some-registry.com/some-project/some-image:latest | |
| tags: | |
| - latest | |
| - ${DRONE_TAG:-0.0.0}-${DRONE_COMMIT_SHA:0:7} | |
| auto_label: false | |
| username: | |
| from_secret: DOCKER_USERNAME | |
| password: | |
| from_secret: DOCKER_PASSWORD | |
| dockerfile: Dockerfile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| FROM golang:1.24-alpine AS builder | |
| ENV GO111MODULE=on \ | |
| GOPROXY=https://goproxy.cn,direct \ | |
| GOPRIVATE=github.com/bytemind-io | |
| WORKDIR /app | |
| RUN apk add git openssh | |
| COPY go.* ./ | |
| RUN go mod download | |
| COPY . . | |
| RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o some-app ./main.go | |
| FROM alpine:latest | |
| WORKDIR /app | |
| RUN apk add some-dependency | |
| COPY --from=builder /app/some-app . | |
| ENTRYPOINT ["/app/some-app"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment