You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在GitHub Runner的Docker容器中持久化Go模块与构建缓存

可行解决方案

方案1:非临时自托管运行器+持久化卷挂载(最稳定)

你的环境支持非临时运行器,直接给运行器绑定Kubernetes持久化卷(PVC),将Go模块与构建缓存目录挂载到卷上,再通过Docker绑定挂载复用这些缓存:

  1. 配置ARC运行器的PVC挂载
    在ARC的RunnerDeployment配置中添加卷挂载,把PVC挂载到运行器的/go/cache和/go/mod目录:
apiVersion: actions.summerwind.dev/v1alpha1
kind: RunnerDeployment
metadata:
  name: go-build-runner
spec:
  replicas: 1
  template:
    spec:
      containers:
      - name: runner
        volumeMounts:
        - name: go-cache
          mountPath: /go/cache
        - name: go-mod
          mountPath: /go/mod
      volumes:
      - name: go-cache
        persistentVolumeClaim:
          claimName: go-cache-pvc
      - name: go-mod
        persistentVolumeClaim:
          claimName: go-mod-pvc
  1. 修改Dockerfile并配置缓存路径
    调整Dockerfile,指定Go缓存的环境变量,同时调整文件复制顺序减少缓存失效:
FROM golang:1.21-alpine as builder
ENV GOMODCACHE=/go/mod
ENV GOCACHE=/go/cache
RUN mkdir -p /code
WORKDIR /code
# 先复制依赖文件,避免代码变更触发依赖重下
COPY go.mod go.sum ./
RUN go mod download -x
# 再复制剩余代码
COPY . .
RUN go build -o output/result -v ./mycode

FROM alpine:latest
COPY --from=builder /code/output/result /myprogram
CMD ["myprogram"]
  1. GitHub Action构建步骤配置
    在构建命令中添加绑定挂载参数,将运行器上的持久化缓存目录挂载到容器内:
- name: Build Docker image
  run: |
    docker build \
      -v /go/mod:/go/mod \
      -v /go/cache:/go/cache \
      -t my-test-image .

非临时运行器的构建会持久化保留缓存,跨Action运行直接复用。

方案2:临时运行器+GitHub Actions缓存(兼容临时实例)

如果必须使用临时运行器,利用GHA的actions/cache在每次运行间备份/恢复Go缓存,再挂载到Docker容器:

  1. Workflow中添加缓存管理步骤
    构建前恢复缓存,构建完成后保存缓存:
- name: Prepare Go cache directories
  run: |
    mkdir -p $HOME/go/mod $HOME/go/cache
    echo "GOMODCACHE=$HOME/go/mod" >> $GITHUB_ENV
    echo "GOCACHE=$HOME/go/cache" >> $GITHUB_ENV

- name: Restore Go cache
  uses: actions/cache@v3
  with:
    path: |
      $HOME/go/mod
      $HOME/go/cache
    key: go-cache-${{ hashFiles('go.sum') }}
    restore-keys: |
      go-cache-

- name: Build Docker image
  run: |
    docker build \
      -v $GOMODCACHE:/go/mod \
      -v $GOCACHE:/go/cache \
      -t my-test-image .
  1. Dockerfile调整同方案1
    同样设置GOMODCACHE和GOCACHE环境变量,先复制go.mod/go.sum再复制代码,降低缓存失效概率。

方案3:优化BuildKit缓存+依赖层分离(辅助提速)

结合BuildKit特性优化Dockerfile层缓存,即使无法跨BuildKit实例共享,也能减少同一实例内的重复构建工作:

  1. 启用BuildKit构建
    在Workflow中开启BuildKit:
- name: Build with BuildKit
  env:
    DOCKER_BUILDKIT: 1
  run: docker build -t my-test-image .
  1. 修改Dockerfile使用BuildKit缓存挂载
    通过RUN --mount=type=cache挂载BuildKit缓存到Go的缓存目录,同时分离依赖层:
FROM golang:1.21-alpine as builder
RUN mkdir -p /code
WORKDIR /code
# 先处理依赖文件
COPY go.mod go.sum ./
# 挂载BuildKit缓存到模块缓存目录
RUN --mount=type=cache,target=/go/pkg/mod \
    go mod download -x
# 复制代码并挂载构建缓存
COPY . .
RUN --mount=type=cache,target=/root/.cache/go-build \
    go build -o output/result -v ./mycode

FROM alpine:latest
COPY --from=builder /code/output/result /myprogram
CMD ["myprogram"]

该方案可减少同一BuildKit实例内的重复工作,配合方案2的GHA缓存可最大化提速。


内容的提问来源于stack exchange,提问作者Bogd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 15:57:40