Skip to content

Commit

Permalink
Move build steps to separate build.sh script in generated RPM spec
Browse files Browse the repository at this point in the history
  • Loading branch information
adamperlin authored and cpuguy83 committed Aug 21, 2024
1 parent 4a469a9 commit 447bd57
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 20 deletions.
45 changes: 45 additions & 0 deletions frontend/rpm/handle_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rpm
import (
"context"
"fmt"
"strings"

"github.com/Azure/dalec"
"github.com/Azure/dalec/frontend"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
32 changes: 12 additions & 20 deletions frontend/rpm/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 447bd57

Please sign in to comment.