forked from wrenn/wrenn
Add pre/post build stages to template builds
Pre-build: apt update Post-build: apt clean, apt autoremove, rm apt lists Total steps count includes pre/post commands for accurate progress bars.
This commit is contained in:
@ -25,6 +25,18 @@ const (
|
|||||||
healthcheckTimeout = 60 * time.Second
|
healthcheckTimeout = 60 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// preBuildCmds run before the user recipe to prepare the build environment.
|
||||||
|
var preBuildCmds = []string{
|
||||||
|
"apt update",
|
||||||
|
}
|
||||||
|
|
||||||
|
// postBuildCmds run after the user recipe to clean up caches and reduce image size.
|
||||||
|
var postBuildCmds = []string{
|
||||||
|
"apt clean",
|
||||||
|
"apt autoremove -y",
|
||||||
|
"rm -rf /var/lib/apt/lists/*",
|
||||||
|
}
|
||||||
|
|
||||||
// buildAgentClient is the subset of the host agent client used by the build worker.
|
// buildAgentClient is the subset of the host agent client used by the build worker.
|
||||||
type buildAgentClient interface {
|
type buildAgentClient interface {
|
||||||
CreateSandbox(ctx context.Context, req *connect.Request[pb.CreateSandboxRequest]) (*connect.Response[pb.CreateSandboxResponse], error)
|
CreateSandbox(ctx context.Context, req *connect.Request[pb.CreateSandboxRequest]) (*connect.Response[pb.CreateSandboxResponse], error)
|
||||||
@ -91,7 +103,7 @@ func (s *BuildService) Create(ctx context.Context, p BuildCreateParams) (db.Temp
|
|||||||
Healthcheck: p.Healthcheck,
|
Healthcheck: p.Healthcheck,
|
||||||
Vcpus: p.VCPUs,
|
Vcpus: p.VCPUs,
|
||||||
MemoryMb: p.MemoryMB,
|
MemoryMb: p.MemoryMB,
|
||||||
TotalSteps: int32(len(p.Recipe)),
|
TotalSteps: int32(len(p.Recipe) + len(preBuildCmds) + len(postBuildCmds)),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return db.TemplateBuild{}, fmt.Errorf("insert build: %w", err)
|
return db.TemplateBuild{}, fmt.Errorf("insert build: %w", err)
|
||||||
@ -170,13 +182,18 @@ func (s *BuildService) executeBuild(ctx context.Context, buildIDStr string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse recipe.
|
// Parse user recipe and wrap with pre/post build stages.
|
||||||
var recipe []string
|
var userRecipe []string
|
||||||
if err := json.Unmarshal(build.Recipe, &recipe); err != nil {
|
if err := json.Unmarshal(build.Recipe, &userRecipe); err != nil {
|
||||||
s.failBuild(ctx, buildID, fmt.Sprintf("invalid recipe JSON: %v", err))
|
s.failBuild(ctx, buildID, fmt.Sprintf("invalid recipe JSON: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
recipe := make([]string, 0, len(userRecipe)+len(preBuildCmds)+len(postBuildCmds))
|
||||||
|
recipe = append(recipe, preBuildCmds...)
|
||||||
|
recipe = append(recipe, userRecipe...)
|
||||||
|
recipe = append(recipe, postBuildCmds...)
|
||||||
|
|
||||||
// Pick a platform host and create a sandbox.
|
// Pick a platform host and create a sandbox.
|
||||||
host, err := s.Scheduler.SelectHost(ctx, id.PlatformTeamID, false)
|
host, err := s.Scheduler.SelectHost(ctx, id.PlatformTeamID, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user