Skip to content

Commit

Permalink
fix: use wrap script in order to let git work in container
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlJi committed Aug 27, 2024
1 parent ff50c34 commit f4e697d
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions internal/runner/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"strings"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
Expand Down Expand Up @@ -62,16 +63,23 @@ func (r *DockerRunner) Run(ctx context.Context, cfg *config.Linter) (io.ReadClos
var (
dockerConfig = &container.Config{
Image: cfg.DockerAsRunner,
Cmd: cfg.Args,
Env: cfg.Env,
Entrypoint: cfg.Command,
Entrypoint: []string{"/bin/sh", "-c"},
WorkingDir: cfg.WorkDir,
}
dockerHostConfig = &container.HostConfig{
Binds: []string{cfg.WorkDir + ":" + cfg.WorkDir},
}
)

// add git config so that git can work in the container
wrapperScript := fmt.Sprintf(`#!/bin/sh
git config --global --add safe.directory %s
%s %s
`, cfg.WorkDir, strings.Join(cfg.Command, " "), strings.Join(cfg.Args, " "))

dockerConfig.Cmd = []string{wrapperScript}

resp, err := r.cli.ContainerCreate(ctx, dockerConfig, dockerHostConfig, nil, nil, "")
if err != nil {
return nil, fmt.Errorf("failed to create container: %w", err)
Expand All @@ -82,11 +90,11 @@ func (r *DockerRunner) Run(ctx context.Context, cfg *config.Linter) (io.ReadClos
return nil, fmt.Errorf("failed to start container: %w", err)
}

options := container.LogsOptions{
logOptions := container.LogsOptions{
ShowStdout: true,
ShowStderr: true,
Follow: true,
Timestamps: false,
}
return r.cli.ContainerLogs(ctx, resp.ID, options)

return r.cli.ContainerLogs(ctx, resp.ID, logOptions)
}

0 comments on commit f4e697d

Please sign in to comment.