From 62ab81441d7ab895d193e0a5db80c82b20cddc2d Mon Sep 17 00:00:00 2001 From: icechen Date: Thu, 30 Dec 2021 20:06:10 +0800 Subject: [PATCH] update env --- config_handler/api_config.go | 12 +++-- config_handler/config_handler.go | 26 ++++++++-- config_handler/service_config.go | 8 ++-- consts/env.go | 9 ++++ git/git.go | 3 +- go_handler/go_api_handler.go | 47 +++++-------------- .../go_api_production_pipeline.go | 37 +++++++++++++++ .../go_api_test_pipeline.go | 37 +++++++++++++++ .../go_service_production_pipeline.go | 37 +++++++++++++++ .../go_service_test_pipeline.go | 37 +++++++++++++++ go_handler/go_service_handler.go | 47 +++++-------------- 11 files changed, 217 insertions(+), 83 deletions(-) create mode 100644 consts/env.go create mode 100644 go_handler/go_handler_template/go_api_production_pipeline.go create mode 100644 go_handler/go_handler_template/go_api_test_pipeline.go create mode 100644 go_handler/go_handler_template/go_service_production_pipeline.go create mode 100644 go_handler/go_handler_template/go_service_test_pipeline.go diff --git a/config_handler/api_config.go b/config_handler/api_config.go index 9ba9344..b9455bf 100644 --- a/config_handler/api_config.go +++ b/config_handler/api_config.go @@ -1,11 +1,14 @@ package config_handler -import "git.icechen.cn/pkg/drone_plugin/go_handler" +import ( + "git.icechen.cn/pkg/drone_plugin/consts" + "git.icechen.cn/pkg/drone_plugin/go_handler" +) -func (al ApiList) toDestinationConfig(nameSpace string, serviceEnv map[string]string) (string, error) { +func (al ApiList) toDestinationConfig(nameSpace string, deployEnv consts.Env, serviceEnv map[string]string) (string, error) { retConfig := "" for _, api := range al { - config, err := api.toDestinationConfig(nameSpace, serviceEnv) + config, err := api.toDestinationConfig(nameSpace, deployEnv, serviceEnv) if err != nil { return "", err } @@ -14,7 +17,7 @@ func (al ApiList) toDestinationConfig(nameSpace string, serviceEnv map[string]st return retConfig, nil } -func (a Api) toDestinationConfig(nameSpace string, serviceEnv map[string]string) (string, error) { +func (a Api) toDestinationConfig(nameSpace string, deployEnv consts.Env, serviceEnv map[string]string) (string, error) { var handler Handler switch a.Type { case go_handler.TypeGolang: @@ -23,6 +26,7 @@ func (a Api) toDestinationConfig(nameSpace string, serviceEnv map[string]string) Name: a.Name, Root: a.Root, Port: a.Port, + DeployEnv: deployEnv, ServiceEnv: serviceEnv, } } diff --git a/config_handler/config_handler.go b/config_handler/config_handler.go index d998a28..3a3bce2 100644 --- a/config_handler/config_handler.go +++ b/config_handler/config_handler.go @@ -3,8 +3,11 @@ package config_handler import ( "context" "encoding/json" + "errors" "strings" + "git.icechen.cn/pkg/drone_plugin/consts" + "git.icechen.cn/pkg/drone_plugin/git" "github.com/drone/drone-go/drone" @@ -38,11 +41,16 @@ func (p *plugin) Find(ctx context.Context, req *config.Request) (*drone.Config, return nil, err } - // 2. 非 monorepo 的直接返回 + // 2. 是否部署 + // 2.1 非 monorepo 的直接返回 if cfg.Type != TypeMonorepo { // 返回 nil 按照 204 处理 return nil, nil } + // 2.2 无部署环境报错返回,阻断部署 + if getDeployEnv(req) == consts.EnvNone { + return nil, errors.New("ignore event") + } // 3. 获取提交文件树 modifiedFileList, err := getGitClient(req).GetCommitModifiedFileList() @@ -54,7 +62,7 @@ func (p *plugin) Find(ctx context.Context, req *config.Request) (*drone.Config, // 4. 根据文件树以及原始配置文件的信息,组装需要构建的服务的ci信息 // 4.1 api modifiedApiList := getModifiedApi(cfg.Api, modifiedFileList) - destinationApi, err := modifiedApiList.toDestinationConfig(cfg.Name, cfg.toServiceEnv()) + destinationApi, err := modifiedApiList.toDestinationConfig(cfg.Name, getDeployEnv(req), cfg.toServiceEnv()) if err != nil { logrus.Error(err) return nil, err @@ -62,7 +70,7 @@ func (p *plugin) Find(ctx context.Context, req *config.Request) (*drone.Config, // 4.2 service modifiedServiceList := getModifiedService(cfg.Service, modifiedFileList) - destinationService, err := modifiedServiceList.toDestinationConfig(cfg.Name, cfg.toServiceEnv()) + destinationService, err := modifiedServiceList.toDestinationConfig(cfg.Name, getDeployEnv(req), cfg.toServiceEnv()) if err != nil { logrus.Error(err) return nil, err @@ -85,6 +93,18 @@ func getGitClient(req *config.Request) git.Client { return git.New(req.Repo.Namespace, req.Repo.Name, req.Build.After, req.Repo.Config) } +func getDeployEnv(req *config.Request) consts.Env { + switch req.Build.Event { + case "push": + if req.Build.Target == "master" { + return consts.EnvTest + } + case "tag": + return consts.EnvProduction + } + return consts.EnvNone +} + func getModifiedApi(api ApiList, modifiedFileList []string) ApiList { ret := make(ApiList, 0) tempIndex := NewSet() diff --git a/config_handler/service_config.go b/config_handler/service_config.go index 379de54..fc055f4 100644 --- a/config_handler/service_config.go +++ b/config_handler/service_config.go @@ -1,13 +1,14 @@ package config_handler import ( + "git.icechen.cn/pkg/drone_plugin/consts" "git.icechen.cn/pkg/drone_plugin/go_handler" ) -func (sl ServiceList) toDestinationConfig(nameSpace string, serviceEnv map[string]string) (string, error) { +func (sl ServiceList) toDestinationConfig(nameSpace string, deployEnv consts.Env, serviceEnv map[string]string) (string, error) { retConfig := "" for _, service := range sl { - config, err := service.toDestinationConfig(nameSpace, serviceEnv) + config, err := service.toDestinationConfig(nameSpace, deployEnv, serviceEnv) if err != nil { return "", err } @@ -16,7 +17,7 @@ func (sl ServiceList) toDestinationConfig(nameSpace string, serviceEnv map[strin return retConfig, nil } -func (s Service) toDestinationConfig(nameSpace string, serviceEnv map[string]string) (string, error) { +func (s Service) toDestinationConfig(nameSpace string, deployEnv consts.Env, serviceEnv map[string]string) (string, error) { var handler Handler switch s.Type { case go_handler.TypeGolang: @@ -25,6 +26,7 @@ func (s Service) toDestinationConfig(nameSpace string, serviceEnv map[string]str Name: s.Name, Root: s.Root, Port: s.Port, + DeployEnv: deployEnv, ServiceEnv: serviceEnv, } } diff --git a/consts/env.go b/consts/env.go new file mode 100644 index 0000000..4bc3952 --- /dev/null +++ b/consts/env.go @@ -0,0 +1,9 @@ +package consts + +type Env uint + +const ( + EnvNone Env = 0 // 忽略 + EnvTest Env = 1 // 测试环境 + EnvProduction Env = 2 // 生产环境 +) diff --git a/git/git.go b/git/git.go index fe1454b..ffc0c2d 100644 --- a/git/git.go +++ b/git/git.go @@ -12,7 +12,8 @@ var cli *gitea.Client func init() { var err error - cli, err = gitea.NewClient("http://gitea:3000/", gitea.SetToken("4322b0d361004db5dcea1741417f9e014a0f428f")) + // cli, err = gitea.NewClient("http://gitea:3000/", gitea.SetToken("4322b0d361004db5dcea1741417f9e014a0f428f")) + cli, err = gitea.NewClient("https://git.icechen.cn", gitea.SetToken("4322b0d361004db5dcea1741417f9e014a0f428f")) if err != nil { panic(err) } diff --git a/go_handler/go_api_handler.go b/go_handler/go_api_handler.go index 46f012a..8f2f9a4 100644 --- a/go_handler/go_api_handler.go +++ b/go_handler/go_api_handler.go @@ -3,57 +3,32 @@ package go_handler import ( "bytes" "text/template" + + "git.icechen.cn/pkg/drone_plugin/go_handler/go_handler_template" + + "git.icechen.cn/pkg/drone_plugin/consts" ) const TypeGolang = "golang" -const apiPipeline = ` -kind: pipeline -type: docker -name: 部署{{ .Name }} - -steps: - - name: build - image: plugins/docker - volumes: - - name: docker - path: /var/run/docker.sock - settings: - username: - from_secret: reg_username - password: - from_secret: reg_password - repo: reg.icechen.cn/{{ .NameSpace }}/{{ .Name }} - registry: reg.icechen.cn - tags: ${DRONE_COMMIT:0:8} - dockerfile: ./app/api/{{ .Name }}/Dockerfile - - - name: run - image: docker - volumes: - - name: docker - path: /var/run/docker.sock - commands: - - docker rm -f {{ .NameSpace }}-{{ .Name }} - - docker run -d --name="{{ .NameSpace }}-{{ .Name }}" --network="nginx-net"{{ range $key, $value := .ServiceEnv }} --env {{ $key }}={{ $value }} {{ end }}reg.icechen.cn/{{ .NameSpace }}/{{ .Name }}:${DRONE_COMMIT:0:8} - -volumes: - - name: docker - host: - path: /var/run/docker.sock -` - type GoApiHandler struct { NameSpace string Name string Root string Port string ServiceEnv map[string]string + DeployEnv consts.Env } func (gah GoApiHandler) ToDestinationConfig() (string, error) { var err error t := template.New(gah.Name) + + apiPipeline := go_handler_template.ApiTestPipeline + if gah.DeployEnv == consts.EnvProduction { + apiPipeline = go_handler_template.ApiProductionPipeline + } + t, err = t.Parse(apiPipeline) if err != nil { return "", err diff --git a/go_handler/go_handler_template/go_api_production_pipeline.go b/go_handler/go_handler_template/go_api_production_pipeline.go new file mode 100644 index 0000000..d67c2bf --- /dev/null +++ b/go_handler/go_handler_template/go_api_production_pipeline.go @@ -0,0 +1,37 @@ +package go_handler_template + +const ApiProductionPipeline = ` +kind: pipeline +type: docker +name: 部署{{ .Name }} + +steps: + - name: build + image: plugins/docker + volumes: + - name: docker + path: /var/run/docker.sock + settings: + username: + from_secret: reg_username + password: + from_secret: reg_password + repo: reg.icechen.cn/{{ .NameSpace }}/{{ .Name }} + registry: reg.icechen.cn + tags: ${DRONE_COMMIT:0:8} + dockerfile: ./{{ .Root }}/Dockerfile + + - name: run + image: docker + volumes: + - name: docker + path: /var/run/docker.sock + commands: + - docker rm -f {{ .NameSpace }}-{{ .Name }} + - docker run -d --name="{{ .NameSpace }}-{{ .Name }}" --network="nginx-net"{{ range $key, $value := .ServiceEnv }} --env {{ $key }}={{ $value }} {{ end }}reg.icechen.cn/{{ .NameSpace }}/{{ .Name }}:${DRONE_COMMIT:0:8} + +volumes: + - name: docker + host: + path: /var/run/docker.sock +` diff --git a/go_handler/go_handler_template/go_api_test_pipeline.go b/go_handler/go_handler_template/go_api_test_pipeline.go new file mode 100644 index 0000000..b43e70d --- /dev/null +++ b/go_handler/go_handler_template/go_api_test_pipeline.go @@ -0,0 +1,37 @@ +package go_handler_template + +const ApiTestPipeline = ` +kind: pipeline +type: docker +name: 部署{{ .Name }} + +steps: + - name: build + image: plugins/docker + volumes: + - name: docker + path: /var/run/docker.sock + settings: + username: + from_secret: reg_username + password: + from_secret: reg_password + repo: reg.icechen.cn/{{ .NameSpace }}/{{ .Name }} + registry: reg.icechen.cn + tags: ${DRONE_COMMIT:0:8} + dockerfile: ./{{ .Root }}/Dockerfile + + - name: run + image: docker + volumes: + - name: docker + path: /var/run/docker.sock + commands: + - docker rm -f {{ .NameSpace }}-{{ .Name }} + - docker run -d --name="{{ .NameSpace }}-{{ .Name }}" --network="nginx-net"{{ range $key, $value := .ServiceEnv }} --env {{ $key }}={{ $value }} {{ end }}reg.icechen.cn/{{ .NameSpace }}/{{ .Name }}:${DRONE_COMMIT:0:8} + +volumes: + - name: docker + host: + path: /var/run/docker.sock +` diff --git a/go_handler/go_handler_template/go_service_production_pipeline.go b/go_handler/go_handler_template/go_service_production_pipeline.go new file mode 100644 index 0000000..87230db --- /dev/null +++ b/go_handler/go_handler_template/go_service_production_pipeline.go @@ -0,0 +1,37 @@ +package go_handler_template + +const ServiceProductionPipeline = ` +kind: pipeline +type: docker +name: 部署{{ .Name }} + +steps: + - name: build + image: plugins/docker + volumes: + - name: docker + path: /var/run/docker.sock + settings: + username: + from_secret: reg_username + password: + from_secret: reg_password + repo: reg.icechen.cn/{{ .NameSpace }}/{{ .Name }} + registry: reg.icechen.cn + tags: ${DRONE_COMMIT:0:8} + dockerfile: ./{{ .Root }}/Dockerfile + + - name: run + image: docker + volumes: + - name: docker + path: /var/run/docker.sock + commands: + - docker rm -f {{ .NameSpace }}-{{ .Name }} + - docker run -d --name="{{ .NameSpace }}-{{ .Name }}" --network="nginx-net"{{ range $key, $value := .ServiceEnv }} --env {{ $key }}={{ $value }} {{ end }}reg.icechen.cn/{{ .NameSpace }}/{{ .Name }}:${DRONE_COMMIT:0:8} + +volumes: + - name: docker + host: + path: /var/run/docker.sock +` diff --git a/go_handler/go_handler_template/go_service_test_pipeline.go b/go_handler/go_handler_template/go_service_test_pipeline.go new file mode 100644 index 0000000..2d5ca3e --- /dev/null +++ b/go_handler/go_handler_template/go_service_test_pipeline.go @@ -0,0 +1,37 @@ +package go_handler_template + +const ServiceTestPipeline = ` +kind: pipeline +type: docker +name: 部署{{ .Name }} + +steps: + - name: build + image: plugins/docker + volumes: + - name: docker + path: /var/run/docker.sock + settings: + username: + from_secret: reg_username + password: + from_secret: reg_password + repo: reg.icechen.cn/{{ .NameSpace }}/{{ .Name }} + registry: reg.icechen.cn + tags: ${DRONE_COMMIT:0:8} + dockerfile: ./{{ .Root }}/Dockerfile + + - name: run + image: docker + volumes: + - name: docker + path: /var/run/docker.sock + commands: + - docker rm -f {{ .NameSpace }}-{{ .Name }} + - docker run -d --name="{{ .NameSpace }}-{{ .Name }}" --network="nginx-net"{{ range $key, $value := .ServiceEnv }} --env {{ $key }}={{ $value }} {{ end }}reg.icechen.cn/{{ .NameSpace }}/{{ .Name }}:${DRONE_COMMIT:0:8} + +volumes: + - name: docker + host: + path: /var/run/docker.sock +` diff --git a/go_handler/go_service_handler.go b/go_handler/go_service_handler.go index 6a7b755..bf868e8 100644 --- a/go_handler/go_service_handler.go +++ b/go_handler/go_service_handler.go @@ -3,55 +3,30 @@ package go_handler import ( "bytes" "text/template" + + "git.icechen.cn/pkg/drone_plugin/go_handler/go_handler_template" + + "git.icechen.cn/pkg/drone_plugin/consts" ) -const servicePipeline = ` -kind: pipeline -type: docker -name: 部署{{ .Name }} - -steps: - - name: build - image: plugins/docker - volumes: - - name: docker - path: /var/run/docker.sock - settings: - username: - from_secret: reg_username - password: - from_secret: reg_password - repo: reg.icechen.cn/{{ .NameSpace }}/{{ .Name }} - registry: reg.icechen.cn - tags: ${DRONE_COMMIT:0:8} - dockerfile: ./app/service/{{ .Name }}/Dockerfile - - - name: run - image: docker - volumes: - - name: docker - path: /var/run/docker.sock - commands: - - docker rm -f {{ .NameSpace }}-{{ .Name }} - - docker run -d --name="{{ .NameSpace }}-{{ .Name }}" --network="nginx-net"{{ range $key, $value := .ServiceEnv }} --env {{ $key }}={{ $value }} {{ end }}reg.icechen.cn/{{ .NameSpace }}/{{ .Name }}:${DRONE_COMMIT:0:8} - -volumes: - - name: docker - host: - path: /var/run/docker.sock -` - type GoServiceHandler struct { NameSpace string Name string Root string Port string ServiceEnv map[string]string + DeployEnv consts.Env } func (gsh GoServiceHandler) ToDestinationConfig() (string, error) { var err error t := template.New(gsh.Name) + + servicePipeline := go_handler_template.ServiceTestPipeline + if gsh.DeployEnv == consts.EnvProduction { + servicePipeline = go_handler_template.ServiceProductionPipeline + } + t, err = t.Parse(servicePipeline) if err != nil { return "", err