firecow / gitlab-ci-local

Tired of pushing to test your .gitlab-ci.yml?
MIT License
2.03k stars 115 forks source link

Boolean CLI args cannot be set to `false` using env files #1259

Open PigeonF opened 3 weeks ago

PigeonF commented 3 weeks ago

Minimal .gitlab-ci.yml illustrating the issue

---
foo:
  image: docker.io/library/alpine
  script:
    - id
$ echo "TIMESTAMPS=false" > .gitlab-ci-local-env
$ gitlab-ci-local
[13:00:56 1.93 ms] foo starting docker.io/library/alpine:latest (test)
[13:00:57  963 ms] foo copied to docker volumes in 919 ms
[13:00:58  1.52 s] foo $ id
[13:00:58  1.52 s] foo > uid=0(root) gid=0(root) groups=0(root)
[13:00:58  1.74 s] foo finished in 1.74 s

(Note that I use TIMESTAMPS here because it is much easier to show than UMASK, which is what I actually wanted to set 😅 )

I'm pretty sure the issue is that injectDotenv sets string values only

https://github.com/firecow/gitlab-ci-local/blob/428ca66c8300f3bc64c89cf864e5052e44c58c37/src/argv.ts#L64-L69

which means that when boolean properties are read, the returned value is not a boolean, but a string (i.e. not false, but "false"), which evaluates to true, as it is a non-empty string.

https://github.com/firecow/gitlab-ci-local/blob/428ca66c8300f3bc64c89cf864e5052e44c58c37/src/argv.ts#L229-L231

The easiest solution is probably to check if the map value is a bool or a string, and if it is a string compare against "true" (yargs environment overrides seem to only work with true, not variations like TRUE or 1).

firecow commented 3 weeks ago

These works.

mjn@mjn-laptop:~/workspace/gitlab-com-ci-debugging$ GCL_TIMESTAMPS=false gitlab-ci-local
parsing and downloads finished in 57 ms
json schema validated in 177 ms
test starting shell (test)
test $ echo hello
test > hello
test finished in 48 ms

 PASS  test
pipeline finished in 361 ms
mjn@mjn-laptop:~/workspace/gitlab-com-ci-debugging$ GCL_TIMESTAMPS=true gitlab-ci-local
parsing and downloads finished in 55 ms
json schema validated in 183 ms
[07:03:44    2 ms] test starting shell (test)
[07:03:44   13 ms] test $ echo hello
[07:03:44   15 ms] test > hello
[07:03:44   16 ms] test finished in 16 ms

 PASS  [  16 ms] test
pipeline finished in 325 ms

But .gitlab-ci-local-env parsing is definently broken

mjn@mjn-laptop:~/workspace/gitlab-com-ci-debugging$ echo "TIMESTAMPS=false" > .gitlab-ci-local-env && gitlab-ci-local 
parsing and downloads finished in 57 ms
json schema validated in 175 ms
[07:04:17 1.76 ms] test starting shell (test)
[07:04:17   12 ms] test $ echo hello
[07:04:17   12 ms] test > hello
[07:04:17   14 ms] test finished in 14 ms

 PASS  [  14 ms] test
pipeline finished in 313 ms