簡體   English   中英

GitLab CI/CD 在與其他變量值連接時將 $HOME 顯示為 null

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

我在.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

當我運行上述內容時,即使/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
       

我在這里做錯了什么嗎? 為什么$HOME與其他變量一起使用時為空/NULL?

使用 gitlab-ci variables:指令設置變量時,$HOME 尚不可用,因為它未在 shell 中運行。
$HOME 在您啟動script (或before_script )部分時由您的 shell 設置。
如果您在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