You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	[newmanExecute] Allow env vars in the runOptions (#3966)
* Allow env vars in the runOptions * Add documentation * Regenerate for documentation * Fix documentation
This commit is contained in:
		| @@ -163,7 +163,11 @@ func resolveTemplate(config *newmanExecuteOptions, collection string) ([]string, | ||||
| 	} | ||||
|  | ||||
| 	for _, runOption := range config.RunOptions { | ||||
| 		templ, err := template.New("template").Parse(runOption) | ||||
| 		templ, err := template.New("template").Funcs(template.FuncMap{ | ||||
| 			"getenv": func(varName string) string { | ||||
| 				return os.Getenv(varName) | ||||
| 			}, | ||||
| 		}).Parse(runOption) | ||||
| 		if err != nil { | ||||
| 			log.SetErrorCategory(log.ErrorConfiguration) | ||||
| 			return nil, errors.Wrap(err, "could not parse newman command template") | ||||
|   | ||||
| @@ -202,7 +202,7 @@ func NewmanExecuteCommand() *cobra.Command { | ||||
| func addNewmanExecuteFlags(cmd *cobra.Command, stepConfig *newmanExecuteOptions) { | ||||
| 	cmd.Flags().StringVar(&stepConfig.NewmanCollection, "newmanCollection", `**/*.postman_collection.json`, "The test collection that should be executed. This could also be a file pattern.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.NewmanRunCommand, "newmanRunCommand", os.Getenv("PIPER_newmanRunCommand"), "+++ Deprecated +++ Please use list parameter `runOptions` instead.") | ||||
| 	cmd.Flags().StringSliceVar(&stepConfig.RunOptions, "runOptions", []string{`run`, `{{.NewmanCollection}}`, `--reporters`, `cli,junit,html`, `--reporter-junit-export`, `target/newman/TEST-{{.CollectionDisplayName}}.xml`, `--reporter-html-export`, `target/newman/TEST-{{.CollectionDisplayName}}.html`}, "The newman command that will be executed inside the docker container.") | ||||
| 	cmd.Flags().StringSliceVar(&stepConfig.RunOptions, "runOptions", []string{`run`, `{{.NewmanCollection}}`, `--reporters`, `cli,junit,html`, `--reporter-junit-export`, `target/newman/TEST-{{.CollectionDisplayName}}.xml`, `--reporter-html-export`, `target/newman/TEST-{{.CollectionDisplayName}}.html`}, "The newman command that will be executed inside the docker container. Env vars can be passed via template as in \"{{getenv MY_ENV_VAR}}\".") | ||||
| 	cmd.Flags().StringVar(&stepConfig.NewmanInstallCommand, "newmanInstallCommand", `npm install newman newman-reporter-html --global --quiet`, "The shell command that will be executed inside the docker container to install Newman.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.NewmanEnvironment, "newmanEnvironment", os.Getenv("PIPER_newmanEnvironment"), "Specify an environment file path or URL. Environments provide a set of variables that one can use within collections.") | ||||
| 	cmd.Flags().StringVar(&stepConfig.NewmanGlobals, "newmanGlobals", os.Getenv("PIPER_newmanGlobals"), "Specify the file path or URL for global variables. Global variables are similar to environment variables but have a lower precedence and can be overridden by environment variables having the same name.") | ||||
|   | ||||
| @@ -1,6 +1,8 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"github.com/google/uuid" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| @@ -230,6 +232,19 @@ func TestResolveTemplate(t *testing.T) { | ||||
| 		assert.Equal(t, []string{"this", "is", "my", "fancy", "command", "theDisplayName"}, cmd) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("get environment variable", func(t *testing.T) { | ||||
| 		t.Parallel() | ||||
|  | ||||
| 		temporaryEnvVarName := uuid.New().String() | ||||
| 		os.Setenv(temporaryEnvVarName, "myEnvVar") | ||||
| 		defer os.Unsetenv(temporaryEnvVarName) | ||||
| 		config := newmanExecuteOptions{RunOptions: []string{"this", "is", "my", "fancy", "command", "with", "--env-var", "{{getenv \"" + temporaryEnvVarName + "\"}}"}} | ||||
|  | ||||
| 		cmd, err := resolveTemplate(&config, "collectionsDisplayName") | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.Equal(t, []string{"this", "is", "my", "fancy", "command", "with", "--env-var", "myEnvVar"}, cmd) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("error when parameter cannot be resolved", func(t *testing.T) { | ||||
| 		t.Parallel() | ||||
|  | ||||
|   | ||||
| @@ -46,6 +46,20 @@ If the following error occurs during the pipeline run, the `newmanRunCommand` is | ||||
|  | ||||
| Referencing `newmanEnvironment` and `newmanGlobals` in the runOptions is redundant now. Both parameters are added to runCommand using `newmanEnvironment` and `newmanGlobals` from config  when configured and not referenced by go templating using `"--environment", "{{`{{.Config.NewmanEnvironment}}`}}"` and `"--globals", "{{`{{.Config.NewmanGlobals}}`}}"` as shown above. | ||||
|  | ||||
| ## Passing Credentials | ||||
|  | ||||
| If you need to pass additional credentials you can do so via environment | ||||
| variables. This is done via templating in the `runOptions`, as per this example: | ||||
|  | ||||
| ```yaml | ||||
| runOptions: [ | ||||
|     {{`"run", "{{.NewmanCollection}}",`}} | ||||
|     {{`"--environment", "{{.Config.NewmanEnvironment}}",`}} | ||||
|     {{`"--env-var", "username={{getenv \"PIPER_TESTCREDENTIAL_USERNAME\"}}",`}} | ||||
|     {{`"--env-var", "password={{getenv \"PIPER_TESTCREDENTIAL_PASSWORD\"}}"`}} | ||||
| ] | ||||
| ``` | ||||
|  | ||||
| ## Example | ||||
|  | ||||
| Pipeline step: | ||||
|   | ||||
| @@ -25,7 +25,7 @@ spec: | ||||
|           - STEPS | ||||
|         type: string | ||||
|       - name: runOptions | ||||
|         description: The newman command that will be executed inside the docker container. | ||||
|         description: The newman command that will be executed inside the docker container. Env vars can be passed via template as in "{{getenv MY_ENV_VAR}}". | ||||
|         scope: | ||||
|           - PARAMETERS | ||||
|           - STAGES | ||||
|   | ||||
		Reference in New Issue
	
	Block a user