简体   繁体   English

GitLab CI/CD 在与其他变量值连接时将 $HOME 显示为 null

[英]GitLab CI/CD shows $HOME as null when concatenated with other variable value

I have defined the following stages, environment variable in my .gitlab-ci.yaml script:我在.gitlab-ci.yaml脚本中定义了以下阶段和环境变量:

stages:
  - prepare
  - run-test

variables:
  MY_TEST_DIR: "$HOME/mytests"

prepare-scripts:
  stage: prepare
  before_script:
    - cd $HOME
    - pwd
  script:
    - echo "Your test directory is $MY_TEST_DIR"
    - cd $MY_TEST_DIR
    - pwd
  when: always
  tags:
    - ubuntutest

When I run the above, I get the following error even though /home/gitlab-runner/mytests exists:当我运行上述内容时,即使/home/gitlab-runner/mytests存在,我也会收到以下错误:

Running with gitlab-runner 15.2.1 (32fc1585)
  on Ubuntu20 sY8v5evy
Resolving secrets
Preparing the "shell" executor
Using Shell executor...
Preparing environment
Running on PCUbuntu...
Getting source from Git repository
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /home/gitlab-runner/tests/sY8v5evy/0/childless/tests/.git/
Checking out cbc73566 as test.1...
Skipping Git submodules setup
Executing "step_script" stage of the job script
$ cd $HOME
/home/gitlab-runner
$ echo "Your test directory is $MY_TEST_DIR"
SDK directory is /mytests
$ cd $MY_TEST_DIR
Cleaning up project directory and file based variables
ERROR: Job failed: exit status 1
       

Is there something that I'm doing wrong here?我在这里做错了什么吗? Why is $HOME empty/NULL when used along with other variable?为什么$HOME与其他变量一起使用时为空/NULL?

When setting a variable using gitlab-ci variables: directive, $HOME isn't available yet because it's not running in a shell.使用 gitlab-ci variables:指令设置变量时,$HOME 尚不可用,因为它未在 shell 中运行。
$HOME is set by your shell when you start the script (or before_script ) part. $HOME 在您启动script (或before_script )部分时由您的 shell 设置。
If you export it during the script step, it should be available, so:如果您在script步骤中导出它,它应该是可用的,所以:

prepare-scripts:
  stage: prepare
  before_script:
    - cd $HOME
    - pwd
  script:
    - export MY_TEST_DIR="$HOME/mytests"
    - echo "Your test directory is $MY_TEST_DIR"
    - cd $MY_TEST_DIR
    - pwd
  when: always
  tags:
    - ubuntutest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM