From a09b11256b1ad540d6606912dab82a992f74b130 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Sun, 22 Dec 2024 09:00:02 +0100 Subject: Clean up, add a deployment stage Errors should be handled a bit more nicely now. The SFTP part could also be done from deploy scripts like: scp -i {runner.ssh.identity} \ -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \ {runner.ssh.user}@{runner.ssh.address%:*} -p {runner.ssh.address#*:} but that is deemed way too annoying, so we do it from Go. --- acid.adoc | 5 +- acid.go | 627 ++++++++++++++++++++++++++++++++++++++---------------- acid.yaml.example | 8 +- go.mod | 10 +- go.sum | 69 +++++- 5 files changed, 517 insertions(+), 202 deletions(-) diff --git a/acid.adoc b/acid.adoc index ff5d69b..9d3cf67 100644 --- a/acid.adoc +++ b/acid.adoc @@ -45,7 +45,7 @@ file present in the distribution. All paths are currently relative to the directory you launch *acid* from. -The *notify*, *setup*, and *build* scripts are processed using Go's +The *notify*, *setup*, *build*, and *deploy* scripts are processed using Go's _text/template_ package, and take an object describing the task, which has the following fields: @@ -79,7 +79,8 @@ in *sh*(1) command arguments. Runners ------- -Runners receive the following additional environment variables: +Runners and deploy scripts receive the following additional +environment variables: *ACID_ROOT*:: The same as the base directory for configuration. *ACID_RUNNER*:: The same as *Runner* in script templates. diff --git a/acid.go b/acid.go index f938d02..6f62893 100644 --- a/acid.go +++ b/acid.go @@ -21,6 +21,7 @@ import ( "os" "os/exec" "os/signal" + "path/filepath" "sort" "strconv" "strings" @@ -30,6 +31,7 @@ import ( "time" _ "github.com/mattn/go-sqlite3" + "github.com/pkg/sftp" "golang.org/x/crypto/ssh" "gopkg.in/yaml.v3" ) @@ -96,6 +98,7 @@ func (cf *ConfigProject) AutomaticRunners() (runners []string) { type ConfigProjectRunner struct { Setup string `yaml:"setup"` // project setup script (SSH) Build string `yaml:"build"` // project build script (SSH) + Deploy string `yaml:"deploy"` // project deploy script (local) Timeout string `yaml:"timeout"` // timeout duration } @@ -153,7 +156,8 @@ func giteaNewRequest(ctx context.Context, method, path string, body io.Reader) ( func getTasks(ctx context.Context, query string, args ...any) ([]Task, error) { rows, err := gDB.QueryContext(ctx, ` SELECT id, owner, repo, hash, runner, - state, detail, notified, runlog, tasklog FROM task `+query, args...) + state, detail, notified, + runlog, tasklog, deploylog FROM task `+query, args...) if err != nil { return nil, err } @@ -163,7 +167,8 @@ func getTasks(ctx context.Context, query string, args ...any) ([]Task, error) { for rows.Next() { var t Task err := rows.Scan(&t.ID, &t.Owner, &t.Repo, &t.Hash, &t.Runner, - &t.State, &t.Detail, &t.Notified, &t.RunLog, &t.TaskLog) + &t.State, &t.Detail, &t.Notified, + &t.RunLog, &t.TaskLog, &t.DeployLog) if err != nil { return nil, err } @@ -259,6 +264,10 @@ var templateTask = template.Must(template.New("tasks").Parse(`
{{printf "%s" .TaskLog}}{{end}} +{{if .DeployLog}} +
{{printf "%s" .DeployLog}}+{{end}}