Grabbing all DRONE_ENV variables

This commit is contained in:
Daniel
2019-03-29 17:06:14 +11:00
parent a8d725d653
commit 7dc06ffbe1
7 changed files with 66 additions and 84 deletions
+1
View File
@@ -0,0 +1 @@
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o slack
Generated
+8 -2
View File
@@ -1,6 +1,12 @@
hash: 34a5d854d17178f6f0896e906995846ae473f121735e443361c91f1eb0fa783f
updated: 2019-03-29T08:55:36.35423+11:00
hash: dd80df76b62073b5846624df90929f50c8d8ded4aad953864b41bb929cad8ad4
updated: 2019-03-29T13:02:31.745505+11:00
imports:
- name: github.com/aymerick/raymond
version: b565731e1464263de0bda75f2e45d97b54b60110
subpackages:
- ast
- lexer
- parser
- name: github.com/joho/godotenv
version: 5c0e6c6ab1a0a9ef0a8822cba3a05d62f7dad941
- name: github.com/urfave/cli
BIN
View File
Binary file not shown.
+11 -42
View File
@@ -17,39 +17,28 @@ func main() {
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "ca",
EnvVar: "APP_LANG",
Usage: "KUBE_CA,PLUGIN_CA",
Usage: "Certificate Authority cert to use (Base-64 encoded)",
EnvVar: "PLUGIN_CA",
},
cli.StringFlag{
Name: "token",
EnvVar: "APP_LANG",
Usage: "KUBE_TOKEN,PLUGIN_TOKEN",
Usage: "Kubernetes service token",
EnvVar: "PLUGIN_TOKEN",
},
cli.StringFlag{
Name: "server",
EnvVar: "APP_LANG",
Usage: "KUBE_SERVER,PLUGIN_SERVER",
Usage: "Kubernetes server address",
EnvVar: "PLUGIN_SERVER",
},
cli.StringFlag{
Name: "namespace",
Usage: "namespace to use: 'default' is the default :-)",
EnvVar: "KUBE_NAMESPACE,PLUGIN_NAMESPACE",
Usage: "namespace to use: 'default' is the default",
EnvVar: "PLUGIN_NAMESPACE",
},
cli.StringFlag{
Name: "template",
Usage: "template file to use for deployment e.g. deployment.yaml",
EnvVar: "KUBE_TEMPLATE,PLUGIN_TEMPLATE",
},
cli.StringFlag{
Name: "commit.sha",
Usage: "git commit sha",
EnvVar: "DRONE_COMMIT_SHA",
},
cli.StringFlag{
Name: "commit.branch",
Value: "master",
Usage: "git commit branch",
EnvVar: "DRONE_COMMIT_BRANCH",
EnvVar: "PLUGIN_TEMPLATE",
},
}
if err := app.Run(os.Args); err != nil {
@@ -62,32 +51,12 @@ func run(c *cli.Context) error {
_ = godotenv.Load(c.String("env-file"))
}
plugin := Plugin{
Repo: Repo{
Owner: c.String("repo.owner"),
Name: c.String("repo.name"),
},
Build: Build{
Tag: c.String("build.tag"),
Number: c.Int("build.number"),
Event: c.String("build.event"),
Status: c.String("build.status"),
Commit: c.String("commit.sha"),
Ref: c.String("commit.ref"),
Branch: c.String("commit.branch"),
Author: c.String("commit.author"),
Link: c.String("build.link"),
Started: c.Int64("build.started"),
Created: c.Int64("build.created"),
},
Job: Job{
Started: c.Int64("job.started"),
},
Config: Config{
Template: c.String("template"),
KubeConfig: KubeConfig{
Token: c.String("token"),
Server: c.String("server"),
Ca: c.String("ca"),
Namespace: c.String("namespace"),
Template: c.String("template"),
},
}
return plugin.Exec()
+42 -38
View File
@@ -4,62 +4,40 @@ import (
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"github.com/aymerick/raymond"
)
type (
Repo struct {
Owner string
Name string
}
Build struct {
Tag string
Event string
Number int
Commit string
Ref string
Branch string
Author string
Status string
Link string
Started int64
Created int64
}
Job struct {
Started int64
}
Config struct {
KubeConfig struct {
Ca string
Server string
Token string
Namespace string
Template string
}
Plugin struct {
Repo Repo
Build Build
Config Config
Job Job
Template string
KubeConfig KubeConfig
}
)
func (p Plugin) Exec() error {
if p.Config.Server == "" {
if p.KubeConfig.Server == "" {
log.Fatal("KUBE_SERVER is not defined")
}
if p.Config.Token == "" {
if p.KubeConfig.Token == "" {
log.Fatal("KUBE_TOKEN is not defined")
}
if p.Config.Ca == "" {
if p.KubeConfig.Ca == "" {
log.Fatal("KUBE_CA is not defined")
}
if p.Config.Namespace == "" {
p.Config.Namespace = "default"
if p.KubeConfig.Namespace == "" {
p.KubeConfig.Namespace = "default"
}
if p.Config.Template == "" {
if p.Template == "" {
log.Fatal("KUBE_TEMPLATE, or template must be defined")
}
@@ -69,11 +47,37 @@ func (p Plugin) Exec() error {
// log.Fatal(err.Error())
// }
// parse the template file and do substitutions
out, err := ioutil.ReadFile(p.Config.Template)
raw, err := ioutil.ReadFile(p.Template)
if err != nil {
return err
}
fmt.Printf("%v", out)
source := string(raw)
ctx := make(map[string]string)
ctx["KUBE_CA"] = p.KubeConfig.Ca
ctx["KUBE_TOKEN"] = p.KubeConfig.Ca
ctx["KUBE_SERVER"] = p.KubeConfig.Ca
droneEnv := os.Environ()
for _, value := range droneEnv {
re := regexp.MustCompile(`^(DRONE_.*)=(.*)`)
if re.MatchString(value) {
matches := re.FindStringSubmatch(value)
ctx[matches[1]] = matches[2]
}
}
// parse template
tpl, err := raymond.Parse(source)
if err != nil {
panic(err)
}
result, err := tpl.Exec(ctx)
if err != nil {
panic(err)
}
fmt.Print(result)
return err
}
Regular → Executable
+2
View File
@@ -1,3 +1,5 @@
#!/bin/bash
export DRONE_BRANCH=test
go build
./kubano --server=go --token=lol --ca=ho --template=test/deployment.yaml
+2 -2
View File
@@ -10,11 +10,11 @@ spec:
template:
metadata:
labels:
app: {{ deployment-name }}
app: {{commit.branch}}
spec:
containers:
- name: nginx
image: 10.0.0.24:443/{{ git-username }}:{{ git-repo }}.{{ git-branch }}
image: 10.0.0.24:443/test:{{DRONE_BRANCH}}.{{[commit.sha]}}
ports:
- containerPort: 80
imagePullSecrets: