Home > Software engineering >  GitLab CI/CD shows $HOME as null when concatenated with other variable value
GitLab CI/CD shows $HOME as null when concatenated with other variable value

Time:08-19

I have defined the following stages, environment variable in my .gitlab-ci.yaml script:

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:

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?

CodePudding user response:

When setting a variable using gitlab-ci variables: directive, $HOME isn't available yet because it's not running in a shell.
$HOME is set by your shell when you start the script (or before_script) part.
If you export it during the script step, it should be available, so :

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
  • Related