Skip to content

Commit

Permalink
[go] Support "nested" Go main builds
Browse files Browse the repository at this point in the history
  • Loading branch information
csweichel committed Dec 7, 2022
1 parent 346bec2 commit 7858d75
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ config:
buildFlags: []
# Command that's executed to lint the code
lintCommand: ["golangci-lint", "run"]
# GoMod can point to a go.mod file outside the component root. Leeway expects a go.sum alongside the go.mod.
goMod: "../go.mod"
```
### Yarn packages
Expand Down Expand Up @@ -173,6 +175,14 @@ config:
- gitpod/leeway:${__pkg_version}
```
The first image name of each Docker dependency which pushed an image will result in a build argument. This mechanism enables a package to build the base image for another one, by using the build argument as `FROM` value.
The name of this build argument is the package name of the dependency, transformed as follows:
- `/` is replaced with `_`
- `:` is replaced with `__`
- all uppercase.

E.g. `component/nested:docker` becomes `COMPONENT_NESTED__DOCKER`.

### Generic packages
```YAML
config:
Expand Down
2 changes: 1 addition & 1 deletion pkg/leeway/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ func (p *Package) buildGo(buildctx *buildContext, wd, result string) (res *packa
return nil, xerrors.Errorf("package should have Go config")
}

if _, err := os.Stat(filepath.Join(p.C.Origin, "go.mod")); os.IsNotExist(err) {
if _, err := os.Stat(filepath.Join(wd, "go.mod")); os.IsNotExist(err) {
return nil, xerrors.Errorf("can only build Go modules (missing go.mod file)")
}

Expand Down
23 changes: 21 additions & 2 deletions pkg/leeway/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ type GoPkgConfig struct {
BuildCommand []string `yaml:"buildCommand,omitempty"`
LintCommand []string `yaml:"lintCommand,omitempty"`
GoVersion string `yaml:"goVersion,omitempty"`
GoMod string `yaml:"goMod,omitempty"`
}

// Validate ensures this config can be acted upon/is valid
Expand All @@ -495,6 +496,15 @@ func (cfg GoPkgConfig) Validate() error {
}
}

if cfg.GoMod != "" {
if filepath.IsAbs(cfg.GoMod) {
return xerrors.Errorf("goMod must be relative to the component root")
}
if !strings.HasSuffix(cfg.GoMod, ".mod") {
return xerrors.Errorf("goMod must end with .mod")
}
}

return nil
}

Expand All @@ -510,12 +520,21 @@ const (

// AdditionalSources returns a list of unresolved sources coming in through this configuration
func (cfg GoPkgConfig) AdditionalSources(workspaceOrigin string) []string {
var res []string

fn := filepath.Join(workspaceOrigin, "go.work")
if _, err := os.Stat(fn); err == nil {
return []string{fn}
res = append(res, fn)
}

return []string{}
if cfg.GoMod != "" {
res = append(res,
cfg.GoMod,
strings.TrimSuffix(cfg.GoMod, ".mod")+".sum",
)
}

return res
}

// DockerPkgConfig configures a Docker package
Expand Down
2 changes: 1 addition & 1 deletion pkg/leeway/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ func loadComponent(ctx context.Context, workspace *Workspace, path string, args
}

if _, err := os.Stat(fn); os.IsNotExist(err) {
return comp, xerrors.Errorf("%s: %w", comp.Name, err)
return comp, xerrors.Errorf("cannot find additional source for %s: %w", comp.Name, err)
}
if _, found := completeSources[fn]; found {
continue
Expand Down

0 comments on commit 7858d75

Please sign in to comment.