pipeline icon indicating copy to clipboard operation
pipeline copied to clipboard

Can't echo array params in a task

Open EmmaMunley opened this issue 2 years ago • 7 comments

Expected Behavior

I expect to be able to run this yaml file and have it echo the array values.

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: mytask
spec:
  params:
    - name: TEST
      type: array
  steps:
    - name: echo
      image: alpine
      script: |
        echo "$(params.TEST[*])"
---
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  generateName: test-pr
spec:
  serviceAccountName: "default"
  pipelineSpec:
    tasks:
      - name: task
        taskRef:
          name: mytask
          kind: Task
        params:
          - name: TEST
            value:
              - a
              - b
              - c

Actual Behavior

Instead I get this error message: Error from server (BadRequest): error when creating "test.yaml": admission webhook "validation.webhook.pipeline.tekton.dev" denied the request: validation failed: variable type invalid in "echo \"$(params.TEST[*])\"\n": spec.steps[0].script

As a workaround, you can use environment variables like so, but this doesn't always work because I sometimes get this error: env: can't execute 'bash': No such file or directory

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: mytask
spec:
  params:
    - name: TEST
      type: array
  steps:
    - name: use-environments
      image: bash:latest
      args: ["$(params.TEST[*])"]
      script: |
        for arg in "$@"; do
          echo "Arg: $arg"
        done

Steps to Reproduce the Problem

  1. Run the examples provided

Additional Info

  • Kubernetes version:

    Output of kubectl version:

Client Version: version.Info{Major:"1", Minor:"25", GitVersion:"v1.25.4", GitCommit:"872a965c6c6526caa949f0c6ac028ef7aff3fb78", GitTreeState:"clean", BuildDate:"2022-11-09T13:36:36Z", GoVersion:"go1.19.3", Compiler:"gc", Platform:"darwin/arm64"}
Kustomize Version: v4.5.7
Server Version: version.Info{Major:"1", Minor:"26", GitVersion:"v1.26.3", GitCommit:"9e644106593f3f4aa98f8a84b23db5fa378900bd", GitTreeState:"clean", BuildDate:"2023-03-15T13:33:12Z", GoVersion:"go1.19.7", Compiler:"gc", Platform:"linux/arm64"}
  • Tekton Pipeline version:

    Output of tkn version or kubectl get pods -n tekton-pipelines -l app=tekton-pipelines-controller -o=jsonpath='{.items[0].metadata.labels.version}'

Client version: 0.31.1
Pipeline version: devel

EmmaMunley avatar Jul 25 '23 21:07 EmmaMunley

cc: @jerop, @lbernick, @pritidesai, @Yongxuanzhang

EmmaMunley avatar Jul 27 '23 14:07 EmmaMunley

Thanks! This is the same as https://github.com/tektoncd/pipeline/issues/4912

Yongxuanzhang avatar Jul 27 '23 15:07 Yongxuanzhang

given that the same issue has come up again, can we please document this as a known limitation? 🙏🏾

jerop avatar Jul 27 '23 16:07 jerop

Indeed, this was never supported, as it is not obvious what the representation of the array should be, especially since it's now known in which environment it will be used. We do support indexing in arrays now, which is a step forward.
If we supported something like $(#params.TEST[*]) to read the length of the array, it would be possible for scripts to iterate on element arrays.

afrittoli avatar Aug 04 '23 13:08 afrittoli

If users want to iterate the array params in script, this can do this:

      taskSpec:
        params:
          - name: environments
            type: array
        steps:
          - name: use-environments
            image: bash:latest
            args: [
              "$(params.environments[*])",
            ]
            script: |
              for arg in "$@"; do
                echo "Arg: $arg"
              done

So even if we cannot use the whole array ref in script directly, we have the workaround to pass the array params to args and reference it in script

Yongxuanzhang avatar Aug 08 '23 20:08 Yongxuanzhang

Issues go stale after 90d of inactivity. Mark the issue as fresh with /remove-lifecycle stale with a justification. Stale issues rot after an additional 30d of inactivity and eventually close. If this issue is safe to close now please do so with /close with a justification. If this issue should be exempted, mark the issue as frozen with /lifecycle frozen with a justification.

/lifecycle stale

Send feedback to tektoncd/plumbing.

tekton-robot avatar Nov 06 '23 21:11 tekton-robot

If users want to iterate the array params in script, this can do this:

      taskSpec:
        params:
          - name: environments
            type: array
        steps:
          - name: use-environments
            image: bash:latest
            args: [
              "$(params.environments[*])",
            ]
            script: |
              for arg in "$@"; do
                echo "Arg: $arg"
              done

So even if we cannot use the whole array ref in script directly, we have the workaround to pass the array params to args and reference it in script

OK, but there isn't a workaround in case of many array params

satand avatar May 28 '25 13:05 satand