Skip to content

Commit

Permalink
Append terraform binary dir to PATH when running custom commands (#678)
Browse files Browse the repository at this point in the history
Append Atlantis terraform binary dir to PATH when running custom commands
  • Loading branch information
maximede authored and lkysow committed Jul 16, 2019
1 parent 7c33751 commit 8180f35
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
5 changes: 4 additions & 1 deletion server/events/runtime/run_step_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
"path/filepath"
"strings"

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 is the directory where Atlantis downloads Terraform binaries.
TerraformBinDir string
}

func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command string, path string) (string, error) {
Expand All @@ -34,6 +36,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("%s:%s", os.Getenv("PATH"), r.TerraformBinDir),
"PLANFILE": filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectName)),
"PROJECT_NAME": ctx.ProjectName,
"PULL_AUTHOR": ctx.Pull.Author,
Expand Down
8 changes: 7 additions & 1 deletion server/events/runtime/run_step_runner_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package runtime_test

import (
"fmt"
"os"
"strings"
"testing"

version "github.com/hashicorp/go-version"
"github.com/hashicorp/go-version"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/runtime"
"github.com/runatlantis/atlantis/server/logging"
Expand Down Expand Up @@ -62,6 +64,9 @@ func TestRunStepRunner_Run(t *testing.T) {
{
Command: "echo user_name=$USER_NAME",
ExpOut: "user_name=acme-user\n",
}, {
Command: "echo $PATH",
ExpOut: fmt.Sprintf("%s:%s\n", os.Getenv("PATH"), "/bin/dir"),
},
{
Command: "echo args=$COMMENT_ARGS",
Expand All @@ -74,6 +79,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
11 changes: 8 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 @@ -184,6 +184,11 @@ func (c *DefaultClient) DefaultVersion() *version.Version {
return c.defaultVersion
}

// TerraformBinDir returns the directory where we download Terraform binaries.
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 @@ -290,6 +290,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 8180f35

Please sign in to comment.