【问题标题】:GitLab CI/CD shows $HOME as null when concatenated with other variable value当与其他变量值连接时,GitLab CI/CD 将 $HOME 显示为 null
【发布时间】:2022-08-17 21:51:12
【问题描述】:

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

    标签: bash gitlab yaml environment-variables cicd


    【解决方案1】:

    使用 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
    

    【讨论】:

      猜你喜欢
      • 2022-12-31
      • 2019-08-07
      • 2019-02-02
      • 2022-08-11
      • 2021-09-15
      • 1970-01-01
      • 2022-01-18
      • 2021-08-21
      • 2022-08-18
      相关资源
      最近更新 更多