drone_plugin/go_handler/go_api_handler.go

68 lines
1.3 KiB
Go
Raw Normal View History

2021-12-28 20:42:51 +08:00
package go_handler
2021-12-29 20:24:21 +08:00
import (
"bytes"
"text/template"
)
2021-12-28 20:42:51 +08:00
const TypeGolang = "golang"
2021-12-29 20:24:21 +08:00
const apiPipeline = `
2021-12-28 20:42:51 +08:00
kind: pipeline
2021-12-29 20:24:21 +08:00
type: docker
name: {{ .Name }}
2021-12-28 20:42:51 +08:00
steps:
2021-12-29 20:24:21 +08:00
- 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}
- name: run
image: docker
volumes:
- name: docker
path: /var/run/docker.sock
commands:
- docker rm -f {{ .NameSpace }}-{{ .Name }}
2021-12-29 20:28:37 +08:00
- docker run -d --name="{{ .NameSpace }}-{{ .Name }}" --network="nginx-net" reg.icechen.cn/{{ .NameSpace }}/{{ .Name }}:${DRONE_COMMIT:0:8}
2021-12-29 20:24:21 +08:00
volumes:
- name: docker
host:
path: /var/run/docker.sock
2021-12-28 20:42:51 +08:00
`
2021-12-29 19:40:57 +08:00
type GoApiHandler struct {
2021-12-29 20:24:21 +08:00
NameSpace string
Name string
Root string
Port string
2021-12-29 19:40:57 +08:00
}
2021-12-28 20:42:51 +08:00
2021-12-29 20:24:21 +08:00
func (gah GoApiHandler) ToDestinationConfig() (string, error) {
var err error
t := template.New(gah.Name)
t, err = t.Parse(apiPipeline)
if err != nil {
return "", err
}
b := bytes.Buffer{}
err = t.Execute(&b, gah)
if err != nil {
return "", err
}
return b.String(), nil
2021-12-28 20:42:51 +08:00
}