Skip to content

Commit

Permalink
Append terraform binary dir to PATH when running custom commands
Browse files Browse the repository at this point in the history
  • Loading branch information
maximede committed Jun 13, 2019
1 parent 26c7df1 commit bb20ef0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
4 changes: 3 additions & 1 deletion server/events/runtime/run_step_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"os/exec"
"path/filepath"

version "github.com/hashicorp/go-version"
"github.com/hashicorp/go-version"
"github.com/runatlantis/atlantis/server/events/models"
)

// RunStepRunner runs custom commands.
type RunStepRunner struct {
DefaultTFVersion *version.Version
TerraformBinDir string
}

func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command string, path string) (string, error) {
Expand All @@ -32,6 +33,7 @@ func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command string, pa
"HEAD_BRANCH_NAME": ctx.Pull.HeadBranch,
"HEAD_REPO_NAME": ctx.HeadRepo.Name,
"HEAD_REPO_OWNER": ctx.HeadRepo.Owner,
"PATH": fmt.Sprintf("$PATH:%s", r.TerraformBinDir),
"PLANFILE": filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectName)),
"PROJECT_NAME": ctx.ProjectName,
"PULL_AUTHOR": ctx.Pull.Author,
Expand Down
4 changes: 4 additions & 0 deletions server/events/runtime/run_step_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func TestRunStepRunner_Run(t *testing.T) {
{
Command: "echo user_name=$USER_NAME",
ExpOut: "user_name=acme-user\n",
},{
Command: "echo $PATH",
ExpOut: "$PATH:/bin/dir\n",
},
}

Expand All @@ -70,6 +73,7 @@ func TestRunStepRunner_Run(t *testing.T) {
defaultVersion, _ := version.NewVersion("0.8")
r := runtime.RunStepRunner{
DefaultTFVersion: defaultVersion,
TerraformBinDir: "/bin/dir",
}
for _, c := range cases {
t.Run(c.Command, func(t *testing.T) {
Expand Down
10 changes: 7 additions & 3 deletions server/events/terraform/terraform_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (
"strings"
"sync"

getter "github.com/hashicorp/go-getter"
version "github.com/hashicorp/go-version"
homedir "github.com/mitchellh/go-homedir"
"github.com/hashicorp/go-getter"
"github.com/hashicorp/go-version"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/logging"
)
Expand Down Expand Up @@ -177,6 +177,10 @@ func (c *DefaultClient) DefaultVersion() *version.Version {
return c.defaultVersion
}

func (c *DefaultClient) TerraformBinDir() string {
return c.binDir
}

// See Client.RunCommandWithVersion.
func (c *DefaultClient) RunCommandWithVersion(log *logging.SimpleLogger, path string, args []string, v *version.Version, workspace string) (string, error) {
tfCmd, cmd, err := c.prepCmd(log, v, workspace, path, args)
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
},
RunStepRunner: &runtime.RunStepRunner{
DefaultTFVersion: defaultTfVersion,
TerraformBinDir: terraformClient.TerraformBinDir(),
},
PullApprovedChecker: vcsClient,
WorkingDir: workingDir,
Expand Down

0 comments on commit bb20ef0

Please sign in to comment.