tektoncd / pipeline

A cloud-native Pipeline resource.
https://tekton.dev
Apache License 2.0
8.48k stars 1.78k forks source link

Can't echo array params in a task #6972

Open EmmaMunley opened 1 year ago

EmmaMunley commented 1 year ago

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

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"}
Client version: 0.31.1
Pipeline version: devel
EmmaMunley commented 1 year ago

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

Yongxuanzhang commented 1 year ago

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

jerop commented 1 year ago

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

afrittoli commented 1 year ago

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.

Yongxuanzhang commented 1 year ago

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

tekton-robot commented 1 year ago

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.