Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for GHA services #775

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions pkg/container/docker_network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package container

import (
"context"

"github.com/docker/docker/api/types"
"github.com/nektos/act/pkg/common"
)

func NewDockerNetworkCreateExecutor(name string, config types.NetworkCreate) common.Executor {
return func(ctx context.Context) error {
if common.Dryrun(ctx) {
return nil
}

cli, err := GetDockerClient(ctx)
if err != nil {
return err
}

if exists := DockerNetworkExists(ctx, name); exists {
return nil
}

if _, err = cli.NetworkCreate(ctx, name, config); err != nil {
return err
}

return nil
}
}

func NewDockerNetworkRemoveExecutor(name string) common.Executor {
return func(ctx context.Context) error {
if common.Dryrun(ctx) {
return nil
}

cli, err := GetDockerClient(ctx)
if err != nil {
return err
}

if err = cli.NetworkRemove(ctx, name); err != nil {
return err
}

return nil
}
}

func DockerNetworkExists(ctx context.Context, name string) bool {
if _, exists, _ := GetDockerNetwork(ctx, name); !exists {
return false
}
return true
}

func GetDockerNetwork(ctx context.Context, name string) (types.NetworkResource, bool, error) {
cli, err := GetDockerClient(ctx)
if err != nil {
return types.NetworkResource{}, false, err
}

res, err := cli.NetworkInspect(ctx, name, types.NetworkInspectOptions{})
if err != nil {
return types.NetworkResource{}, false, err
}

return res, true, nil
}
107 changes: 83 additions & 24 deletions pkg/container/docker_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ type FileEntry struct {

// Container for managing docker run containers
type Container interface {
ID() string
Create(capAdd []string, capDrop []string) common.Executor
ConnectToNetwork(name 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)
Expand All @@ -77,6 +79,7 @@ type Container interface {
UpdateFromPath(env *map[string]string) common.Executor
Remove() common.Executor
Close() common.Executor
SetContainerNetworkMode(mode string) common.Executor
}

// NewContainer creates a reference to a container
Expand Down Expand Up @@ -105,6 +108,10 @@ func supportsContainerImagePlatform(cli *client.Client) bool {
return constraint.Check(sv)
}

func (cr *containerReference) ID() string {
return cr.id
}

func (cr *containerReference) Create(capAdd []string, capDrop []string) common.Executor {
return common.
NewInfoExecutor("%sdocker create image=%s platform=%s entrypoint=%+q cmd=%+q", logPrefix, cr.input.Image, cr.input.Platform, cr.input.Entrypoint, cr.input.Cmd).
Expand Down Expand Up @@ -146,19 +153,26 @@ func (cr *containerReference) Pull(forcePull bool) common.Executor {
}

func (cr *containerReference) Copy(destPath string, files ...*FileEntry) common.Executor {
return common.NewPipelineExecutor(
cr.connect(),
cr.find(),
cr.copyContent(destPath, files...),
).IfNot(common.Dryrun)
return common.
NewInfoExecutor("%sdocker cp destination=%s", logPrefix, destPath).
Then(
common.NewPipelineExecutor(
cr.connect(),
cr.find(),
cr.copyContent(destPath, files...),
).IfNot(common.Dryrun),
)
}

func (cr *containerReference) CopyDir(destPath string, srcPath string, useGitIgnore bool) common.Executor {
return common.NewPipelineExecutor(
common.NewInfoExecutor("%sdocker cp src=%s dst=%s", logPrefix, srcPath, destPath),
cr.Exec([]string{"mkdir", "-p", destPath}, nil, "", ""),
cr.copyDir(destPath, srcPath, useGitIgnore),
).IfNot(common.Dryrun)
return common.
NewInfoExecutor("%sdocker cp src=%s dst=%s", logPrefix, srcPath, destPath).
Then(
common.NewPipelineExecutor(
cr.Exec([]string{"mkdir", "-p", destPath}, nil, "", ""),
cr.copyDir(destPath, srcPath, useGitIgnore),
).IfNot(common.Dryrun),
)
}

func (cr *containerReference) GetContainerArchive(ctx context.Context, srcPath string) (io.ReadCloser, error) {
Expand All @@ -178,22 +192,54 @@ func (cr *containerReference) UpdateFromPath(env *map[string]string) common.Exec
return cr.extractPath(env).IfNot(common.Dryrun)
}

func (cr *containerReference) SetContainerNetworkMode(mode string) common.Executor {
return common.
NewDebugExecutor("Changed network mode for container '%s' from '%s' to '%s'", cr.input.Name, cr.input.NetworkMode, mode).
Then(
common.NewPipelineExecutor(
func(ctx context.Context) error {
cr.input.NetworkMode = mode
return nil
},
).IfNot(common.Dryrun),
)
}

func (cr *containerReference) ConnectToNetwork(name string) common.Executor {
return common.
NewInfoExecutor("%sdocker network connect net=%s container=%s", logPrefix, name, cr.input.Name).
Then(
common.NewPipelineExecutor(
cr.connect(),
cr.find(),
cr.connectToNetwork(name),
).IfNot(common.Dryrun),
)
}

func (cr *containerReference) Exec(command []string, env map[string]string, user, workdir string) common.Executor {
return common.NewPipelineExecutor(
common.NewInfoExecutor("%sdocker exec cmd=[%s] user=%s workdir=%s", logPrefix, strings.Join(command, " "), user, workdir),
cr.connect(),
cr.find(),
cr.exec(command, env, user, workdir),
).IfNot(common.Dryrun)
return common.
NewInfoExecutor("%sdocker exec cmd=%v user=%s workdir=%s", logPrefix, command, user, workdir).
Then(
common.NewPipelineExecutor(
cr.connect(),
cr.find(),
cr.exec(command, env, user, workdir),
).IfNot(common.Dryrun),
)
}

func (cr *containerReference) Remove() common.Executor {
return common.NewPipelineExecutor(
cr.connect(),
cr.find(),
).Finally(
cr.remove(),
).IfNot(common.Dryrun)
return common.
NewInfoExecutor("%sdocker rm %s", logPrefix, cr.id).
Then(
common.NewPipelineExecutor(
cr.connect(),
cr.find(),
).Finally(
cr.remove(),
).IfNot(common.Dryrun),
).IfBool(cr.id != "")
}

type containerReference struct {
Expand Down Expand Up @@ -233,6 +279,12 @@ func GetDockerClient(ctx context.Context) (*client.Client, error) {
return cli, err
}

func (cr *containerReference) connectToNetwork(name string) common.Executor {
return func(ctx context.Context) error {
return cr.cli.NetworkConnect(ctx, name, cr.id, nil)
}
}

func (cr *containerReference) connect() common.Executor {
return func(ctx context.Context) error {
if cr.cli != nil {
Expand Down Expand Up @@ -315,14 +367,20 @@ func (cr *containerReference) create(capAdd []string, capDrop []string) common.E

config := &container.Config{
Image: input.Image,
Cmd: input.Cmd,
Entrypoint: input.Entrypoint,
WorkingDir: input.WorkingDir,
Env: input.Env,
Tty: isTerminal,
Hostname: input.Hostname,
}

if len(input.Cmd) > 0 {
config.Cmd = input.Cmd
}

if len(input.Entrypoint) > 0 {
config.Entrypoint = input.Entrypoint
}

mounts := make([]mount.Mount, 0)
for mountSource, mountTarget := range input.Mounts {
mounts = append(mounts, mount.Mount{
Expand Down Expand Up @@ -357,6 +415,7 @@ func (cr *containerReference) create(capAdd []string, capDrop []string) common.E
if err != nil {
return errors.WithStack(err)
catthehacker marked this conversation as resolved.
Show resolved Hide resolved
}

logger.Debugf("Created container name=%s id=%v from image %v (platform: %s)", input.Name, resp.ID, input.Image, input.Platform)
logger.Debugf("ENV ==> %v", input.Env)

Expand Down
Loading