diff --git a/act/container/docker_build.go b/act/container/docker_build.go index c108da73..1c840606 100644 --- a/act/container/docker_build.go +++ b/act/container/docker_build.go @@ -8,7 +8,6 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/builder/dockerignore" - "github.com/docker/docker/client" "github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/fileutils" "github.com/nektos/act/pkg/common" @@ -30,11 +29,10 @@ func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor { return nil } - cli, err := client.NewClientWithOpts(client.FromEnv) + cli, err := GetDockerClient(ctx) if err != nil { return err } - cli.NegotiateAPIVersion(ctx) logger.Debugf("Building image from '%v'", input.ContextDir) diff --git a/act/container/docker_images.go b/act/container/docker_images.go index 2cb298ca..efe267ac 100644 --- a/act/container/docker_images.go +++ b/act/container/docker_images.go @@ -5,17 +5,15 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" - "github.com/docker/docker/client" ) // ImageExistsLocally returns a boolean indicating if an image with the // requested name (and tag) exist in the local docker image store func ImageExistsLocally(ctx context.Context, imageName string) (bool, error) { - cli, err := client.NewClientWithOpts(client.FromEnv) + cli, err := GetDockerClient(ctx) if err != nil { return false, err } - cli.NegotiateAPIVersion(ctx) filters := filters.NewArgs() filters.Add("reference", imageName) diff --git a/act/container/docker_pull.go b/act/container/docker_pull.go index 13fdaa85..6a01856e 100644 --- a/act/container/docker_pull.go +++ b/act/container/docker_pull.go @@ -6,7 +6,6 @@ import ( "strings" "github.com/docker/docker/api/types" - "github.com/docker/docker/client" "github.com/nektos/act/pkg/common" "github.com/pkg/errors" log "github.com/sirupsen/logrus" @@ -48,11 +47,10 @@ func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor { imageRef := cleanImage(input.Image) logger.Debugf("pulling image '%v'", imageRef) - cli, err := client.NewClientWithOpts(client.FromEnv) + cli, err := GetDockerClient(ctx) if err != nil { return err } - cli.NegotiateAPIVersion(ctx) reader, err := cli.ImagePull(ctx, imageRef, types.ImagePullOptions{}) _ = logDockerResponse(logger, reader, err != nil) diff --git a/act/container/docker_run.go b/act/container/docker_run.go index 8a537264..69731bf0 100644 --- a/act/container/docker_run.go +++ b/act/container/docker_run.go @@ -15,6 +15,7 @@ import ( "github.com/go-git/go-billy/v5/osfs" "github.com/go-git/go-git/v5/plumbing/format/gitignore" + "github.com/docker/cli/cli/connhelper" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/mount" @@ -137,16 +138,46 @@ type containerReference struct { input *NewContainerInput } +func GetDockerClient(ctx context.Context) (*client.Client, error) { + var err error + var cli *client.Client + + // TODO: this should maybe need to be a global option, not hidden in here? + // though i'm not sure how that works out when there's another Executor :D + // I really would like something that works on OSX native for eg + dockerHost := os.Getenv("DOCKER_HOST") + + if strings.HasPrefix(dockerHost, "ssh://") { + var helper *connhelper.ConnectionHelper + + helper, err = connhelper.GetConnectionHelper(dockerHost) + if err != nil { + return nil, err + } + cli, err = client.NewClientWithOpts( + client.WithHost(helper.Host), + client.WithDialContext(helper.Dialer), + ) + } else { + cli, err = client.NewClientWithOpts(client.FromEnv) + } + if err != nil { + return nil, errors.WithStack(err) + } + cli.NegotiateAPIVersion(ctx) + + return cli, err +} + func (cr *containerReference) connect() common.Executor { return func(ctx context.Context) error { if cr.cli != nil { return nil } - cli, err := client.NewClientWithOpts(client.FromEnv) + cli, err := GetDockerClient(ctx) if err != nil { - return errors.WithStack(err) + return err } - cli.NegotiateAPIVersion(ctx) cr.cli = cli return nil } diff --git a/act/container/docker_volume.go b/act/container/docker_volume.go index a0b533f5..787a49a6 100644 --- a/act/container/docker_volume.go +++ b/act/container/docker_volume.go @@ -3,7 +3,6 @@ package container import ( "context" - "github.com/docker/docker/client" "github.com/nektos/act/pkg/common" ) @@ -17,11 +16,10 @@ func NewDockerVolumeRemoveExecutor(volume string, force bool) common.Executor { return nil } - cli, err := client.NewClientWithOpts(client.FromEnv) + cli, err := GetDockerClient(ctx) if err != nil { return err } - cli.NegotiateAPIVersion(ctx) return cli.VolumeRemove(ctx, volume, force) }