forked from runatlantis/atlantis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: MultiEnv step added (runatlantis#1793)
* MultiEnv step added * Values removed from the output * Fixing tests * multienv_step_runner_test added * Documentation of new feature added * Documentatation of new feature modified * multienv_step test file extension changed * Fixed multinev_step_runner test * Enhanced debug logging in multienv_step_runner * Enhanced errorhandling * Test command modified * Test command modified * Test command modified * Test command modified * Errorhandling modified * Test command modified * Test command modified * Create empty map in test * Fixing test ExpOut * Testing, refactoring, eliminating extra debug log * MultiEnv result modified from json to plain string * MultiEnv step Run parameter updated * Import added * project_command_runner updated * project_command_runner updated * multienv_step_runner_test updated * multienv doc modified * Update runatlantis.io/docs/custom-workflows.md Co-authored-by: PePe Amengual <[email protected]> Co-authored-by: Istvan Tapaszto <[email protected]> Co-authored-by: PePe Amengual <[email protected]>
- Loading branch information
Showing
6 changed files
with
156 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package runtime | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/runatlantis/atlantis/server/events/command" | ||
) | ||
|
||
// EnvStepRunner set environment variables. | ||
type MultiEnvStepRunner struct { | ||
RunStepRunner *RunStepRunner | ||
} | ||
|
||
// Run runs the multienv step command. | ||
// The command must return a json string containing the array of name-value pairs that are being added as extra environment variables | ||
func (r *MultiEnvStepRunner) Run(ctx command.ProjectContext, command string, path string, envs map[string]string) (string, error) { | ||
res, err := r.RunStepRunner.Run(ctx, command, path, envs) | ||
if err == nil { | ||
envVars := strings.Split(res, ",") | ||
if len(envVars) > 0 { | ||
var sb strings.Builder | ||
sb.WriteString("Dynamic environment variables added:\n") | ||
for _, item := range envVars { | ||
nameValue := strings.Split(item, "=") | ||
if len(nameValue) == 2 { | ||
envs[nameValue[0]] = nameValue[1] | ||
sb.WriteString(nameValue[0]) | ||
sb.WriteString("\n") | ||
} else { | ||
return "", fmt.Errorf("Invalid environment variable definition: %s", item) | ||
} | ||
} | ||
return sb.String(), nil | ||
} | ||
return "No dynamic environment variable added", nil | ||
} | ||
return "", err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package runtime_test | ||
|
||
import ( | ||
"testing" | ||
|
||
version "github.com/hashicorp/go-version" | ||
. "github.com/petergtz/pegomock" | ||
"github.com/runatlantis/atlantis/server/core/runtime" | ||
"github.com/runatlantis/atlantis/server/core/terraform/mocks" | ||
"github.com/runatlantis/atlantis/server/events/command" | ||
"github.com/runatlantis/atlantis/server/events/models" | ||
"github.com/runatlantis/atlantis/server/logging" | ||
. "github.com/runatlantis/atlantis/testing" | ||
) | ||
|
||
func TestMultiEnvStepRunner_Run(t *testing.T) { | ||
cases := []struct { | ||
Command string | ||
ProjectName string | ||
ExpOut string | ||
ExpErr string | ||
Version string | ||
}{ | ||
{ | ||
Command: `echo 'TF_VAR_REPODEFINEDVARIABLE_ONE=value1'`, | ||
ExpOut: "Dynamic environment variables added:\nTF_VAR_REPODEFINEDVARIABLE_ONE\n", | ||
Version: "v1.2.3", | ||
}, | ||
} | ||
RegisterMockTestingT(t) | ||
tfClient := mocks.NewMockClient() | ||
tfVersion, err := version.NewVersion("0.12.0") | ||
Ok(t, err) | ||
runStepRunner := runtime.RunStepRunner{ | ||
TerraformExecutor: tfClient, | ||
DefaultTFVersion: tfVersion, | ||
} | ||
multiEnvStepRunner := runtime.MultiEnvStepRunner{ | ||
RunStepRunner: &runStepRunner, | ||
} | ||
for _, c := range cases { | ||
t.Run(c.Command, func(t *testing.T) { | ||
tmpDir, cleanup := TempDir(t) | ||
defer cleanup() | ||
ctx := command.ProjectContext{ | ||
BaseRepo: models.Repo{ | ||
Name: "basename", | ||
Owner: "baseowner", | ||
}, | ||
HeadRepo: models.Repo{ | ||
Name: "headname", | ||
Owner: "headowner", | ||
}, | ||
Pull: models.PullRequest{ | ||
Num: 2, | ||
HeadBranch: "add-feat", | ||
BaseBranch: "master", | ||
Author: "acme", | ||
}, | ||
User: models.User{ | ||
Username: "acme-user", | ||
}, | ||
Log: logging.NewNoopLogger(t), | ||
Workspace: "myworkspace", | ||
RepoRelDir: "mydir", | ||
TerraformVersion: tfVersion, | ||
ProjectName: c.ProjectName, | ||
} | ||
envMap := make(map[string]string) | ||
value, err := multiEnvStepRunner.Run(ctx, c.Command, tmpDir, envMap) | ||
if c.ExpErr != "" { | ||
ErrContains(t, c.ExpErr, err) | ||
return | ||
} | ||
Ok(t, err) | ||
Equals(t, c.ExpOut, value) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters