Skip to content

bug: VARS not overridden when calling deps of a task from for loop #1565

@mihakrumpestar

Description

@mihakrumpestar

As stated in the title, when doing loop, we override VAR/variable PROJECT already defined in root. This VAR is correctly overridden in the direct task of the for loop, but not in the deps that that task has. There the initial/original VAR value persist that we defined in the top level VARS.

I did not find anything similar in other issues, but might have missed it.

task deploy-all PROJECT=all
version: '3'

vars:
  ENV: prod
  PROJECT: # Task parameter/arguments are not yet available in root interpolation/templating, Taskfile also does not support top level require
    sh: '[ -z "${PROJECT+x}" ] && { echo "Error: PROJECT is not defined" >&2; exit 1; } || echo "$PROJECT"'

tasks:
  deploy:
    preconditions:
      - test -d ./{{.PROJECT}}
    deps:
      - task: template
    vars:
      PROJECT_ENV: './{{.PROJECT}}/.{{.ENV}}.env'
      GOMPLATE_OUTPUT: './{{.PROJECT}}/docker-compose.{{.ENV}}.yml'
    cmds:
      - |
        CMD="docker compose -f {{.GOMPLATE_OUTPUT}} -p {{.PROJECT}}_{{.ENV}} up -d --force-recreate --remove-orphans"
        CMD="sops exec-env .{{.ENV}}.env '$CMD'"
        if [ -f {{.PROJECT_ENV}} ]; then
            CMD="sops exec-env {{.PROJECT_ENV}} \"$CMD\""
        fi

        echo -e "\033[0;32mtask: [{{.TASK}}] $CMD\033[0m"
        eval $CMD
    silent: true
    
  template:
    preconditions: # Not compatible as subtasks/deps in for loop
      - test -d ./{{.PROJECT}}
    vars:
      GOMPLATE_INPUT: './{{.PROJECT}}/docker-compose.{{.ENV}}.tmpl.yml'
      GOMPLATE_OUTPUT: './{{.PROJECT}}/docker-compose.{{.ENV}}.yml'
    cmds:
      - |
        if test -f {{.GOMPLATE_INPUT}}; then
          CMD='gomplate -d datasource={{.GOMPLATE_DATASOURCE}} -f {{.GOMPLATE_INPUT}} -o - |  (echo -e "$DO_NOT_EDIT" && cat) > {{.GOMPLATE_OUTPUT}}'
          echo -e "\033[0;32mtask: [{{.TASK}}] $CMD\033[0m"
          eval $CMD
        else
          MSG="File {{.GOMPLATE_INPUT}} does not exist, skipping..."
          echo -e "\033[0;32mtask: [{{.TASK}}] $MSG\033[0m"
        fi
    silent: true
    
  deploy-all:
    prompt: This is a dangerous command... Do you want to continue?
    preconditions:
      - '[[ "{{.PROJECT}}" == "all" ]] || exit 1'
    cmds:
      - for:  ["whoami"]
      #- for: { var: SUBDIRS }
        task: deploy
        vars:
          PROJECT: '{{.ITEM}}'
  • Task version: latest using go source as of 27.3.2024, commit bb68fb3
  • Operating system: Arch Linux
  • Experiments enabled: no

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: needs triageWaiting to be triaged by a maintainer.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions