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

Fix issues when running some docker containers #66

Merged
merged 3 commits into from
Feb 28, 2024
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v2.0.0-alpha.16]
- Add `stage.*.container.skip_workspace` boolean parameter to skip mounting the current working directory when using the docker plugin
- Do not pass the PATH and host environment variables to the child docker container

## [v2.0.0-alpha.14]
- Fixes `depends_on` not being respected on modules.

Expand Down
26 changes: 13 additions & 13 deletions examples/terraform/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 29 additions & 24 deletions internal/ci/stage_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func (s *Stage) run(conductor *Conductor, evalCtx *hcl.EvalContext, options ...r

envStrings := s.processEnvironmentVariables(conductor, environment, cfg, tmpDir, paramsGo)

cmd, d := s.parseExecCommand(conductor, evalCtx, cfg, envStrings, stream)
cmd, d := s.parseExecCommand(conductor, evalCtx, cfg, stream)
diags.Extend(d)
if diags.HasErrors() {
return diags.Diagnostics()
Expand All @@ -507,6 +507,7 @@ func (s *Stage) run(conductor *Conductor, evalCtx *hcl.EvalContext, options ...r
logger.Tracef("script: %.30s... ", cmd.String())

if s.Container == nil {
cmd.Env = append(os.Environ(), envStrings...)
s.process = cmd
logger.Tracef("running command: %.30s...", cmd.String())
if !cfg.Behavior.DryRun {
Expand All @@ -520,6 +521,7 @@ func (s *Stage) run(conductor *Conductor, evalCtx *hcl.EvalContext, options ...r
fmt.Println(cmd.String())
}
} else {
cmd.Env = envStrings
d := s.executeDocker(conductor, evalCtx, cmd, cfg)
diags.Extend(d)
}
Expand Down Expand Up @@ -587,8 +589,9 @@ func (s *Stage) executeDocker(conductor *Conductor, evalCtx *hcl.EvalContext, cm
}

logger.Trace("parsing container arguments")
binds := []string{
fmt.Sprintf("%s:/workspace", cmd.Dir),
var binds []string
if !s.Container.SkipWorkspace {
binds = append(binds, fmt.Sprintf("%s:/workspace", cmd.Dir))
}

logger.Trace("parsing container volumes")
Expand All @@ -614,11 +617,11 @@ func (s *Stage) executeDocker(conductor *Conductor, evalCtx *hcl.EvalContext, cm

logger.Trace("dry run check")
if cfg.Behavior.DryRun {
fmt.Println(ui.Blue("docker:run.image"), ui.Green(image))
fmt.Println(ui.Blue("docker:run.workdir"), ui.Green("/workspace"))
fmt.Println(ui.Blue("docker:run.volume"), ui.Green(cmd.Dir+":/workspace"))
fmt.Println(ui.Blue("docker:run.stdin"), ui.Green(s.Container.Stdin))
fmt.Println(ui.Blue("docker:run.args"), ui.Green(cmd.String()))
fmt.Println(ui.Blue("# docker:run.image"), ui.Green(image))
fmt.Println(ui.Blue("# docker:run.workdir"), ui.Green("/workspace"))
fmt.Println(ui.Blue("# docker:run.volume"), ui.Green(cmd.Dir+":/workspace"))
fmt.Println(ui.Blue("# docker:run.stdin"), ui.Green(s.Container.Stdin))
fmt.Println(ui.Blue("# docker:run.args"), ui.Green(cmd.String()))
return diags
}

Expand All @@ -631,12 +634,9 @@ func (s *Stage) executeDocker(conductor *Conductor, evalCtx *hcl.EvalContext, cm

logger.Trace("creating container")
resp, err := cli.ContainerCreate(conductor.Context(), &dockerContainer.Config{
Image: image,
Cmd: cmd.Args,
WorkingDir: "/workspace",
Volumes: map[string]struct{}{
"/workspace": {},
},
Image: image,
WorkingDir: "/workspace",
Cmd: cmd.Args,
Tty: true,
AttachStdout: true,
AttachStderr: true,
Expand Down Expand Up @@ -766,7 +766,7 @@ func (s *Stage) parseEnvironmentVariables(conductor *Conductor, evalCtx *hcl.Eva
return environment, diags
}

func (s *Stage) parseExecCommand(conductor *Conductor, evalCtx *hcl.EvalContext, cfg *runnable.Config, envStrings []string, outputBuffer io.Writer) (*exec.Cmd, hcl.Diagnostics) {
func (s *Stage) parseExecCommand(conductor *Conductor, evalCtx *hcl.EvalContext, cfg *runnable.Config, outputBuffer io.Writer) (*exec.Cmd, hcl.Diagnostics) {
var diags hcl.Diagnostics
logger := conductor.Logger().WithField("stage", s.Id)

Expand Down Expand Up @@ -832,7 +832,6 @@ func (s *Stage) parseExecCommand(conductor *Conductor, evalCtx *hcl.EvalContext,
cmd := exec.CommandContext(conductor.Context(), cmdHcl.command, cmdHcl.args...)
cmd.Stdout = io.MultiWriter(logger.Writer(), outputBuffer)
cmd.Stderr = io.MultiWriter(logger.Writer(), outputBuffer)
cmd.Env = append(os.Environ(), envStrings...)
cmd.Dir = dir
return cmd, diags
}
Expand Down Expand Up @@ -970,23 +969,29 @@ func (s *Stage) hclEndpoint(conductor *Conductor, evalCtx *hcl.EvalContext) ([]s
conductor.Eval().Mutex().RUnlock()

var entrypoint []string

if d.HasErrors() {
diags = diags.Extend(d)
} else if entrypointRaw.IsNull() {
entrypoint = nil
} else if !entrypointRaw.CanIterateElements() {
return nil, diags
}

if entrypointRaw.IsNull() {
return nil, diags
}
if !entrypointRaw.CanIterateElements() {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "entrypoint must be a list of strings",
Detail: fmt.Sprintf("the provided entrypoint, was not recognized as a valid string. received entrypoint='''%s'''", entrypointRaw),
Subject: s.Container.Entrypoint.Range().Ptr(),
EvalContext: evalCtx,
})
} else {
v := entrypointRaw.AsValueSlice()
for _, e := range v {
entrypoint = append(entrypoint, e.AsString())
}
return nil, diags
}

v := entrypointRaw.AsValueSlice()
for _, e := range v {
entrypoint = append(entrypoint, e.AsString())
}
return entrypoint, diags
}
Expand Down
3 changes: 3 additions & 0 deletions internal/ci/stage_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type StageContainer struct {
// Volumes have a list of host path volume mapping which is bound on docker run
Volumes StageContainerVolumes `hcl:"volume,block" json:"volumes"`

// SkipWorkspace allows you to skip mounting the workspace directory
SkipWorkspace bool `hcl:"skip_workspace,optional" json:"skip_workspace"`

// Ports have a list of ports that needs to be exposed from the container
Ports StageContainerPorts `hcl:"port,block" json:"ports"`

Expand Down
Loading