Skip to content

Commit aca4649

Browse files
authored
Compile and attach service configs when exporting docker image (#2208)
1 parent 38f6fe0 commit aca4649

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
/encore
44
/git-remote-encore
55
/target
6+
/__debug_*
67

78
# Don't commit dotfiles
89
/.encore
910
/.vscode
11+
/.zed
1012

1113
# Build artifact that must be placed alongside go files for Windows
1214
*.syso

cli/daemon/export/export.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package export
22

33
import (
44
"context"
5+
"encoding/base64"
56
"encoding/json"
67
"fmt"
78
"path/filepath"
9+
"strings"
810
"time"
911

1012
"github.com/cockroachdb/errors"
@@ -76,21 +78,6 @@ func Docker(ctx context.Context, app *apps.Instance, req *daemonpb.ExportRequest
7678
return false, errors.Wrap(err, "cache metadata")
7779
}
7880

79-
// Validate the service configs.
80-
_, err = bld.ServiceConfigs(ctx, builder.ServiceConfigsParams{
81-
Parse: parse,
82-
CueMeta: &cueutil.Meta{
83-
// Dummy data to satisfy config validation.
84-
APIBaseURL: "http://localhost:0",
85-
EnvName: "encore-eject",
86-
EnvType: cueutil.EnvType_Development,
87-
CloudType: cueutil.CloudType_Local,
88-
},
89-
})
90-
if err != nil {
91-
return false, err
92-
}
93-
9481
log.Info().Msgf("compiling Encore application for %s/%s", req.Goos, req.Goarch)
9582
result, err := bld.Compile(ctx, builder.CompileParams{
9683
Build: buildInfo,
@@ -192,6 +179,28 @@ func Docker(ctx context.Context, app *apps.Instance, req *daemonpb.ExportRequest
192179
}
193180
spec.WriteFiles[defaultInfraConfigPath] = data
194181
spec.Env = append(spec.Env, fmt.Sprintf("ENCORE_INFRA_CONFIG_PATH=%s", defaultInfraConfigPath))
182+
183+
// Validate the service configs.
184+
cfgs, err := bld.ServiceConfigs(ctx, builder.ServiceConfigsParams{
185+
Parse: parse,
186+
CueMeta: &cueutil.Meta{
187+
APIBaseURL: cfg.Metadata.BaseURL,
188+
EnvName: cfg.Metadata.EnvName,
189+
EnvType: orDefault(cueutil.EnvType(cfg.Metadata.EnvType), "development"),
190+
CloudType: orDefault(cueutil.CloudType(cfg.Metadata.Cloud), "local"),
191+
},
192+
})
193+
if err != nil {
194+
return false, err
195+
}
196+
for svcName, cfgStr := range cfgs.Configs {
197+
spec.Env = append(spec.Env, fmt.Sprintf(
198+
"%s%s=%s",
199+
"ENCORE_CFG_",
200+
strings.ToUpper(svcName),
201+
base64.RawURLEncoding.EncodeToString([]byte(cfgStr)),
202+
))
203+
}
195204
}
196205
var baseImgOverride option.Option[v1.Image]
197206
if params.BaseImageTag != "" {
@@ -253,6 +262,14 @@ func Docker(ctx context.Context, app *apps.Instance, req *daemonpb.ExportRequest
253262
return true, nil
254263
}
255264

265+
func orDefault[T comparable](value T, defaultValue T) T {
266+
var zero T
267+
if value == zero {
268+
return defaultValue
269+
}
270+
return value
271+
}
272+
256273
func resolveBaseImage(ctx context.Context, log zerolog.Logger, p *daemonpb.DockerExportParams, spec *dockerbuild.ImageSpec) (v1.Image, error) {
257274
baseImgTag := p.BaseImageTag
258275
if baseImgTag == "" || baseImgTag == "scratch" {

v2/v2builder/v2builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func (*BuilderImpl) ServiceConfigs(ctx context.Context, p builder.ServiceConfigs
221221

222222
pd := p.Parse.Data.(*parseData)
223223
cfg := computeConfigs(pd.pc.Errs, pd.appDesc, pd.mainModule, p.CueMeta)
224-
if err != nil {
224+
if err := pd.pc.Errs.AsError(); err != nil {
225225
return nil, err
226226
}
227227
return &builder.ServiceConfigsResult{

0 commit comments

Comments
 (0)