firecow / gitlab-ci-local

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

can't seem to get VARIABLE to work in `~/.gitlab-ci-local/.env` #1165

Closed justinTM closed 6 months ago

justinTM commented 6 months ago

Minimal .gitlab-ci.yml illustrating the issue

my-job:
  script:
    if [ -n "$CI_JOB_TOKEN" ]; then echo "can't find CI_JOB_TOKEN!" && exit 1; fi
# ~/.gitlab-ci-local/.env
VARIABLE="CI_JOB_TOKEN=$GITLAB_ACCESS_TOKEN"
GCL_VARIABLE="CI_JOB_TOKEN=$GITLAB_ACCESS_TOKEN"
CI_JOB_TOKEN=$GITLAB_ACCESS_TOKEN

export VARIABLE="CI_JOB_TOKEN=$GITLAB_ACCESS_TOKEN"
export GCL_VARIABLE="CI_JOB_TOKEN=$GITLAB_ACCESS_TOKEN"
export CI_JOB_TOKEN=$GITLAB_ACCESS_TOKEN
$ echo $GITLAB_ACCESS_TOKEN
glpat-XXXXXXXXX
$ gitlab-ci-local \        
    --variable DOCKER_HOST="" --variable RUNNER_REGISTRATION_TOKEN=$RUNNER_REGISTRATION_TOKEN \
    --variable CI_ENVIRONMENT_STAGE=dev --variable CI_PROJECT_ID=$CI_PROJECT_ID \
    --volume ~/.aws:/root/.aws --volume ~/.terraform.d:/root/.terraform.d \
    --privileged --mount-cache \
    'my-job'
Using fallback branch data
parsing and downloads finished in 23 ms
my-job starting shell (test)
my-job $ if [ -n "$CI_JOB_TOKEN" ]; then echo "can't find CI_JOB_TOKEN!" && exit 1; fi
my-job > can't find CI_JOB_TOKEN!
my-job finished in 13 ms  FAIL 1 

Expected behavior nothing is printed -- job succeeds.

Host information macOS gitlab-ci-local 4.47.0

Containerd binary Are you using docker or podman, or any other relevant containerization tool.

Additional context Add any other context about the problem here.

justinTM commented 6 months ago

ah i see... it is not compatible with setting any variables via CLI. when i remove all --variable flags in the command, it succeeds. so definitely a bug. i guess no one uses this feature lol!

justinTM commented 6 months ago

it also apparently can't handle multiple VARIABLE flags inside dotenv file :/

justinTM commented 6 months ago

also, when you specify multiple variables inside a single VARIABLE, it only applies the second one (skipping the first):

# ~/.gitlab-ci-local/.env
VARIABLE="CI_JOB_TOKEN=$GITLAB_ACCESS_TOKEN;DOCKER_HOST='xxxxx'"
❯ gitlab-ci-local 'my-job'
Using fallback branch data
parsing and downloads finished in 21 ms
my-job starting shell (test)
my-job $ if [ -n "$CI_JOB_TOKEN" ]; then echo "can't find CI_JOB_TOKEN!" && exit 1; fi
my-job > can't find CI_JOB_TOKEN!
my-job finished in 11 ms  FAIL 1 

 FAIL  my-job
  > can't find CI_JOB_TOKEN!

putting a space ` instead of;` makes it work for the first variable but not for the second:

# ~/.gitlab-ci-local/.env
VARIABLE="CI_JOB_TOKEN=$GITLAB_ACCESS_TOKEN DOCKER_HOST='xxxxx'"
❯ gitlab-ci-local 'my-job'
Using fallback branch data
parsing and downloads finished in 22 ms
my-job starting shell (test)
my-job $ if [ -n "$CI_JOB_TOKEN" ]; then echo "can't find CI_JOB_TOKEN!" && exit 1; fi
my-job $ if [ -n "$DOCKER_HOST" ]; then echo "can't find DOCKER_HOST!" && exit 1; fi
my-job > can't find DOCKER_HOST!
my-job finished in 12 ms  FAIL 1 

 FAIL  my-job
  > can't find DOCKER_HOST!
firecow commented 6 months ago

https://github.com/firecow/gitlab-ci-local/issues/992 Yeah, we are tracking the issue here