drone_plugin/go_handler/go_api_handler.go

69 lines
1.3 KiB
Go

package go_handler
import (
"bytes"
"text/template"
)
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" 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
}
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
}