Browse Source

Allow text/template syntax for environment variables for greater flexibility

and to allow mapping any data from the webhooks to env variables.
tags/v0.2.0
Bernhard Fröhlich 1 month ago
parent
commit
9fb4c18e3d
Signed by: Bernhard Froehlich <decke@bluelife.at> GPG Key ID: 4DD88C3F9F3B8333
1 changed files with 46 additions and 7 deletions
  1. 46
    7
      main.go

+ 46
- 7
main.go View File

@@ -20,6 +20,7 @@ import (
20 20
 	"regexp"
21 21
 	"strings"
22 22
 	"sync"
23
+	texttemplate "text/template"
23 24
 	"time"
24 25
 
25 26
 	"github.com/NYTimes/gziphandler"
@@ -170,6 +171,23 @@ func (c *controller) renderBuildTemplate(j *job) {
170 171
 	outfile.Sync()
171 172
 }
172 173
 
174
+func (c *controller) evalEnvVariable(j *job, key string, val string) (string, string) {
175
+	tmpl, err := texttemplate.New(key).Parse(val)
176
+	if err != nil {
177
+		log.Printf("Failed parsing env var %s=%s: %v", key, val, err)
178
+		return key, ""
179
+	}
180
+
181
+	var out bytes.Buffer
182
+	err = tmpl.Execute(&out, &j)
183
+	if err != nil {
184
+		log.Printf("Failed executing env var %s: %v", key, err)
185
+		return key, ""
186
+	}
187
+
188
+	return key, out.String()
189
+}
190
+
173 191
 func (c *controller) sendStatusUpdate(j *job, b *build) error {
174 192
 	target := ""
175 193
 
@@ -208,14 +226,10 @@ func (c *controller) startWorker(q *queue) {
208 226
 			log.Printf("ID %s started on %s\n", j.ID, q.Name)
209 227
 			c.sendStatusUpdate(j, b)
210 228
 
211
-			env := append(os.Environ(),
212
-				fmt.Sprintf("JOB_ID=%s", j.ID),
213
-				fmt.Sprintf("COMMIT_ID=%s", j.PushEvent.CommitID),
214
-				fmt.Sprintf("REPO_URL=%s", j.PushEvent.Repository.CloneURL),
215
-			)
216
-
229
+			env := os.Environ()
217 230
 			for k, v := range q.Environment {
218
-				env = append(env, fmt.Sprintf("%s=%s", k, v))
231
+				key, val := c.evalEnvVariable(j, k, v)
232
+				env = append(env, fmt.Sprintf("%s=%s", key, val))
219 233
 			}
220 234
 
221 235
 			os.MkdirAll(q.Workdir, os.ModePerm)
@@ -399,6 +413,31 @@ func parseConfig(file string) config {
399 413
 		if err != nil {
400 414
 			log.Fatalf("Error: %v", err)
401 415
 		}
416
+
417
+		_, ok := cfg.Queues[i].Environment["JOB_ID"]
418
+		if ! ok {
419
+			cfg.Queues[i].Environment["JOB_ID"] = "{{.ID}}"
420
+		}
421
+
422
+		_, ok = cfg.Queues[i].Environment["COMMIT_ID"]
423
+		if ! ok {
424
+			cfg.Queues[i].Environment["COMMIT_ID"] = "{{.PushEvent.CommitID}}"
425
+		}
426
+
427
+		_, ok = cfg.Queues[i].Environment["REPO_URL"]
428
+		if ! ok {
429
+			cfg.Queues[i].Environment["REPO_URL"] = "{{.PushEvent.Repository.HTMLURL}}"
430
+		}
431
+
432
+		_, ok = cfg.Queues[i].Environment["AUTHOR"]
433
+		if ! ok {
434
+			cfg.Queues[i].Environment["AUTHOR"] = "{{(index .PushEvent.Commits 0).Author.Username}}"
435
+		}
436
+
437
+		_, ok = cfg.Queues[i].Environment["AUTHOR_EMAIL"]
438
+		if ! ok {
439
+			cfg.Queues[i].Environment["AUTHOR_EMAIL"] = "{{(index .PushEvent.Commits 0).Author.EMail}}"
440
+		}
402 441
 	}
403 442
 
404 443
 	return cfg

Loading…
Cancel
Save