diff --git a/pkg/apis/pipeline/v1/resultref.go b/pkg/apis/pipeline/v1/resultref.go index 4bcd085f32d..a4b98eef220 100644 --- a/pkg/apis/pipeline/v1/resultref.go +++ b/pkg/apis/pipeline/v1/resultref.go @@ -170,18 +170,18 @@ func parseExpression(substitutionExpression string) (string, string, *int, strin return "", "", nil, "", ParamTypeString, fmt.Errorf("must be one of the form 1). %q; 2). %q", resultExpressionFormat, objectResultExpressionFormat) } -func parseTaskExpression(substitutionExpression string) (string, string, int, string, ParamType, error) { +func parseTaskExpression(substitutionExpression string) (string, string, *int, string, ParamType, error) { if looksLikeResultRef(substitutionExpression) { return parseExpression(substitutionExpression) } - return "", "", 0, "", ParamTypeString, fmt.Errorf("must be one of the form 1). %q; 2). %q", resultExpressionFormat, objectResultExpressionFormat) + return "", "", nil, "", ParamTypeString, fmt.Errorf("must be one of the form 1). %q; 2). %q", resultExpressionFormat, objectResultExpressionFormat) } -func ParseStepExpression(substitutionExpression string) (string, string, int, string, ParamType, error) { +func ParseStepExpression(substitutionExpression string) (string, string, *int, string, ParamType, error) { if looksLikeStepResultRef(substitutionExpression) { return parseExpression(substitutionExpression) } - return "", "", 0, "", ParamTypeString, fmt.Errorf("must be one of the form 1). %q; 2). %q", resultExpressionFormat, objectResultExpressionFormat) + return "", "", nil, "", ParamTypeString, fmt.Errorf("must be one of the form 1). %q; 2). %q", resultExpressionFormat, objectResultExpressionFormat) } // ParseResultName parse the input string to extract resultName and result index. diff --git a/pkg/entrypoint/entrypointer.go b/pkg/entrypoint/entrypointer.go index 1df657dbd99..7f77d6d1b1c 100644 --- a/pkg/entrypoint/entrypointer.go +++ b/pkg/entrypoint/entrypointer.go @@ -379,10 +379,10 @@ func findReplacement(s string) (string, []string, error) { if paramType == v1.ParamTypeObject && objectKey != "" { replaceWithString = result.ObjectVal[objectKey] } else if paramType == v1.ParamTypeArray { - if arrayIdx == -1 { - replaceWithArray = append(replaceWithArray, result.ArrayVal...) + if arrayIdx != nil { + replaceWithString = result.ArrayVal[*arrayIdx] } else { - replaceWithString = result.ArrayVal[arrayIdx] + replaceWithArray = append(replaceWithArray, result.ArrayVal...) } } else { replaceWithString = result.StringVal @@ -391,7 +391,7 @@ func findReplacement(s string) (string, []string, error) { } // replaceCommandAndArgs performs replacements for step results in environment variables. -func replaceEnv() error { +func replaceEnv(stepResultRegex *regexp.Regexp) error { for _, e := range os.Environ() { pair := strings.SplitN(e, "=", 2) matches := stepResultRegex.FindAllStringSubmatch(pair[1], -1) @@ -410,7 +410,7 @@ func replaceEnv() error { // It reads the original script file, performs the replacements and writes a new file // since the original script location is unknown. // It then updates the command to execute the new file. -func replaceScript(e *Entrypointer) error { +func replaceScript(e *Entrypointer, stepResultRegex *regexp.Regexp) error { if strings.HasPrefix(e.Command[0], "/tekton/scripts") { fileContentBytes, err := os.ReadFile(e.Command[0]) if err != nil { @@ -440,9 +440,9 @@ func replaceScript(e *Entrypointer) error { } // replaceCommandAndArgs performs replacements for step results in e.Command -func replaceCommandAndArgs(command []string) ([]string, error) { +func replaceCommandAndArgs(command []string, stepResultRegex *regexp.Regexp) ([]string, error) { newCommand := []string{} - for _, c := range e.Command { + for _, c := range command { matches := stepResultRegex.FindAllStringSubmatch(c, -1) if len(matches) > 0 { for _, m := range matches { @@ -472,15 +472,15 @@ func (e *Entrypointer) applyStepResultSubstitutions() error { pattern := `\$\(steps\..*\.results\..*\)` stepResultRegex := regexp.MustCompile(pattern) // env - if err := replaceEnv(); err != nil { + if err := replaceEnv(stepResultRegex); err != nil { return err } // script - if err := replaceScript(e); err != nil { + if err := replaceScript(e, stepResultRegex); err != nil { return err } // command + args - newCommand, err := replaceCommandAndArgs(e.Command) + newCommand, err := replaceCommandAndArgs(e.Command, stepResultRegex) if err != nil { return err }