From 7e8d0708111125b11166494dc22da6d67f9e83e7 Mon Sep 17 00:00:00 2001 From: ChristopherHX Date: Tue, 10 Jan 2023 23:08:57 +0100 Subject: [PATCH] feat: Allow building without docker support (#1507) * feat: Allow building without docker support * fix macos build tag Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- pkg/container/container_types.go | 70 ++++++++++++++++++++++++++++++++ pkg/container/docker_auth.go | 2 + pkg/container/docker_build.go | 10 +---- pkg/container/docker_cli.go | 2 + pkg/container/docker_images.go | 2 + pkg/container/docker_logger.go | 2 + pkg/container/docker_pull.go | 11 +---- pkg/container/docker_run.go | 47 +-------------------- pkg/container/docker_stub.go | 57 ++++++++++++++++++++++++++ pkg/container/docker_volume.go | 2 + 10 files changed, 143 insertions(+), 62 deletions(-) create mode 100644 pkg/container/container_types.go create mode 100644 pkg/container/docker_stub.go diff --git a/pkg/container/container_types.go b/pkg/container/container_types.go new file mode 100644 index 0000000..c83ec35 --- /dev/null +++ b/pkg/container/container_types.go @@ -0,0 +1,70 @@ +package container + +import ( + "context" + "io" + + "github.com/nektos/act/pkg/common" +) + +// NewContainerInput the input for the New function +type NewContainerInput struct { + Image string + Username string + Password string + Entrypoint []string + Cmd []string + WorkingDir string + Env []string + Binds []string + Mounts map[string]string + Name string + Stdout io.Writer + Stderr io.Writer + NetworkMode string + Privileged bool + UsernsMode string + Platform string + Options string +} + +// FileEntry is a file to copy to a container +type FileEntry struct { + Name string + Mode int64 + Body string +} + +// Container for managing docker run containers +type Container interface { + Create(capAdd []string, capDrop []string) common.Executor + Copy(destPath string, files ...*FileEntry) common.Executor + CopyDir(destPath string, srcPath string, useGitIgnore bool) common.Executor + GetContainerArchive(ctx context.Context, srcPath string) (io.ReadCloser, error) + Pull(forcePull bool) common.Executor + Start(attach bool) common.Executor + Exec(command []string, env map[string]string, user, workdir string) common.Executor + UpdateFromEnv(srcPath string, env *map[string]string) common.Executor + UpdateFromImageEnv(env *map[string]string) common.Executor + UpdateFromPath(env *map[string]string) common.Executor + Remove() common.Executor + Close() common.Executor + ReplaceLogWriter(io.Writer, io.Writer) (io.Writer, io.Writer) +} + +// NewDockerBuildExecutorInput the input for the NewDockerBuildExecutor function +type NewDockerBuildExecutorInput struct { + ContextDir string + Container Container + ImageTag string + Platform string +} + +// NewDockerPullExecutorInput the input for the NewDockerPullExecutor function +type NewDockerPullExecutorInput struct { + Image string + ForcePull bool + Platform string + Username string + Password string +} diff --git a/pkg/container/docker_auth.go b/pkg/container/docker_auth.go index 7d2fc4a..466c003 100644 --- a/pkg/container/docker_auth.go +++ b/pkg/container/docker_auth.go @@ -1,3 +1,5 @@ +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows)) + package container import ( diff --git a/pkg/container/docker_build.go b/pkg/container/docker_build.go index 17e2c7b..2c972f4 100644 --- a/pkg/container/docker_build.go +++ b/pkg/container/docker_build.go @@ -1,3 +1,5 @@ +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows)) + package container import ( @@ -16,14 +18,6 @@ import ( "github.com/nektos/act/pkg/common" ) -// NewDockerBuildExecutorInput the input for the NewDockerBuildExecutor function -type NewDockerBuildExecutorInput struct { - ContextDir string - Container Container - ImageTag string - Platform string -} - // NewDockerBuildExecutor function to create a run executor for the container func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor { return func(ctx context.Context) error { diff --git a/pkg/container/docker_cli.go b/pkg/container/docker_cli.go index 60c9fe8..a1481c3 100644 --- a/pkg/container/docker_cli.go +++ b/pkg/container/docker_cli.go @@ -1,3 +1,5 @@ +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows)) + // This file is exact copy of https://github.com/docker/cli/blob/9ac8584acfd501c3f4da0e845e3a40ed15c85041/cli/command/container/opts.go // appended with license information. // diff --git a/pkg/container/docker_images.go b/pkg/container/docker_images.go index e23699e..676d4ce 100644 --- a/pkg/container/docker_images.go +++ b/pkg/container/docker_images.go @@ -1,3 +1,5 @@ +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows)) + package container import ( diff --git a/pkg/container/docker_logger.go b/pkg/container/docker_logger.go index b6b2f15..f2c21e6 100644 --- a/pkg/container/docker_logger.go +++ b/pkg/container/docker_logger.go @@ -1,3 +1,5 @@ +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows)) + package container import ( diff --git a/pkg/container/docker_pull.go b/pkg/container/docker_pull.go index 1eb04e1..75bfed1 100644 --- a/pkg/container/docker_pull.go +++ b/pkg/container/docker_pull.go @@ -1,3 +1,5 @@ +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows)) + package container import ( @@ -12,15 +14,6 @@ import ( "github.com/nektos/act/pkg/common" ) -// NewDockerPullExecutorInput the input for the NewDockerPullExecutor function -type NewDockerPullExecutorInput struct { - Image string - ForcePull bool - Platform string - Username string - Password string -} - // NewDockerPullExecutor function to create a run executor for the container func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor { return func(ctx context.Context) error { diff --git a/pkg/container/docker_run.go b/pkg/container/docker_run.go index 8e30a80..4ef6860 100644 --- a/pkg/container/docker_run.go +++ b/pkg/container/docker_run.go @@ -1,3 +1,5 @@ +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows)) + package container import ( @@ -38,51 +40,6 @@ import ( "github.com/nektos/act/pkg/common" ) -// NewContainerInput the input for the New function -type NewContainerInput struct { - Image string - Username string - Password string - Entrypoint []string - Cmd []string - WorkingDir string - Env []string - Binds []string - Mounts map[string]string - Name string - Stdout io.Writer - Stderr io.Writer - NetworkMode string - Privileged bool - UsernsMode string - Platform string - Options string -} - -// FileEntry is a file to copy to a container -type FileEntry struct { - Name string - Mode int64 - Body string -} - -// Container for managing docker run containers -type Container interface { - Create(capAdd []string, capDrop []string) common.Executor - Copy(destPath string, files ...*FileEntry) common.Executor - CopyDir(destPath string, srcPath string, useGitIgnore bool) common.Executor - GetContainerArchive(ctx context.Context, srcPath string) (io.ReadCloser, error) - Pull(forcePull bool) common.Executor - Start(attach bool) common.Executor - Exec(command []string, env map[string]string, user, workdir string) common.Executor - UpdateFromEnv(srcPath string, env *map[string]string) common.Executor - UpdateFromImageEnv(env *map[string]string) common.Executor - UpdateFromPath(env *map[string]string) common.Executor - Remove() common.Executor - Close() common.Executor - ReplaceLogWriter(io.Writer, io.Writer) (io.Writer, io.Writer) -} - // NewContainer creates a reference to a container func NewContainer(input *NewContainerInput) ExecutionsEnvironment { cr := new(containerReference) diff --git a/pkg/container/docker_stub.go b/pkg/container/docker_stub.go new file mode 100644 index 0000000..b28c90d --- /dev/null +++ b/pkg/container/docker_stub.go @@ -0,0 +1,57 @@ +//go:build WITHOUT_DOCKER || !(linux || darwin || windows) + +package container + +import ( + "context" + "runtime" + + "github.com/docker/docker/api/types" + "github.com/nektos/act/pkg/common" + "github.com/pkg/errors" +) + +// ImageExistsLocally returns a boolean indicating if an image with the +// requested name, tag and architecture exists in the local docker image store +func ImageExistsLocally(ctx context.Context, imageName string, platform string) (bool, error) { + return false, errors.New("Unsupported Operation") +} + +// RemoveImage removes image from local store, the function is used to run different +// container image architectures +func RemoveImage(ctx context.Context, imageName string, force bool, pruneChildren bool) (bool, error) { + return false, errors.New("Unsupported Operation") +} + +// NewDockerBuildExecutor function to create a run executor for the container +func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor { + return func(ctx context.Context) error { + return errors.New("Unsupported Operation") + } +} + +// NewDockerPullExecutor function to create a run executor for the container +func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor { + return func(ctx context.Context) error { + return errors.New("Unsupported Operation") + } +} + +// NewContainer creates a reference to a container +func NewContainer(input *NewContainerInput) ExecutionsEnvironment { + return nil +} + +func RunnerArch(ctx context.Context) string { + return runtime.GOOS +} + +func GetHostInfo(ctx context.Context) (info types.Info, err error) { + return types.Info{}, nil +} + +func NewDockerVolumeRemoveExecutor(volume string, force bool) common.Executor { + return func(ctx context.Context) error { + return nil + } +} diff --git a/pkg/container/docker_volume.go b/pkg/container/docker_volume.go index 5a6d476..6eafd33 100644 --- a/pkg/container/docker_volume.go +++ b/pkg/container/docker_volume.go @@ -1,3 +1,5 @@ +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows)) + package container import (