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

Append terraform binary dir to PATH when running custom commands #678

Merged
merged 3 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
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 @@ -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
maximede marked this conversation as resolved.
Show resolved Hide resolved
}

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(os.ExpandEnv("$PATH:%s"), r.TerraformBinDir),
maximede marked this conversation as resolved.
Show resolved Hide resolved
"PLANFILE": filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectName)),
"PROJECT_NAME": ctx.ProjectName,
"PULL_AUTHOR": ctx.Pull.Author,
Expand All @@ -44,6 +46,7 @@ func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command string, pa
for key, val := range customEnvVars {
finalEnvVars = append(finalEnvVars, fmt.Sprintf("%s=%s", key, val))
}

maximede marked this conversation as resolved.
Show resolved Hide resolved
cmd.Env = finalEnvVars
out, err := cmd.CombinedOutput()

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(os.ExpandEnv("$PATH%s"), ":/bin/dir\n"),
maximede marked this conversation as resolved.
Show resolved Hide resolved
},
}

Expand All @@ -70,6 +75,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 {
maximede marked this conversation as resolved.
Show resolved Hide resolved
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