From 447bd5798e07aacac521ede04541196cca6a4cd2 Mon Sep 17 00:00:00 2001 From: adamperlin Date: Tue, 20 Aug 2024 13:42:46 -0700 Subject: [PATCH] Move build steps to separate build.sh script in generated RPM spec --- frontend/rpm/handle_sources.go | 45 ++++++++++++++++++++++++++++++++++ frontend/rpm/template.go | 32 +++++++++--------------- 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/frontend/rpm/handle_sources.go b/frontend/rpm/handle_sources.go index 4e4603327..d46ddacc8 100644 --- a/frontend/rpm/handle_sources.go +++ b/frontend/rpm/handle_sources.go @@ -3,6 +3,7 @@ package rpm import ( "context" "fmt" + "strings" "github.com/Azure/dalec" "github.com/Azure/dalec/frontend" @@ -54,6 +55,45 @@ func HandleSources(wf WorkerFunc) gwclient.BuildFunc { } } +func buildScriptSourceState(spec *dalec.Spec) *llb.State { + if len(spec.Build.Steps) == 0 { + return nil + } + + script := buildScript(spec) + st := llb.Scratch().File(llb.Mkfile("build.sh", 0755, []byte(script))) + return &st +} + +func buildScript(spec *dalec.Spec) string { + b := &strings.Builder{} + + t := spec.Build + if len(t.Steps) == 0 { + return "" + } + + fmt.Fprintln(b, "#!/bin/sh") + fmt.Fprintln(b, "set -e") + + if spec.HasGomods() { + fmt.Fprintln(b, "export GOMODCACHE=\"$(pwd)/"+gomodsName+"\"") + } + + envKeys := dalec.SortMapKeys(t.Env) + for _, k := range envKeys { + v := t.Env[k] + fmt.Fprintf(b, "export %s=\"%s\"\n", k, v) + } + + for _, step := range t.Steps { + writeStep(b, step) + } + + b.WriteString("\n") + return b.String() +} + func Dalec2SourcesLLB(worker llb.State, spec *dalec.Spec, sOpt dalec.SourceOpts, opts ...llb.ConstraintsOpt) ([]llb.State, error) { sources, err := dalec.Sources(spec, sOpt) if err != nil { @@ -83,6 +123,11 @@ func Dalec2SourcesLLB(worker llb.State, spec *dalec.Spec, sOpt dalec.SourceOpts, out = append(out, st.With(sourceTar(worker, gomodsName, withPG("Tar gomod deps")...))) } + scriptSt := buildScriptSourceState(spec) + if scriptSt != nil { + out = append(out, *scriptSt) + } + return out, nil } diff --git a/frontend/rpm/template.go b/frontend/rpm/template.go index 3a354790e..a497aa23c 100644 --- a/frontend/rpm/template.go +++ b/frontend/rpm/template.go @@ -15,6 +15,7 @@ import ( ) const gomodsName = "__gomods" +const buildScriptName = "build.sh" var specTmpl = template.Must(template.New("spec").Funcs(tmplFuncs).Parse(strings.TrimSpace(` Name: {{.Name}} @@ -238,8 +239,15 @@ func (w *specWrapper) Sources() (fmt.Stringer, error) { fmt.Fprintf(b, "Source%d: %s\n", idx, ref) } + sourceIdx := len(keys) + if w.Spec.HasGomods() { - fmt.Fprintf(b, "Source%d: %s.tar.gz\n", len(keys), gomodsName) + fmt.Fprintf(b, "Source%d: %s.tar.gz\n", sourceIdx, gomodsName) + sourceIdx += 1 + } + + if len(w.Spec.Build.Steps) > 0 { + fmt.Fprintf(b, "Source%d: %s\n", sourceIdx, buildScriptName) } if len(keys) > 0 { @@ -328,30 +336,14 @@ func writeStep(b *strings.Builder, step dalec.BuildStep) { func (w *specWrapper) BuildSteps() fmt.Stringer { b := &strings.Builder{} - t := w.Spec.Build - if len(t.Steps) == 0 { + if len(w.Spec.Build.Steps) == 0 { return b } fmt.Fprintf(b, "%%build\n") - - fmt.Fprintln(b, "set -e") - - if w.Spec.HasGomods() { - fmt.Fprintln(b, "export GOMODCACHE=\"$(pwd)/"+gomodsName+"\"") - } - - envKeys := dalec.SortMapKeys(t.Env) - for _, k := range envKeys { - v := t.Env[k] - fmt.Fprintf(b, "export %s=\"%s\"\n", k, v) - } - - for _, step := range t.Steps { - writeStep(b, step) - } - + fmt.Fprintf(b, "%%{_sourcedir}/%s\n", buildScriptName) b.WriteString("\n") + return b }