@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user