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

expose custom env vars for run step runner for custom steps #229

Closed
wants to merge 5 commits into from
Closed
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
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=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")
Expand All @@ -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",
Expand Down