Files
atlantis/server/events/runtime/pre_workflow_hook_runner.go
Sarvar Muminov 3456dc934b Add pre workflow custom hooks to run scripts before workflow execution(plan, apply, etc) (#1255)
* Updated runatlantis.io/docs to have `pre-workflow-hooks` use cases and examples
2020-12-14 15:19:52 -05:00

47 lines
1.2 KiB
Go

package runtime
import (
"fmt"
"os"
"os/exec"
"github.com/runatlantis/atlantis/server/events/models"
)
type PreWorkflowHookRunner struct{}
func (wh *PreWorkflowHookRunner) Run(ctx models.PreWorkflowHookCommandContext, command string, path string) (string, error) {
cmd := exec.Command("sh", "-c", command) // #nosec
cmd.Dir = path
baseEnvVars := os.Environ()
customEnvVars := map[string]string{
"BASE_BRANCH_NAME": ctx.Pull.BaseBranch,
"BASE_REPO_NAME": ctx.BaseRepo.Name,
"BASE_REPO_OWNER": ctx.BaseRepo.Owner,
"DIR": path,
"HEAD_BRANCH_NAME": ctx.Pull.HeadBranch,
"HEAD_REPO_NAME": ctx.HeadRepo.Name,
"HEAD_REPO_OWNER": ctx.HeadRepo.Owner,
"PULL_AUTHOR": ctx.Pull.Author,
"PULL_NUM": fmt.Sprintf("%d", ctx.Pull.Num),
"USER_NAME": ctx.User.Username,
}
finalEnvVars := baseEnvVars
for key, val := range customEnvVars {
finalEnvVars = append(finalEnvVars, fmt.Sprintf("%s=%s", key, val))
}
cmd.Env = finalEnvVars
out, err := cmd.CombinedOutput()
if err != nil {
err = fmt.Errorf("%s: running %q in %q: \n%s", err, command, path, out)
ctx.Log.Debug("error: %s", err)
return "", err
}
ctx.Log.Info("successfully ran %q in %q", command, path)
return string(out), nil
}