logo
2
10
WeChat Login

【攻擂问题示例】github 可以支持打包异构镜像,迁移到 cnb 上我应该如何实现对应的功能#3

Resolved
created 2025-08-04
Edit

我们目前在 Github 依赖 GitHubCI 的能力,在对异构的Docker镜像进行打包,例如 LoongArch64。

有计划将 Github 的 CI 迁移到CNB,我应该如何在 CNB 平台实现这个功能呢。

2
1
Creator

【守擂团解题示例】

CNB 使用 docker buildx 来支持打包异构镜像。

目前通过流水线中指定 runner.tags: cnb:arch:amd64:containerd-snapshotter,可以将构建指定到支持containerd-snapshotter特性的机器(ps. 未来这个标签会去掉,仅供测试,请勿在正式环境中使用)

1,可以在 .cnb.yml 中来配置 docker buildx 镜像构建流水线,比如每次有代码提交的时候触发镜像构建

  push:
    - services:
        - docker
      runner:
        tags: cnb:arch:amd64:containerd-snapshotter
      stages:
        - name: docker build
          script: docker buildx build -t  ${CNB_DOCKER_REGISTRY}/${CNB_REPO_SLUG_LOWERCASE}:latest --platform linux/amd64,linux/amd64/v2,linux/amd64/v3,linux/arm64,linux/riscv64,linux/ppc64le,linux/s390x,linux/386,linux/mips64le,linux/mips64,linux/loong64,linux/arm/v7,linux/arm/v6 --push .
        - name: docker push
          script: docker push ${CNB_DOCKER_REGISTRY}/${CNB_REPO_SLUG_LOWERCASE}:latest

2, 修改你的 Dockerfile,在使用到其他镜像的语句中添加 --platform=$BUILDPLATFORM, 确保构建过程中使用的工具链是与目标平台一致的,从而避免后续构建出的二进制文件与目标平台不兼容的问题,比如

FROM --platform=$BUILDPLATFORM golang:1.23-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /app/git-cnb

# 运行阶段
FROM --platform=$BUILDPLATFORM alpine:latest
RUN apk --no-cache add ca-certificates
RUN apk add --no-cache git
# Sets the working directory inside the container to /app for subsequent commands
WORKDIR /app
COPY --from=builder /app/git-cnb .
ENV PATH="/app:${PATH}"

3, 这样, 你就可以在 CNB 来完成任意平台的镜像构建了,打包后的镜像可以在仓库的制品 tab 中查看,比如
948ec4a5-3686-428f-9ee3-719b3695e456.png

完整示例可参考 looc/git-cnb@10effa25

2
1
pinned this issue
added labels
优质问题

以下内容由 AI 中译中生成
原文为
666
译文为
这个问题我也在考虑,想了解下从GitHub CI迁移到CNB具体该怎么操作?

removed labels
优质问题
Resolved ISSUE
Assignee
None yet
Label
None yet
Priority
None yet
Time period
-
Property
Add custom properties to record and label key information
Participant