update env
continuous-integration/drone/push Build is passing Details

master
icechen 2021-12-30 20:06:10 +08:00
parent 76571a6b0b
commit 62ab81441d
11 changed files with 217 additions and 83 deletions

View File

@ -1,11 +1,14 @@
package config_handler 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 := "" retConfig := ""
for _, api := range al { for _, api := range al {
config, err := api.toDestinationConfig(nameSpace, serviceEnv) config, err := api.toDestinationConfig(nameSpace, deployEnv, serviceEnv)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -14,7 +17,7 @@ func (al ApiList) toDestinationConfig(nameSpace string, serviceEnv map[string]st
return retConfig, nil 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 var handler Handler
switch a.Type { switch a.Type {
case go_handler.TypeGolang: case go_handler.TypeGolang:
@ -23,6 +26,7 @@ func (a Api) toDestinationConfig(nameSpace string, serviceEnv map[string]string)
Name: a.Name, Name: a.Name,
Root: a.Root, Root: a.Root,
Port: a.Port, Port: a.Port,
DeployEnv: deployEnv,
ServiceEnv: serviceEnv, ServiceEnv: serviceEnv,
} }
} }

View File

@ -3,8 +3,11 @@ package config_handler
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"strings" "strings"
"git.icechen.cn/pkg/drone_plugin/consts"
"git.icechen.cn/pkg/drone_plugin/git" "git.icechen.cn/pkg/drone_plugin/git"
"github.com/drone/drone-go/drone" "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 return nil, err
} }
// 2. 非 monorepo 的直接返回 // 2. 是否部署
// 2.1 非 monorepo 的直接返回
if cfg.Type != TypeMonorepo { if cfg.Type != TypeMonorepo {
// 返回 nil 按照 204 处理 // 返回 nil 按照 204 处理
return nil, nil return nil, nil
} }
// 2.2 无部署环境报错返回,阻断部署
if getDeployEnv(req) == consts.EnvNone {
return nil, errors.New("ignore event")
}
// 3. 获取提交文件树 // 3. 获取提交文件树
modifiedFileList, err := getGitClient(req).GetCommitModifiedFileList() modifiedFileList, err := getGitClient(req).GetCommitModifiedFileList()
@ -54,7 +62,7 @@ func (p *plugin) Find(ctx context.Context, req *config.Request) (*drone.Config,
// 4. 根据文件树以及原始配置文件的信息组装需要构建的服务的ci信息 // 4. 根据文件树以及原始配置文件的信息组装需要构建的服务的ci信息
// 4.1 api // 4.1 api
modifiedApiList := getModifiedApi(cfg.Api, modifiedFileList) 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 { if err != nil {
logrus.Error(err) logrus.Error(err)
return nil, err return nil, err
@ -62,7 +70,7 @@ func (p *plugin) Find(ctx context.Context, req *config.Request) (*drone.Config,
// 4.2 service // 4.2 service
modifiedServiceList := getModifiedService(cfg.Service, modifiedFileList) 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 { if err != nil {
logrus.Error(err) logrus.Error(err)
return nil, 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) 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 { func getModifiedApi(api ApiList, modifiedFileList []string) ApiList {
ret := make(ApiList, 0) ret := make(ApiList, 0)
tempIndex := NewSet() tempIndex := NewSet()

View File

@ -1,13 +1,14 @@
package config_handler package config_handler
import ( import (
"git.icechen.cn/pkg/drone_plugin/consts"
"git.icechen.cn/pkg/drone_plugin/go_handler" "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 := "" retConfig := ""
for _, service := range sl { for _, service := range sl {
config, err := service.toDestinationConfig(nameSpace, serviceEnv) config, err := service.toDestinationConfig(nameSpace, deployEnv, serviceEnv)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -16,7 +17,7 @@ func (sl ServiceList) toDestinationConfig(nameSpace string, serviceEnv map[strin
return retConfig, nil 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 var handler Handler
switch s.Type { switch s.Type {
case go_handler.TypeGolang: case go_handler.TypeGolang:
@ -25,6 +26,7 @@ func (s Service) toDestinationConfig(nameSpace string, serviceEnv map[string]str
Name: s.Name, Name: s.Name,
Root: s.Root, Root: s.Root,
Port: s.Port, Port: s.Port,
DeployEnv: deployEnv,
ServiceEnv: serviceEnv, ServiceEnv: serviceEnv,
} }
} }

9
consts/env.go 100644
View File

@ -0,0 +1,9 @@
package consts
type Env uint
const (
EnvNone Env = 0 // 忽略
EnvTest Env = 1 // 测试环境
EnvProduction Env = 2 // 生产环境
)

View File

@ -12,7 +12,8 @@ var cli *gitea.Client
func init() { func init() {
var err error 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 { if err != nil {
panic(err) panic(err)
} }

View File

@ -3,57 +3,32 @@ package go_handler
import ( import (
"bytes" "bytes"
"text/template" "text/template"
"git.icechen.cn/pkg/drone_plugin/go_handler/go_handler_template"
"git.icechen.cn/pkg/drone_plugin/consts"
) )
const TypeGolang = "golang" 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 { type GoApiHandler struct {
NameSpace string NameSpace string
Name string Name string
Root string Root string
Port string Port string
ServiceEnv map[string]string ServiceEnv map[string]string
DeployEnv consts.Env
} }
func (gah GoApiHandler) ToDestinationConfig() (string, error) { func (gah GoApiHandler) ToDestinationConfig() (string, error) {
var err error var err error
t := template.New(gah.Name) 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) t, err = t.Parse(apiPipeline)
if err != nil { if err != nil {
return "", err return "", err

View File

@ -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
`

View File

@ -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
`

View File

@ -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
`

View File

@ -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
`

View File

@ -3,55 +3,30 @@ package go_handler
import ( import (
"bytes" "bytes"
"text/template" "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 { type GoServiceHandler struct {
NameSpace string NameSpace string
Name string Name string
Root string Root string
Port string Port string
ServiceEnv map[string]string ServiceEnv map[string]string
DeployEnv consts.Env
} }
func (gsh GoServiceHandler) ToDestinationConfig() (string, error) { func (gsh GoServiceHandler) ToDestinationConfig() (string, error) {
var err error var err error
t := template.New(gsh.Name) 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) t, err = t.Parse(servicePipeline)
if err != nil { if err != nil {
return "", err return "", err