Compare commits

...

2 commits

Author SHA1 Message Date
85f1a81ecc permitir variables de entorno (para secretos)
Some checks failed
repro-run Corre repro-run.json
2023-01-26 16:13:52 -03:00
f4e717e47a Arreglar cache al conseguir apk-tools 2023-01-25 20:06:41 -03:00
8 changed files with 89 additions and 18 deletions

13
cli.go
View file

@ -4,14 +4,25 @@ import (
"encoding/json" "encoding/json"
"log" "log"
"os" "os"
"strings"
"gitea.nulo.in/Nulo/repro-run/runner" "gitea.nulo.in/Nulo/repro-run/runner"
"github.com/joho/godotenv"
) )
func main() { func main() {
config, err := readConfig() config, err := readConfig()
must(err) 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) { func must(err error, where ...string) {

View file

@ -1,14 +1,42 @@
<!DOCTYPE html> <!DOCTYPE html>
<meta charset="utf-8" /> <meta charset="utf-8" />
<pre></pre>
<h1>Build a webhook!</h1>
<form>
<input type="text" name="k1" placeholder="k1" />
<textarea name="v1" placeholder="v1"></textarea><br />
<input type="text" name="k2" placeholder="k2" />
<textarea name="v2" placeholder="v2"></textarea><br />
<input type="text" name="k3" placeholder="k3" />
<textarea name="v3" placeholder="v3"></textarea><br />
<input type="text" name="k4" placeholder="k4" />
<textarea name="v4" placeholder="v4"></textarea><br />
<input type="text" name="k5" placeholder="k5" />
<textarea name="v5" placeholder="v5"></textarea><br />
<button>Build URL</button>
</form>
<input id="output" readonly />
<script> <script>
const el = document.querySelector("pre"); const form = document.querySelector("form");
const id = location.hash.slice(1); const output = document.querySelector("#output");
const ws = new WebSocket( form.addEventListener("submit", (event) => {
location.origin.replace(/^http/, "ws") + "/logs/socket/" + id event.preventDefault();
);
window.ws = ws; let url = new URL(location.href);
ws.addEventListener("message", async (event) => { url.pathname = "/webhook";
el.append(await event.data.text()); for (let i = 1; i < 6; i++) {
const [key, value] = [
event.target[`k${i}`].value,
event.target[`v${i}`].value,
];
if (key) url.searchParams.set(key, value);
}
output.value = url;
return false;
}); });
</script> </script>

View file

@ -12,7 +12,10 @@ import (
) )
//go:embed index.html //go:embed index.html
var index []byte var indexHtml []byte
//go:embed run.html
var runHtml []byte
func main() { func main() {
config := parseConfig() config := parseConfig()
@ -36,7 +39,8 @@ func main() {
http.Handle("/webhook", webhook{config: config, gitea: g}) http.Handle("/webhook", webhook{config: config, gitea: g})
http.Handle("/logs/socket/", logWebsocket{}) 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)) log.Fatal(http.ListenAndServe(port, nil))
} }

14
gitea-ci/run.html Normal file
View file

@ -0,0 +1,14 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<pre></pre>
<script>
const el = document.querySelector("pre");
const id = location.hash.slice(1);
const ws = new WebSocket(
location.origin.replace(/^http/, "ws") + "/logs/socket/" + id
);
window.ws = ws;
ws.addEventListener("message", async (event) => {
el.append(await event.data.text());
});
</script>

View file

@ -140,7 +140,7 @@ func (h webhook) ServeHTTP(w http.ResponseWriter, req *http.Request) {
} }
runId := req.Header.Get("X-Gitea-Delivery") runId := req.Header.Get("X-Gitea-Delivery")
runUrl := h.config.publicUrl + "/#" + runId runUrl := h.config.publicUrl + "/run#" + runId
log.Printf("New run: %s", runUrl) log.Printf("New run: %s", runUrl)
run := runss.newRun(runId) run := runss.newRun(runId)
defer run.finishRun() defer run.finishRun()
@ -213,7 +213,14 @@ func (h webhook) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return 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" state := "success"
if err != nil { if err != nil {
log.Println("runner.Run", err) log.Println("runner.Run", err)

5
go.mod
View file

@ -7,4 +7,7 @@ require (
nhooyr.io/websocket v1.8.7 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
)

2
go.sum
View file

@ -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/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 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 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 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8= github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8=

View file

@ -14,7 +14,7 @@ import (
//go:embed alpine/keys //go:embed alpine/keys
var alpineKeys embed.FS 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 { if err = os.RemoveAll(rootfs); err != nil {
return return
} }
@ -58,7 +58,7 @@ https://dl-cdn.alpinelinux.org/alpine/v3.17/community`), 0600); err != nil {
cmd := exec.Command("apk", "add", cmd := exec.Command("apk", "add",
"--root", rootfs, "--root", rootfs,
"--initdb", "--initdb",
"--cache-dir", path.Join(cache, "_var_cache_apk"), "--cache-dir", path.Join("..", cache, "_var_cache_apk"),
"apk-tools") "apk-tools")
if err = cmd.Run(); err != nil { if err = cmd.Run(); err != nil {
return fmt.Errorf("apk add apk-tools: %w", err) return fmt.Errorf("apk add apk-tools: %w", err)
@ -92,7 +92,9 @@ https://dl-cdn.alpinelinux.org/alpine/v3.17/community`), 0600); err != nil {
}, },
cachedParams...) cachedParams...)
log.Println(strings.Join(params, " ")) for k, v := range env {
params = append(params, "--setenv", k, v)
}
cmd = exec.Command("bwrap", append(params, cmd = exec.Command("bwrap", append(params,
"apk", "add", "--quiet", "busybox", "busybox-suid", "libc-utils", "alpine-baselayout", "alpine-conf", "alpine-release")...) "apk", "add", "--quiet", "busybox", "busybox-suid", "libc-utils", "alpine-baselayout", "alpine-conf", "alpine-release")...)