GitLab CI shell runner不继承系统环境变量

问题描述 投票:1回答:1

有没有办法在GitLab CI / CD管道中运行作业时,使用GitLab Runner(shell runner)启动进程来继承系统环境(/etc/environment)中定义的变量?

.gitlab-ci.yml的相关部分:

synchronize data:
    stage: synchronize
    only:
        refs:
            - schedules
    variables:
        GIT_STRATEGY: none
    script:
        - [[ -s /etc/environment && -r /etc/environment ]] && source /etc/environment
        # - printenv|sort
        - echo $APP_ENV
        - php -r "var_dump(getenv('APP_ENV'));"

这是作业执行的输出:

Running with gitlab-runner 11.4.2 (cf91d5e1)
  on <redacted> 9b533a38
Using Shell executor...
Running on <redacted>...
Skipping Git repository setup
Skipping Git checkout
Skipping Git submodules setup
$ [[ -s /etc/environment && -r /etc/environment ]] && source /etc/environment
$ echo $APP_ENV
development
$ php -r "var_dump(getenv('APP_ENV'));"
bool(false)

echo仅在我明确获取文件时才有效,但PHP仍然没有获取变量。

在机器上,我运行了这个变量,gitlab-runner用户可以访问变量:

[@localhost ~]$ sudo -u gitlab-runner -i bash --norc --noprofile  
bash-4.2$ echo $APP_ENV 
development
bash-4.2$ php -r 'var_dump(getenv("APP_ENV"));'
string(11) "development"

我也尝试过没有bash以及--norc --noprofilesh,它们都是从交互式shell开始的。

php environment-variables sh gitlab-ci gitlab-ci-runner
1个回答
0
投票

问题是源不导出变量。正如在this answer中详述的那样,你可以在set -a之前使用source

© www.soinside.com 2019 - 2024. All rights reserved.