1

In my GitLab CI script, I am creating a new user which then executes a command. I'm noticing that when the command is executed as this user, the CI environment variables aren't available.

Below is what I do:

before_script:
   - useradd -U -d "$CI_PROJECT_DIR" newuser

test-app:
  stage: test
  image: open-jdk
  script:
    - echo $MY_CI_VAR #here I can see the env var set in GitLab CI
    - su newuser -l -c "grails test-app -unit FooHelper" #in the code it is not available

Question

Is there a way to pass environment variables either when creating a new user or when executing a command as that user.

1 Answer 1

2

The problem is that the command you're executing is run as a different user with su which by default doesn't bring your environment. You specify the -l option which will clear the environment variables before switching to the other user. You can drop the -l option and use -m instead to preserve the environment.

Try this: - su newuser -m -c "grails test-app -unit FooHelper"

You may need to set the full path to grails because your PATH is an environment variable that won't get set to newuser's PATH.

You may also be able to pass it into the command if you need to keep the login shell. - su newuser -l -c "MY_CI_VAR=$MY_CI_VAR grails test-app -unit FooHelper"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .