Task: Passing environment variables from command lines does not work as expected

Created on 22 Jan 2019  路  6Comments  路  Source: go-task/task

  • Task version
    Task version: master (installed by go get)
  • OS
    Ubuntu 18.04 LTS

  • Example Taskfile showing the issue

````

https://taskfile.org

version: '2'

vars:
GREETING: Hello, World!
DIST: dist
IMPORT: code.gitea.io/gitea
GO111MODULE: off
GO: go
EXECUTABLE: '{{if eq OS "windows"}}gitea.exe{{else}}gitea{{end}}'
SED_INPLACE: '{{if eq OS "darwin"}}sed -i ''{{else}}sed -i{{end}}'
BINDATA: modules/{options,public,templates}/bindata.go
GOFILES:
sh: find . -name ".go" -type f ! -path "./vendor/" ! -path "*/bindata.go"
GOFMT: gofmt -s
GOFLAGS: -i -v
EXTRA_GOFLAGS:
VERSION: '{{trimAll "v" "$DRONE_TAG"}}'

tasks:
default:
cmds:
- echo "{{.GREETING}}"
silent: true

show-vars:
cmds:
- echo "OS={{OS}}"
- echo "EXECUTABLE={{.EXECUTABLE}}"
- echo "SED_INPLACE={{.SED_INPLACE}}"
- echo "BINDATA={{.BINDATA}}"
- echo "GOFILES={{.GOFILES}}"
- echo "GOFLAGS={{.GOFLAGS}}"
- echo "EXTRA_GOFLAGS={{.EXTRA_GOFLAGS}}"
- echo "VERSION={{.VERSION}}"
silent: true
````

I am experimenting with the Task for code.gitea.io/gitea.
I find that DRONE_TAG=v1.2.3 task show-vars can capture the variable but
task show-vars DRONE_TAG=v1.2.3 cannot.

Also, the trimAll function does not seem to work.

question

All 6 comments

Hi @typeless,

Good to know you're considering Task for Gitea.

I see two misunderstandings here.

First, the use of variables in template is wrong here:

- VERSION: '{{trimAll "v" "$DRONE_TAG"}}'
+ VERSION: '{{trimAll "v" .DRONE_TAG}}'

trimAll works as expected on my tests.

Second, when you add arguments after a task, you're overriding it for that specific task:

version: '2'

tasks:
  echo:
    cmds:
      - echo "{{.MSG}}"
    silent: true
task echo MSG=foo echo MSG=bar

The above will print foo and then bar. If you give it as an environment variable instead, it'll be available globally, though.

So, what you could do in your example, is overriding the VERSION variable instead of the DRONE_TAG one.

Is it possible to refer to another environment in the dynamic variable declaration? Like https://github.com/typeless/gitea/blob/go-task/Taskfile.yml#L55

I changed the way I wrote it. But it does not seem to work either.

@typeless It's possible, but what you probably want is:

  PACKAGES_WITH_INTEGRATIONS:
    sh: {{.GO | default "go"}} list ./... | grep -v /vendor/

instead of:

  PACKAGES_WITH_INTEGRATIONS:
    sh: $GO list ./... | grep -v /vendor/
$ task show-vars
yaml: line 57: did not find expected key

https://github.com/typeless/gitea/blob/go-task/Taskfile.yml#L57

Please bear with me if this is kind of low-class mistake.

@typeless This is an YAML thing, you have to quote the string since there's special characters in there (can be single quotes).

Or you can make it multiline like you did on other variables.

Hi @typeless,

Seems that you don't have more questions. Can we close this issue?

Was this page helpful?
0 / 5 - 0 ratings