-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.go
181 lines (158 loc) · 5.21 KB
/
build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package dotnetexecute
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"time"
"github.com/Masterminds/semver"
"github.com/Netflix/go-env"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/paketo-buildpacks/packit/v2/sbom"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
//go:generate faux --interface SBOMGenerator --output fakes/sbom_generator.go
type SBOMGenerator interface {
Generate(path string) (sbom.SBOM, error)
}
// Build will return a packit.BuildFunc that will be invoked during the build
// phase of the buildpack lifecycle.
//
// Build generates a SBOM of the .NET app's dependencies based on its compiled
// DLLs. It sets up the entrypoint for the app image and adds a helper that
// will determine at launch-time which container port the app should listen on.
func Build(
config Configuration,
buildpackYMLParser BuildpackConfigParser,
configParser ConfigParser,
sbomGenerator SBOMGenerator,
logger scribe.Emitter,
clock chronos.Clock,
) packit.BuildFunc {
return func(context packit.BuildContext) (packit.BuildResult, error) {
logger.Title("%s %s", context.BuildpackInfo.Name, context.BuildpackInfo.Version)
es, err := env.Marshal(&config)
if err != nil {
// not tested
return packit.BuildResult{}, fmt.Errorf("parsing build configuration: %w", err)
}
logger.Debug.Process("Build configuration:")
for envVar := range es {
// for bug https://github.com/Netflix/go-env/issues/23
if !strings.Contains(envVar, "=") {
logger.Debug.Subprocess("%s: %s", envVar, es[envVar])
}
}
logger.Debug.Break()
projectPath, err := buildpackYMLParser.ParseProjectPath(filepath.Join(context.WorkingDir, "buildpack.yml"))
if err != nil {
return packit.BuildResult{}, fmt.Errorf("error parsing buildpack.yml: %w", err)
}
if projectPath != "" {
nextMajorVersion := semver.MustParse(context.BuildpackInfo.Version).IncMajor()
logger.Subprocess("WARNING: Setting the project path through buildpack.yml will be deprecated soon in .NET Execute Buildpack v%s.", nextMajorVersion.String())
logger.Subprocess("Please specify the project path through the $BP_DOTNET_PROJECT_PATH environment variable instead. See README.md or the documentation on paketo.io for more information.")
}
runtimeConfig, err := configParser.Parse(filepath.Join(context.WorkingDir, "*.runtimeconfig.json"))
if err != nil {
return packit.BuildResult{}, fmt.Errorf("failed to find *.runtimeconfig.json: %w", err)
}
logger.GeneratingSBOM(context.WorkingDir)
var sbomContent sbom.SBOM
duration, err := clock.Measure(func() error {
sbomContent, err = sbomGenerator.Generate(context.WorkingDir)
return err
})
if err != nil {
return packit.BuildResult{}, err
}
logger.Action("Completed in %s", duration.Round(time.Millisecond))
logger.Break()
logger.FormattingSBOM(context.BuildpackInfo.SBOMFormats...)
sbomFormatter, err := sbomContent.InFormats(context.BuildpackInfo.SBOMFormats...)
if err != nil {
return packit.BuildResult{}, err
}
command := filepath.Join(context.WorkingDir, runtimeConfig.AppName)
var args []string
if !runtimeConfig.Executable {
_, err := os.Stat(filepath.Join(context.WorkingDir, fmt.Sprintf("%s.dll", runtimeConfig.AppName)))
if err != nil && !errors.Is(err, os.ErrNotExist) {
return packit.BuildResult{}, err
}
if errors.Is(err, os.ErrNotExist) {
return packit.BuildResult{}, fmt.Errorf("no entrypoint [%s.dll] found: %w ", runtimeConfig.AppName, err)
}
command = "dotnet"
args = append(args, fmt.Sprintf("%s.dll", filepath.Join(context.WorkingDir, runtimeConfig.AppName)))
}
processes := []packit.Process{
{
Type: runtimeConfig.AppName,
Command: command,
Args: args,
Default: true,
Direct: true,
},
}
if config.LiveReloadEnabled {
processes = []packit.Process{
{
Type: fmt.Sprintf("reload-%s", runtimeConfig.AppName),
Command: "watchexec",
Args: append([]string{
"--restart",
"--watch", context.WorkingDir,
"--shell", "none",
"--",
command,
}, args...),
Default: true,
Direct: true,
},
{
Type: runtimeConfig.AppName,
Command: command,
Args: args,
Direct: true,
},
}
err := filepath.Walk(context.WorkingDir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if path == context.WorkingDir {
return nil
}
return os.Chmod(path, info.Mode()|0060)
})
if err != nil {
return packit.BuildResult{}, err
}
}
logger.LaunchProcesses(processes)
portChooserLayer, err := context.Layers.Get("port-chooser")
if err != nil {
return packit.BuildResult{}, err
}
portChooserLayer.Launch = true
portChooserLayer.ExecD = []string{filepath.Join(context.CNBPath, "bin", "port-chooser")}
if config.DebugEnabled {
portChooserLayer.LaunchEnv.Default("ASPNETCORE_ENVIRONMENT", "Development")
}
logger.LayerFlags(portChooserLayer)
logger.EnvironmentVariables(portChooserLayer)
return packit.BuildResult{
Layers: []packit.Layer{
portChooserLayer,
},
Launch: packit.LaunchMetadata{
Processes: processes,
SBOM: sbomFormatter,
},
}, nil
}
}