是否有可能将环境variables传递给新创build的用户?

在我的GitLab CI脚本中,我创build了一个新的用户,然后执行一个命令。 我注意到当这个用户执行该命令时,CI环境variables不可用。

以下是我所做的:

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 

有没有办法在创build新用户时或者以该用户身份执行命令时传递环境variables。

问题是你正在执行的命令是作为一个不同的用户使用su运行,默认情况下不会带来你的环境。 您可以指定-l选项,在切换到其他用户之前清除环境variables。 您可以删除-l选项并使用-m来保留环境。

试试这个: - su newuser -m -c "grails test-app -unit FooHelper"

您可能需要设置grails的完整path,因为PATH是一个环境variables,不会被设置为newuser的PATH。

如果您需要保留loginshell,您也可以将其传递到命令中。 - su newuser -l -c "MY_CI_VAR=$MY_CI_VAR grails test-app -unit FooHelper"