[FIX] fix dockerfile build timeout bug

This commit is contained in:
qingguo 2018-03-22 16:32:07 +08:00
parent a251ef8f7c
commit c8a246df5a
3 changed files with 22 additions and 5 deletions

View File

@ -221,7 +221,7 @@ func (i *SourceCodeBuildItem) buildImage() error {
buildOptions.NoCache = false
}
i.Logger.Info("开始构建镜像", map[string]string{"step": "builder-exector"})
err = sources.ImageBuild(i.DockerClient, i.RepoInfo.GetCodeBuildAbsPath(), buildOptions, i.Logger, 3)
err = sources.ImageBuild(i.DockerClient, i.RepoInfo.GetCodeBuildAbsPath(), buildOptions, i.Logger, 5)
if err != nil {
i.Logger.Error(fmt.Sprintf("构造镜像%s失败: %s", buildImageName, err.Error()), map[string]string{"step": "builder-exector", "status": "failure"})
logrus.Errorf("build image error: %s", err.Error())
@ -237,12 +237,13 @@ func (i *SourceCodeBuildItem) buildImage() error {
RegistryAuth: auth,
}
i.Logger.Info("镜像构建成功,开始推送镜像至仓库", map[string]string{"step": "builder-exector"})
err = sources.ImagePush(i.DockerClient, buildImageName, ipo, i.Logger, 2)
err = sources.ImagePush(i.DockerClient, buildImageName, ipo, i.Logger, 5)
if err != nil {
i.Logger.Error("推送镜像失败", map[string]string{"step": "builder-exector"})
logrus.Errorf("push image error: %s", err.Error())
return err
}
i.Logger.Info("镜像推送镜像至仓库成功", map[string]string{"step": "builder-exector"})
//更新应用的镜像名称
service, err := db.GetManager().TenantServiceDao().GetServiceByID(i.ServiceID)
if err != nil {

View File

@ -158,13 +158,21 @@ func ImagePush(dockerCli *client.Client, image string, opts types.ImagePushOptio
defer readcloser.Close()
r := bufio.NewReader(readcloser)
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if line, _, err := r.ReadLine(); err == nil {
if logger != nil {
//进度信息
logger.Debug(string(line), map[string]string{"step": "progress"})
}
} else {
break
if err.Error() == "EOF" {
return nil
}
return err
}
}
}
@ -214,6 +222,11 @@ func ImageBuild(dockerCli *client.Client, contextDir string, options types.Image
defer rc.Body.Close()
r := bufio.NewReader(rc.Body)
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
if line, _, err := r.ReadLine(); err == nil {
if len(line) > 0 {
message := strings.Replace(string(line), "\n", "", -1)
@ -228,7 +241,10 @@ func ImageBuild(dockerCli *client.Client, contextDir string, options types.Image
}
}
} else {
break
if err.Error() == "EOF" {
return nil
}
return err
}
}
}

View File

@ -45,7 +45,7 @@ func TestBuildImage(t *testing.T) {
Tags: []string{"goodrain.me/gr1e1a6c_goodrain-apps_mysql:20180307135753"},
Remove: true,
}
if err := ImageBuild(dc, "/tmp/gost/mysql/5.5", buildOptions, nil, 5); err != nil {
if err := ImageBuild(dc, "/Users/qingguo/tmp/nginx", buildOptions, nil, 1); err != nil {
t.Fatal(err)
}
}