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
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,
}
}

View File

@ -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()

View File

@ -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,
}
}

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() {
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)
}

View File

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

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 (
"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