From 85f1a81eccd60bca7c218d808856fb9d28b4b809 Mon Sep 17 00:00:00 2001 From: Nulo Date: Thu, 26 Jan 2023 16:13:52 -0300 Subject: [PATCH] permitir variables de entorno (para secretos) --- cli.go | 13 ++++++++++++- gitea-ci/index.html | 46 ++++++++++++++++++++++++++++++++++++--------- gitea-ci/main.go | 8 ++++++-- gitea-ci/run.html | 14 ++++++++++++++ gitea-ci/webhook.go | 11 +++++++++-- go.mod | 5 ++++- go.sum | 2 ++ runner/runner.go | 6 ++++-- 8 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 gitea-ci/run.html diff --git a/cli.go b/cli.go index b8aa0cb..d701f9d 100644 --- a/cli.go +++ b/cli.go @@ -4,14 +4,25 @@ import ( "encoding/json" "log" "os" + "strings" "gitea.nulo.in/Nulo/repro-run/runner" + "github.com/joho/godotenv" ) func main() { config, err := readConfig() must(err) - must(runner.Run(config, "rootfs/", "cache/", ".", nil, false)) + + env, err := godotenv.Read() + if err != nil { + log.Printf("Couldn't load env file: %v", err) + } + for k, v := range env { + env[k] = strings.ReplaceAll(v, "\\n", "\n") + } + + must(runner.Run(config, "rootfs/", "cache/", ".", nil, false, env)) } func must(err error, where ...string) { diff --git a/gitea-ci/index.html b/gitea-ci/index.html index aa4948f..9621968 100644 --- a/gitea-ci/index.html +++ b/gitea-ci/index.html @@ -1,14 +1,42 @@ -

+
+

Build a webhook!

+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ + + diff --git a/gitea-ci/main.go b/gitea-ci/main.go index 59dc0ff..9ed8e9a 100644 --- a/gitea-ci/main.go +++ b/gitea-ci/main.go @@ -12,7 +12,10 @@ import ( ) //go:embed index.html -var index []byte +var indexHtml []byte + +//go:embed run.html +var runHtml []byte func main() { config := parseConfig() @@ -36,7 +39,8 @@ func main() { http.Handle("/webhook", webhook{config: config, gitea: g}) http.Handle("/logs/socket/", logWebsocket{}) - http.Handle("/", staticRoute{file: index, mime: "text/html; charset=utf-8"}) + http.Handle("/", staticRoute{file: indexHtml, mime: "text/html; charset=utf-8"}) + http.Handle("/run", staticRoute{file: runHtml, mime: "text/html; charset=utf-8"}) log.Fatal(http.ListenAndServe(port, nil)) } diff --git a/gitea-ci/run.html b/gitea-ci/run.html new file mode 100644 index 0000000..aa4948f --- /dev/null +++ b/gitea-ci/run.html @@ -0,0 +1,14 @@ + + +

+
diff --git a/gitea-ci/webhook.go b/gitea-ci/webhook.go
index d72d733..a0d1d63 100644
--- a/gitea-ci/webhook.go
+++ b/gitea-ci/webhook.go
@@ -140,7 +140,7 @@ func (h webhook) ServeHTTP(w http.ResponseWriter, req *http.Request) {
 	}
 
 	runId := req.Header.Get("X-Gitea-Delivery")
-	runUrl := h.config.publicUrl + "/#" + runId
+	runUrl := h.config.publicUrl + "/run#" + runId
 	log.Printf("New run: %s", runUrl)
 	run := runss.newRun(runId)
 	defer run.finishRun()
@@ -213,7 +213,14 @@ func (h webhook) ServeHTTP(w http.ResponseWriter, req *http.Request) {
 		return
 	}
 
-	err = runner.Run(*runnerConfig, rootfs, cache, src, run, h.config.doNotDeleteTmp)
+	query := req.URL.Query()
+	env := make(map[string]string)
+	for key := range query {
+		val := query.Get(key)
+		env[key] = val
+	}
+
+	err = runner.Run(*runnerConfig, rootfs, cache, src, run, h.config.doNotDeleteTmp, env)
 	state := "success"
 	if err != nil {
 		log.Println("runner.Run", err)
diff --git a/go.mod b/go.mod
index dd66095..48f2324 100644
--- a/go.mod
+++ b/go.mod
@@ -7,4 +7,7 @@ require (
 	nhooyr.io/websocket v1.8.7
 )
 
-require github.com/klauspost/compress v1.10.3 // indirect
+require (
+	github.com/joho/godotenv v1.4.0 // indirect
+	github.com/klauspost/compress v1.10.3 // indirect
+)
diff --git a/go.sum b/go.sum
index a91c676..6292acb 100644
--- a/go.sum
+++ b/go.sum
@@ -25,6 +25,8 @@ github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
 github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
 github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
 github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
+github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
+github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
 github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
 github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
 github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8=
diff --git a/runner/runner.go b/runner/runner.go
index 1d2ab5a..fdc5ef6 100644
--- a/runner/runner.go
+++ b/runner/runner.go
@@ -14,7 +14,7 @@ import (
 //go:embed alpine/keys
 var alpineKeys embed.FS
 
-func Run(config Config, rootfs string, cache string, src string, w io.Writer, dontDeleteTemp bool) (err error) {
+func Run(config Config, rootfs string, cache string, src string, w io.Writer, dontDeleteTemp bool, env map[string]string) (err error) {
 	if err = os.RemoveAll(rootfs); err != nil {
 		return
 	}
@@ -92,7 +92,9 @@ https://dl-cdn.alpinelinux.org/alpine/v3.17/community`), 0600); err != nil {
 		},
 		cachedParams...)
 
-	log.Println(strings.Join(params, " "))
+	for k, v := range env {
+		params = append(params, "--setenv", k, v)
+	}
 
 	cmd = exec.Command("bwrap", append(params,
 		"apk", "add", "--quiet", "busybox", "busybox-suid", "libc-utils", "alpine-baselayout", "alpine-conf", "alpine-release")...)