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

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

Open
mihakrumpestar opened this issue Mar 27, 2024 · 0 comments
Open
Labels
state: needs triage Waiting to be triaged by a maintainer.

Comments

@mihakrumpestar
Copy link

mihakrumpestar commented Mar 27, 2024

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
@task-bot task-bot added the state: needs triage Waiting to be triaged by a maintainer. label Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs triage Waiting to be triaged by a maintainer.
Projects
None yet
Development

No branches or pull requests

2 participants