forked from runatlantis/atlantis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_step_runner.go
61 lines (54 loc) · 1.87 KB
/
run_step_runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package runtime
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/hashicorp/go-version"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/models"
)
// RunStepRunner runs custom commands.
type RunStepRunner struct {
DefaultTFVersion *version.Version
}
func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command []string, path string) (string, error) {
if len(command) < 1 {
return "", errors.New("no commands for run step")
}
cmd := exec.Command("sh", "-c", strings.Join(command, " ")) // #nosec
cmd.Dir = path
tfVersion := r.DefaultTFVersion.String()
if ctx.ProjectConfig != nil && ctx.ProjectConfig.TerraformVersion != nil {
tfVersion = ctx.ProjectConfig.TerraformVersion.String()
}
baseEnvVars := os.Environ()
customEnvVars := map[string]string{
"WORKSPACE": ctx.Workspace,
"ATLANTIS_TERRAFORM_VERSION": tfVersion,
"DIR": path,
"PLANFILE": filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectConfig)),
"BASE_REPO_NAME": ctx.BaseRepo.Name,
"BASE_REPO_OWNER": ctx.BaseRepo.Owner,
"HEAD_REPO_NAME": ctx.HeadRepo.Name,
"HEAD_REPO_OWNER": ctx.HeadRepo.Owner,
"HEAD_BRANCH_NAME": ctx.Pull.Branch,
"PULL_NUM": fmt.Sprintf("%d", ctx.Pull.Num),
"PULL_AUTHOR": ctx.Pull.Author,
}
finalEnvVars := baseEnvVars
for key, val := range customEnvVars {
finalEnvVars = append(finalEnvVars, fmt.Sprintf("%s=%s", key, val))
}
cmd.Env = finalEnvVars
out, err := cmd.CombinedOutput()
commandStr := strings.Join(command, " ")
if err != nil {
err = fmt.Errorf("%s: running %q in %q: \n%s", err, commandStr, path, out)
ctx.Log.Debug("error: %s", err)
return string(out), err
}
ctx.Log.Info("successfully ran %q in %q", commandStr, path)
return string(out), nil
}