Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use vars in matrix #2065

Open
DjinNO opened this issue Feb 17, 2025 · 1 comment · May be fixed by #2069
Open

Allow to use vars in matrix #2065

DjinNO opened this issue Feb 17, 2025 · 1 comment · May be fixed by #2069
Labels
area: loops Changes related to looping over tasks/commands.

Comments

@DjinNO
Copy link

DjinNO commented Feb 17, 2025

Description

It is not possible to use variables in a matrix task now

version: '3'

vars:
  OS_VAR: ["windows", "linux", "darwin"]
  ARCH_VAR: ["amd64", "arm64"]

tasks:
  default:
    silent: true
    cmds:
      - for:
          matrix:
            OS: {{.OS_VAR}}
            ARCH: {{.ARCH_VAR}}
        cmd: echo "{{.ITEM.OS}}/{{.ITEM.ARCH}}"
err:  invalid keys in command
file: /Users/djinn/GIT/gitlab/gitops/installations/generator/Taskfile.yml:12:9
  10 |     silent: true
  11 |     cmds:
> 12 |       - for:
     |         ^
  13 |           matrix:
  14 |             OS: {{.OS_VAR}}

Correct me if I'm wrong

@task-bot task-bot added the state: needs triage Waiting to be triaged by a maintainer. label Feb 17, 2025
@pd93
Copy link
Member

pd93 commented Feb 17, 2025

@DjinNO You are correct that it is currently not possible to pass variables into a matrix. You would need to define the variables statically:

version: '3'

tasks:
  default:
    silent: true
    cmds:
      - for:
          matrix:
            OS: ["windows", "linux", "darwin"]
            ARCH: ["amd64", "arm64"]
        cmd: echo "{{.ITEM.OS}}/{{.ITEM.ARCH}}"

The error message you're getting is actually a little misleading and I've opened #2068 to address this. With the changes in that PR, you should see something like err: cannot unmarshal !!map into []interface {}. This indicates that the values in a matrix need to be arrays and hence you wouldn't be able to using templating syntax to do what you want (templates always output strings).

If we support this going forwards, I imagine it would look something like this instead:

version: '3'

vars:
  OS_VAR: ["windows", "linux", "darwin"]
  ARCH_VAR: ["amd64", "arm64"]

tasks:
  default:
    silent: true
    cmds:
      - for:
          matrix:
            OS:
              ref: OS_VAR
            ARCH:
              ref: ARCH_VAR
        cmd: echo "{{.ITEM.OS}}/{{.ITEM.ARCH}}"

However, this won't work currently.

@pd93 pd93 added area: loops Changes related to looping over tasks/commands. and removed state: needs triage Waiting to be triaged by a maintainer. labels Feb 17, 2025
@pd93 pd93 linked a pull request Feb 18, 2025 that will close this issue
@pd93 pd93 linked a pull request Feb 18, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: loops Changes related to looping over tasks/commands.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants