2021-12-29 19:40:57 +08:00
|
|
|
package go_handler
|
|
|
|
|
2021-12-30 00:00:25 +08:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
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}
|
2021-12-30 00:03:17 +08:00
|
|
|
dockerfile: ./app/service/{{ .Name }}/Dockerfile
|
2021-12-30 00:00:25 +08:00
|
|
|
|
|
|
|
- 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
|
|
|
|
`
|
|
|
|
|
2021-12-29 19:40:57 +08:00
|
|
|
type GoServiceHandler struct {
|
2021-12-30 00:00:25 +08:00
|
|
|
NameSpace string
|
|
|
|
Name string
|
|
|
|
Root string
|
|
|
|
Port string
|
|
|
|
ServiceEnv map[string]string
|
2021-12-29 19:40:57 +08:00
|
|
|
}
|
|
|
|
|
2021-12-30 00:00:25 +08:00
|
|
|
func (gsh GoServiceHandler) ToDestinationConfig() (string, error) {
|
|
|
|
var err error
|
|
|
|
t := template.New(gsh.Name)
|
2021-12-30 00:05:19 +08:00
|
|
|
t, err = t.Parse(servicePipeline)
|
2021-12-30 00:00:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
b := bytes.Buffer{}
|
|
|
|
err = t.Execute(&b, gsh)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.String(), nil
|
2021-12-29 19:40:57 +08:00
|
|
|
}
|