소스 검색

optimize docker file generation, make docker build faster (#244)

* simplify code, format makefile

* simplify code

* some optimize by kevwan and benying (#240)

Co-authored-by: 杨志泉 <zhiquan.yang@yiducloud.cn>

* optimization (#241)

* optimize docker file generation, make docker build faster

Co-authored-by: benying <31179034+benyingY@users.noreply.github.com>
Co-authored-by: 杨志泉 <zhiquan.yang@yiducloud.cn>
Co-authored-by: bittoy <bittoy@qq.com>
Kevin Wan 4 년 전
부모
커밋
f997aee3ba
3개의 변경된 파일30개의 추가작업 그리고 14개의 파일을 삭제
  1. 2 1
      tools/goctl/api/gogen/genhandlers.go
  2. 19 7
      tools/goctl/docker/docker.go
  3. 9 6
      tools/goctl/docker/template.go

+ 2 - 1
tools/goctl/api/gogen/genhandlers.go

@@ -75,7 +75,8 @@ func genHandler(dir string, cfg *config.Config, group spec.Group, route spec.Rou
 	})
 	})
 }
 }
 
 
-func doGenToFile(dir, handler string, cfg *config.Config, group spec.Group, route spec.Route, handleObj Handler) error {
+func doGenToFile(dir, handler string, cfg *config.Config, group spec.Group,
+	route spec.Route, handleObj Handler) error {
 	filename, err := format.FileNamingFormat(cfg.NamingFormat, handler)
 	filename, err := format.FileNamingFormat(cfg.NamingFormat, handler)
 	if err != nil {
 	if err != nil {
 		return err
 		return err

+ 19 - 7
tools/goctl/docker/docker.go

@@ -6,6 +6,7 @@ import (
 	"path/filepath"
 	"path/filepath"
 	"strings"
 	"strings"
 	"text/template"
 	"text/template"
+	"time"
 
 
 	"github.com/tal-tech/go-zero/tools/goctl/util"
 	"github.com/tal-tech/go-zero/tools/goctl/util"
 	ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
 	ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
@@ -13,10 +14,19 @@ import (
 )
 )
 
 
 const (
 const (
-	etcDir  = "etc"
-	yamlEtx = ".yaml"
+	etcDir    = "etc"
+	yamlEtx   = ".yaml"
+	cstOffset = 60 * 60 * 8 // 8 hours offset for Chinese Standard Time
 )
 )
 
 
+type Docker struct {
+	Chinese   bool
+	GoRelPath string
+	GoFile    string
+	ExeFile   string
+	Argument  string
+}
+
 func DockerCommand(c *cli.Context) error {
 func DockerCommand(c *cli.Context) error {
 	goFile := c.String("go")
 	goFile := c.String("go")
 	if len(goFile) == 0 {
 	if len(goFile) == 0 {
@@ -87,12 +97,14 @@ func generateDockerfile(goFile string, args ...string) error {
 		builder.WriteString(`, "` + arg + `"`)
 		builder.WriteString(`, "` + arg + `"`)
 	}
 	}
 
 
+	_, offset := time.Now().Zone()
 	t := template.Must(template.New("dockerfile").Parse(text))
 	t := template.Must(template.New("dockerfile").Parse(text))
-	return t.Execute(out, map[string]string{
-		"goRelPath": projPath,
-		"goFile":    goFile,
-		"exeFile":   util.FileNameWithoutExt(filepath.Base(goFile)),
-		"argument":  builder.String(),
+	return t.Execute(out, Docker{
+		Chinese:   offset == cstOffset,
+		GoRelPath: projPath,
+		GoFile:    goFile,
+		ExeFile:   util.FileNameWithoutExt(filepath.Base(goFile)),
+		Argument:  builder.String(),
 	})
 	})
 }
 }
 
 

+ 9 - 6
tools/goctl/docker/template.go

@@ -14,13 +14,16 @@ LABEL stage=gobuilder
 
 
 ENV CGO_ENABLED 0
 ENV CGO_ENABLED 0
 ENV GOOS linux
 ENV GOOS linux
-ENV GOPROXY https://goproxy.cn,direct
+{{if .Chinese}}ENV GOPROXY https://goproxy.cn,direct{{end}}
 
 
 WORKDIR /build/zero
 WORKDIR /build/zero
+
+ADD go.mod .
+ADD go.sum .
+RUN go mod download
 COPY . .
 COPY . .
-RUN sh -c "[ -f go.mod ]" || exit
-COPY {{.goRelPath}}/etc /app/etc
-RUN go build -ldflags="-s -w" -o /app/{{.exeFile}} {{.goRelPath}}/{{.goFile}}
+COPY {{.GoRelPath}}/etc /app/etc
+RUN go build -ldflags="-s -w" -o /app/{{.ExeFile}} {{.GoRelPath}}/{{.GoFile}}
 
 
 
 
 FROM alpine
 FROM alpine
@@ -31,10 +34,10 @@ RUN apk add --no-cache tzdata
 ENV TZ Asia/Shanghai
 ENV TZ Asia/Shanghai
 
 
 WORKDIR /app
 WORKDIR /app
-COPY --from=builder /app/{{.exeFile}} /app/{{.exeFile}}
+COPY --from=builder /app/{{.ExeFile}} /app/{{.ExeFile}}
 COPY --from=builder /app/etc /app/etc
 COPY --from=builder /app/etc /app/etc
 
 
-CMD ["./{{.exeFile}}"{{.argument}}]
+CMD ["./{{.ExeFile}}"{{.Argument}}]
 `
 `
 )
 )