From 951c9a1acf63892f5e1c99db488e34090192ee35 Mon Sep 17 00:00:00 2001 From: Jay Wallace Date: Fri, 17 Aug 2018 17:28:35 -0700 Subject: [PATCH 1/5] expose repo name, owner, and branch to run step runner for custom steps --- server/events/runtime/run_step_runner.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/server/events/runtime/run_step_runner.go b/server/events/runtime/run_step_runner.go index 6cb0cb0e64..c82f97d61e 100644 --- a/server/events/runtime/run_step_runner.go +++ b/server/events/runtime/run_step_runner.go @@ -34,6 +34,9 @@ func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command []string, fmt.Sprintf("ATLANTIS_TERRAFORM_VERSION=%s", tfVersion), fmt.Sprintf("DIR=%s", path), fmt.Sprintf("PLANFILE=%s", filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectConfig))), + fmt.Sprintf("REPO_OWNER=%s", ctx.BaseRepo.Owner), + fmt.Sprintf("REPO_NAME=%s", ctx.BaseRepo.Name), + fmt.Sprintf("BRANCH_NAME=%s", ctx.Pull.Branch) } finalEnvVars := append(baseEnvVars, customEnvVars...) cmd.Env = finalEnvVars From 4fed0ea1a67432cc86c7328efc8631a5fd2a6549 Mon Sep 17 00:00:00 2001 From: Jay Wallace Date: Fri, 17 Aug 2018 17:35:28 -0700 Subject: [PATCH 2/5] missing comma --- server/events/runtime/run_step_runner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/events/runtime/run_step_runner.go b/server/events/runtime/run_step_runner.go index c82f97d61e..5cd45f37e9 100644 --- a/server/events/runtime/run_step_runner.go +++ b/server/events/runtime/run_step_runner.go @@ -36,7 +36,7 @@ func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command []string, fmt.Sprintf("PLANFILE=%s", filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectConfig))), fmt.Sprintf("REPO_OWNER=%s", ctx.BaseRepo.Owner), fmt.Sprintf("REPO_NAME=%s", ctx.BaseRepo.Name), - fmt.Sprintf("BRANCH_NAME=%s", ctx.Pull.Branch) + fmt.Sprintf("BRANCH_NAME=%s", ctx.Pull.Branch), } finalEnvVars := append(baseEnvVars, customEnvVars...) cmd.Env = finalEnvVars From 2ec8bbdfc03b7266465db41e6891b7dc2b700c60 Mon Sep 17 00:00:00 2001 From: Jay Wallace Date: Fri, 17 Aug 2018 20:10:40 -0700 Subject: [PATCH 3/5] expose all CustomEnvVars for run step runner --- server/events/runtime/run_step_runner.go | 26 ++++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/server/events/runtime/run_step_runner.go b/server/events/runtime/run_step_runner.go index 5cd45f37e9..44de3d7b52 100644 --- a/server/events/runtime/run_step_runner.go +++ b/server/events/runtime/run_step_runner.go @@ -29,16 +29,24 @@ func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command []string, tfVersion = ctx.ProjectConfig.TerraformVersion.String() } baseEnvVars := os.Environ() - customEnvVars := []string{ - fmt.Sprintf("WORKSPACE=%s", ctx.Workspace), - fmt.Sprintf("ATLANTIS_TERRAFORM_VERSION=%s", tfVersion), - fmt.Sprintf("DIR=%s", path), - fmt.Sprintf("PLANFILE=%s", filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectConfig))), - fmt.Sprintf("REPO_OWNER=%s", ctx.BaseRepo.Owner), - fmt.Sprintf("REPO_NAME=%s", ctx.BaseRepo.Name), - fmt.Sprintf("BRANCH_NAME=%s", ctx.Pull.Branch), + 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)) } - finalEnvVars := append(baseEnvVars, customEnvVars...) cmd.Env = finalEnvVars out, err := cmd.CombinedOutput() From e109ad746115fafa6022c3c3f12d60efa8f9f1a4 Mon Sep 17 00:00:00 2001 From: Jay Wallace Date: Fri, 17 Aug 2018 20:38:19 -0700 Subject: [PATCH 4/5] Add tests for new custom envs --- server/events/runtime/run_step_runner_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/server/events/runtime/run_step_runner_test.go b/server/events/runtime/run_step_runner_test.go index 99739f842b..8666b8bf7e 100644 --- a/server/events/runtime/run_step_runner_test.go +++ b/server/events/runtime/run_step_runner_test.go @@ -38,6 +38,10 @@ func TestRunStepRunner_Run(t *testing.T) { Command: "echo workspace=$WORKSPACE version=$ATLANTIS_TERRAFORM_VERSION dir=$DIR planfile=$PLANFILE", ExpOut: "workspace=myworkspace version=0.11.0 dir=$DIR planfile=$DIR/myworkspace.tfplan\n", }, + { + Command: "echo base_repo_name=$BASE_REPO_NAME base_repo_owner=$BASE_REPO_OWNER head_repo_name=$HEAD_REPO_NAME head_repo_owner=$HEAD_REPO_OWNER head_branch_name=$HEAD_BRANCH_NAME pull_num=$PULL_NUM pull_author=$PULL_AUTHOR", + ExpOut: "base_repo_name=atlantis base_repo_owner=runatlantis head_repo_name=atlantis head_repo_owner=runatlantis head_branch_name=add-feat pull_num=2 pull_author=acme\n", + }, } projVersion, err := version.NewVersion("v0.11.0") @@ -47,6 +51,19 @@ func TestRunStepRunner_Run(t *testing.T) { DefaultTFVersion: defaultVersion, } ctx := models.ProjectCommandContext{ + BaseRepo: models.Repo{ + Name: "atlantis", + Owner: "runatlantis", + }, + HeadRepo: models.Repo{ + Name: "atlantis", + Owner: "runatlantis", + }, + Pull: models.PullRequest{ + Num: 2, + Branch: "add-feat", + Author: "acme", + }, Log: logging.NewNoopLogger(), Workspace: "myworkspace", RepoRelDir: "mydir", From d9c99513e742b69c4670294c67c2a6fbb923d633 Mon Sep 17 00:00:00 2001 From: Jay Wallace Date: Fri, 17 Aug 2018 20:48:44 -0700 Subject: [PATCH 5/5] document new custom env vars --- runatlantis.io/docs/atlantis-yaml-reference.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/runatlantis.io/docs/atlantis-yaml-reference.md b/runatlantis.io/docs/atlantis-yaml-reference.md index 6851acd373..3181344358 100644 --- a/runatlantis.io/docs/atlantis-yaml-reference.md +++ b/runatlantis.io/docs/atlantis-yaml-reference.md @@ -180,6 +180,13 @@ Or a custom command * `PLANFILE` - Absolute path to the location where Atlantis expects the plan to either be generated (by plan) or already exist (if running apply). Can be used to override the built-in `plan`/`apply` commands, ex. `run: terraform plan -out $PLANFILE`. +* `BASE_REPO_NAME` - Name of the repository that the pull request will be merged into, ex. `atlantis`. +* `BASE_REPO_OWNER` - Owner of the repository that the pull request will be merged into, ex. `runatlantis`. +* `HEAD_REPO_NAME` - Name of the repository that is getting merged into the base repository, ex. `atlantis`. +* `HEAD_REPO_OWNER` - Owner of the repository that is getting merged into the base repository, ex. `acme-corp`. +* `HEAD_BRANCH_NAME` - Name of the head branch of the pull request +* `PULL_NUM` - Pull request number or ID, ex. `2`. +* `PULL_AUTHOR` - Username of the pull request author, ex. `acme-user`. ::: ## Next Steps