Skip to content

Commit

Permalink
feat(action): add option to build container from Dockerfile
Browse files Browse the repository at this point in the history
- very useful to locally test changes to Dockerfiles
  • Loading branch information
AtomicFS committed Sep 5, 2023
1 parent 288dd97 commit 02f284f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 24 deletions.
32 changes: 25 additions & 7 deletions action/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ type SetupOpts struct {
}

// Setup for setting up a Docker container via dagger
func Setup(ctx context.Context, client *dagger.Client, opts *SetupOpts) (*dagger.Container, error) {
// Make sure there is a non-empty URL or name provided
if opts.ContainerURL == "" {
return nil, errEmptyURL
}
func Setup(ctx context.Context, client *dagger.Client, opts *SetupOpts, dockerfileDirectoryPath string) (*dagger.Container, error) {
// dockerfileDirectoryPath allows to use Dockerfile and build locally,
// which is handy for testing changes to said Dockerfile without the need to
// have the container uploaded into package registry

// None of the directories can be empty string
for _, val := range []string{opts.MountContainerDir, opts.MountHostDir, opts.WorkdirContainer} {
Expand All @@ -46,8 +45,27 @@ func Setup(ctx context.Context, client *dagger.Client, opts *SetupOpts) (*dagger
return nil, errDirectoryInvalid
}

// Pull docker container
container := client.Container().From(opts.ContainerURL)
// Setup container either from URL or build from Dockerfile
var container *dagger.Container
if dockerfileDirectoryPath == "" {
// Use URL
fmt.Printf("Container setup: URL mode")

// Make sure there is a non-empty URL or name provided
if opts.ContainerURL == "" {
return nil, errEmptyURL
}

// Pull docker container
container = client.Container().From(opts.ContainerURL)
} else {
// Use Dockerfile
fmt.Printf("Container setup: Dockerfile mode")

container = client.Container().Build(
client.Host().Directory(dockerfileDirectoryPath),
)
}

// Mount repository into the container
return container.
Expand Down
10 changes: 5 additions & 5 deletions action/container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func TestSetup(t *testing.T) {
name: "empty URL",
opts: SetupOpts{
ContainerURL: "",
MountContainerDir: "",
MountHostDir: "",
WorkdirContainer: "",
MountContainerDir: "/src",
MountHostDir: ".",
WorkdirContainer: "/src",
},
wantErr: errEmptyURL,
lsContains: "",
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestSetup(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
container, err := Setup(ctx, client, &tc.opts)
container, err := Setup(ctx, client, &tc.opts, "")
assert.ErrorIs(t, err, tc.wantErr)
if err != nil {
// No need to continue on err
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestGetArtifacts(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
container, err := Setup(ctx, client, &opts)
container, err := Setup(ctx, client, &opts, "")
assert.NoError(t, err)

// Run commands in container
Expand Down
4 changes: 2 additions & 2 deletions action/recipes/coreboot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
)

// coreboot builds coreboot with all blobs and stuff
func coreboot(ctx context.Context, client *dagger.Client, common *commonOpts, opts *corebootOpts, artifacts *[]container.Artifacts) error {
func coreboot(ctx context.Context, client *dagger.Client, common *commonOpts, dockerfileDirectoryPath string, opts *corebootOpts, artifacts *[]container.Artifacts) error {
// TODO: get blobs in place!
_ = opts
envVars := map[string]string{}

return buildWithKernelBuildSystem(ctx, client, common, envVars, artifacts)
return buildWithKernelBuildSystem(ctx, client, common, dockerfileDirectoryPath, envVars, artifacts)
}
2 changes: 1 addition & 1 deletion action/recipes/coreboot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestCoreboot(t *testing.T) {
}

// Try to build coreboot
err = coreboot(ctx, client, &common, &corebootOpts, &artifacts)
err = coreboot(ctx, client, &common, "", &corebootOpts, &artifacts)
assert.NoError(t, err)

// Check artifacts
Expand Down
4 changes: 2 additions & 2 deletions action/recipes/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
var errUnknownArchCrossCompile = errors.New("unknown architecture for cross-compilation")

// linux builds linux kernel
func linux(ctx context.Context, client *dagger.Client, common *commonOpts, opts *linuxOpts, artifacts *[]container.Artifacts) error {
func linux(ctx context.Context, client *dagger.Client, common *commonOpts, dockerfileDirectoryPath string, opts *linuxOpts, artifacts *[]container.Artifacts) error {
// Not sure if there will be any linuxOpts
_ = opts

Expand All @@ -37,5 +37,5 @@ func linux(ctx context.Context, client *dagger.Client, common *commonOpts, opts
envVars["CROSS_COMPILE"] = val
}

return buildWithKernelBuildSystem(ctx, client, common, envVars, artifacts)
return buildWithKernelBuildSystem(ctx, client, common, dockerfileDirectoryPath, envVars, artifacts)
}
18 changes: 15 additions & 3 deletions action/recipes/linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ func TestLinux(t *testing.T) {
pwd, err := os.Getwd()
assert.NoError(t, err)

// Use "" if you want to test containers from github package registry
// Use "../../docker/linux" if you want to test containers built fresh from Dockerfile
dockerfilePath := ""
if true {
dockerfilePath, err = filepath.Abs("../../docker/linux")
assert.NoError(t, err)
}

testCases := []struct {
name string
linuxVersion string
Expand Down Expand Up @@ -120,11 +128,13 @@ func TestLinux(t *testing.T) {

// Copy over defconfig file into tmpDir/linux
defconfigPath := filepath.Join(common.repoPath, common.defconfigPath)
defconfigLocalPath, err := filepath.Abs(filepath.Join(pwd, fmt.Sprintf("../../tests/linux_%s/linux.defconfig", linuxVersion.String())))
// ^^^ this relative path might be funky
assert.NoError(t, err)
err = filesystem.CopyFile(
filepath.Join(pwd, fmt.Sprintf("../../tests/linux_%s/linux.defconfig", linuxVersion.String())),
defconfigLocalPath,
defconfigPath,
)
// ^^^ this relative path might be funky
assert.NoError(t, err)

// Artifacts
Expand All @@ -145,12 +155,14 @@ func TestLinux(t *testing.T) {
}

// Try to build linux kernel
err = linux(ctx, client, &common, &linuxOpts, &artifacts)
err = linux(ctx, client, &common, dockerfilePath, &linuxOpts, &artifacts)
assert.ErrorIs(t, err, tc.wantErr)

// Check artifacts
assert.ErrorIs(t, filesystem.CheckFileExists(filepath.Join(outputPath, "vmlinux")), os.ErrExist)
assert.ErrorIs(t, filesystem.CheckFileExists(filepath.Join(outputPath, "defconfig")), os.ErrExist)
assert.NoError(t, os.Chdir(pwd)) // just to make sure
})
}
assert.NoError(t, os.Chdir(pwd)) // just to make sure
}
8 changes: 4 additions & 4 deletions action/recipes/recipes.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func Execute(ctx context.Context, client *dagger.Client, action *githubactions.A
HostPath: common.outputDir,
},
}
return coreboot(ctx, client, &common, &opts, &artifacts)
return coreboot(ctx, client, &common, "", &opts, &artifacts)
case "linux":
opts, err := linuxGetOpts(action.GetInput)
if err != nil {
Expand All @@ -176,7 +176,7 @@ func Execute(ctx context.Context, client *dagger.Client, action *githubactions.A
HostPath: common.outputDir,
},
}
return linux(ctx, client, &common, &opts, &artifacts)
return linux(ctx, client, &common, "", &opts, &artifacts)
/*
case "edk2":
return edk2(ctx, action, client)
Expand All @@ -191,15 +191,15 @@ func Execute(ctx context.Context, client *dagger.Client, action *githubactions.A
// buildWithKernelBuildSystem is a generic function to build stuff with Kernel Build System
// usable for linux kernel and coreboot
// https://www.kernel.org/doc/html/latest/kbuild/index.html
func buildWithKernelBuildSystem(ctx context.Context, client *dagger.Client, common *commonOpts, envVars map[string]string, artifacts *[]container.Artifacts) error {
func buildWithKernelBuildSystem(ctx context.Context, client *dagger.Client, common *commonOpts, dockerfileDirectoryPath string, envVars map[string]string, artifacts *[]container.Artifacts) error {
// Spin up container
containerOpts := container.SetupOpts{
ContainerURL: common.sdkVersion,
MountContainerDir: common.containerWorkDir,
MountHostDir: common.repoPath,
WorkdirContainer: common.containerWorkDir,
}
myContainer, err := container.Setup(ctx, client, &containerOpts)
myContainer, err := container.Setup(ctx, client, &containerOpts, dockerfileDirectoryPath)
if err != nil {
return err
}
Expand Down

0 comments on commit 02f284f

Please sign in to comment.