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 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
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,15 @@ 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 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 @@ -32,6 +34,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"),
},
}

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
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 @@ -177,6 +177,11 @@ func (c *DefaultClient) DefaultVersion() *version.Version {
return c.defaultVersion
}

// TerraformBinDir returns the directory where we download Terraform binaries.
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