Skip to content

Commit e06c92c

Browse files
authored
feat: support image cache when build (#2400)
1 parent 64ac098 commit e06c92c

4 files changed

Lines changed: 27 additions & 2 deletions

File tree

smart-app-builder/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ docker run -it --rm --privileged \
3333
-e SOURCE_GET_URL=file:///podman/source \
3434
-e DEST_PUT_URL=file:///podman/dest \
3535
-e BUILDER_SHIM_IMAGE=bk-builder-heroku-bionic:v1.0.2 \
36+
-e CACHE_REGISTRY='mirrors.example.com/foo' \
37+
-e REGISTRY_AUTH='{"mirrors.example.com": "Basic xxx"}' \
3638
-v $APP_PATH:/podman/source \
3739
-v $SMART_PATH:/podman/dest \
3840
smart-app-builder:pind
@@ -49,6 +51,8 @@ docker run -it --rm --privileged \
4951
- `DEST_PUT_URL`: S-Mart 包的生成路径。同样地,`podman` 用户需要对目录具有读写权限(如示例中的 `/podman/dest`)。
5052
- `BUILDER_SHIM_IMAGE`: CNB
5153
构建工具镜像。镜像的制作方法可以参考 [cnb-builder-shim]([blueking-paas/cnb-builder-shim/README.md at builder-stack · TencentBlueKing/blueking-paas · GitHub](https://github.com/TencentBlueKing/blueking-paas/blob/builder-stack/cnb-builder-shim/README.md))。
54+
- `CACHE_REGISTRY`: (可选) 用于缓存的镜像仓库。设置后, 构建过程会启用缓存功能, 否则不启用。
55+
- `REGISTRY_AUTH`: (可选) 镜像仓库凭证。如果镜像仓库需要鉴权, 则必须设置此参数。
5256

5357
#### 2.2 基于 DIND 构建
5458

@@ -70,6 +74,8 @@ docker run -it --rm --privileged \
7074
-e SOURCE_GET_URL=file:///tmp/source \
7175
-e DEST_PUT_URL=file:///tmp/dest \
7276
-e BUILDER_SHIM_IMAGE=bk-builder-heroku-bionic:v1.0.2 \
77+
-e CACHE_REGISTRY='mirrors.example.com/foo' \
78+
-e REGISTRY_AUTH='{"mirrors.example.com": "Basic xxx"}' \
7379
-v $APP_PATH:/tmp/source \
7480
-v $SMART_PATH:/tmp/dest \
7581
smart-app-builder:dind
@@ -84,6 +90,8 @@ docker run -it --rm --privileged \
8490
- `DEST_PUT_URL`: S-Mart 包的生成路径
8591
- `BUILDER_SHIM_IMAGE`: CNB
8692
构建工具镜像。镜像的制作方法可以参考 [cnb-builder-shim]([blueking-paas/cnb-builder-shim/README.md at builder-stack · TencentBlueKing/blueking-paas · GitHub](https://github.com/TencentBlueKing/blueking-paas/blob/builder-stack/cnb-builder-shim/README.md))。
93+
- `CACHE_REGISTRY`: (可选) 用于缓存的镜像仓库。设置后, 构建过程会启用缓存功能, 否则不启用。
94+
- `REGISTRY_AUTH`: (可选) 镜像仓库凭证。如果镜像仓库需要鉴权, 则必须设置此参数。
8795

8896
#### 2.3 PIND vs DIND
8997

smart-app-builder/pkg/builder/executor/container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (c *containerExecutor) runBuilder(buildPlan *plan.BuildPlan, runImage strin
133133
}
134134

135135
// 2. 生成运行命令参数
136-
args := makeRunArgs(group, moduleSrcTGZ, runImage)
136+
args := makeRunArgs(buildPlan.AppCode, group, moduleSrcTGZ, runImage)
137137
c.logger.Info("build", "module name", group.BuildModuleName, "run args", args)
138138

139139
// 3. 运行构建命令, 并打印输出

smart-app-builder/pkg/builder/executor/utils.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func archiveArtifactTarball(buildPlan *plan.BuildPlan, artifactDir string) (stri
9090
}
9191

9292
// makeRunArgs 生成运行命令, 其中 moduleSrcTGZ 为模块源码包, runImage 为运行时镜像
93-
func makeRunArgs(group *plan.ModuleBuildGroup, moduleSrcTGZ string, runImage string) []string {
93+
func makeRunArgs(appCode string, group *plan.ModuleBuildGroup, moduleSrcTGZ string, runImage string) []string {
9494
args := make([]string, 0)
9595

9696
envSlice := lo.MapToSlice(group.Envs, func(k string, v string) string {
@@ -118,6 +118,15 @@ func makeRunArgs(group *plan.ModuleBuildGroup, moduleSrcTGZ string, runImage str
118118
args = append(args, "-e", env)
119119
}
120120

121+
// 添加缓存配置
122+
if config.G.CacheRegistry != "" {
123+
cacheImage := fmt.Sprintf("%s/%s/%s:cnb-build-cache", config.G.CacheRegistry, appCode, group.BuildModuleName)
124+
args = append(args, "-e", fmt.Sprintf("CACHE_IMAGE=%s", cacheImage))
125+
if config.G.RegistryAuth != "" {
126+
args = append(args, "-e", fmt.Sprintf("CNB_REGISTRY_AUTH=%s", config.G.RegistryAuth))
127+
}
128+
}
129+
121130
// 挂载源码压缩包
122131
args = append(args, "--mount", fmt.Sprintf("type=bind,source=%s,target=%s", moduleSrcTGZ, bindTarget))
123132

smart-app-builder/pkg/config/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ var G = struct {
4848

4949
// BuildpackType is the type of buildpack to use. Supported values: oci-embedded and tgz, default is oci-embedded
5050
BuildpackType string
51+
52+
// CacheRegistry is a registry used to cache image layers. e.g., mirrors.tencent.com/bkapps
53+
CacheRegistry string
54+
// RegistryAuth is the cache registry auth. e.g., '{"mirrors.tencent.com": "Basic xxx"}'
55+
RegistryAuth string
5156
}{Viper: viper.New()}
5257

5358
// SetGlobalConfig set global config
@@ -95,4 +100,7 @@ func SetGlobalConfig() {
95100

96101
// set bk-buildpack-apt env
97102
G.SetDefault("APT_BUILDPACK_VERSION", "v2")
103+
104+
G.CacheRegistry = G.GetString("CACHE_REGISTRY")
105+
G.RegistryAuth = G.GetString("REGISTRY_AUTH")
98106
}

0 commit comments

Comments
 (0)