Skip to content

Commit

Permalink
Expose repo name, owner, and branch to run step runner for custom ste…
Browse files Browse the repository at this point in the history
…ps (#232)

Commits by @mootpt (squashed)
  • Loading branch information
lkysow authored Aug 20, 2018
1 parent 352b6fd commit 756435e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
7 changes: 7 additions & 0 deletions runatlantis.io/docs/atlantis-yaml-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 17 additions & 6 deletions server/events/runtime/run_step_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +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))),
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()

Expand Down
17 changes: 17 additions & 0 deletions server/events/runtime/run_step_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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=basename base_repo_owner=baseowner head_repo_name=headname head_repo_owner=headowner head_branch_name=add-feat pull_num=2 pull_author=acme\n",
},
}

projVersion, err := version.NewVersion("v0.11.0")
Expand All @@ -47,6 +51,19 @@ func TestRunStepRunner_Run(t *testing.T) {
DefaultTFVersion: defaultVersion,
}
ctx := models.ProjectCommandContext{
BaseRepo: models.Repo{
Name: "basename",
Owner: "baseowner",
},
HeadRepo: models.Repo{
Name: "headname",
Owner: "headowner",
},
Pull: models.PullRequest{
Num: 2,
Branch: "add-feat",
Author: "acme",
},
Log: logging.NewNoopLogger(),
Workspace: "myworkspace",
RepoRelDir: "mydir",
Expand Down

0 comments on commit 756435e

Please sign in to comment.