gitlabgitlab-ciexpansion

using gitlab variables in gitlab-ci.yml


I'm trying to use a couple of built-in variables in my gitlab-ci.yml file, but nothing I've done seems to work. And it seems like it should. I'm trying to use $PWD and $CI_COMMIT_SHORT_SHA at them moment, but might like to use others in the future. This is a long listing because I've tried a number of different things:

default:
  image: csi:0.1

variables:
  ONE:
    value: $PWD
    expand: true
  TWO:
    value: "hello world"
    expand: true
  THREE:
    value: $TWO
    expand: true
  FLAGS:
    value: '-al'
    expand: true
  LS_CMD:
    value: 'ls "$FLAGS" $$TMP_DIR'
    expand: true
.branch-detection:
  rules:
    - if: ($CI_COMMIT_BRANCH != "main" && $CI_COMMIT_BRANCH != "development")
      when: never
stages:
  - deploy
build-test-deploy:
  variables:
    FOUR:
      value: $PWD
      expand: true
    FIVE:
      value: "hi"
      expand: true
    SIX:
      value: '$FIVE, there'
      expand: true
  stage: deploy
  extends:
    - .branch-detection
  script:
    - echo $PWD
    - echo '$PWD/my/dist/folder'
    - echo $ONE
    - echo $TWO
    - echo $THREE
    - echo $FOUR
    - echo $FIVE
    - echo $SIX
    - echo $SEVEN
    - echo $EIGHT
    - echo $NINE
    - echo $FLAGS
    - echo $LS_CMD
  rules:
    - if: $CI_COMMIT_BRANCH == "development"
      variables:
        FIVE: "My value changed in the rules, did SIX?"
        SEVEN: $TWO
        EIGHT: $FOUR
        NINE: 'this is variable six, enclosed in double quotes "$SIX"'

What I get as output vs [what I would expect if different]:

$ echo $PWD
/builds/path/to/my/repo
$ echo '$PWD/my/dist/folder'
$PWD/my/dist/folder  [/builds/path/to/my/repo/my/dist/folder]
$ echo $ONE
                     [/builds/path/to/my/repo]
$ echo $TWO
hello world
$ echo $THREE
hello world
$ echo $FOUR
                     [/builds/path/to/my/repo]
$ echo $FIVE
My value changed in the rules, did SIX?
$ echo $SIX
My value changed in the rules, did SIX?, there
$ echo $SEVEN
hello world
$ echo $EIGHT
                     [/builds/path/to/my/repo]
$ echo $NINE
this is variable six, enclosed in double quotes "My value changed in the rules, did SIX?, there"
$ echo $FLAGS
-al
$ echo $LS_CMD
ls "-al" $TMP_DIR

It appears I can't assign $PWD to a variable. Even worse, when I tried - echo '$PWD/my/dist/folder' it didn't even echo an empty line, it just skipped it.

Obviously I'm missing something basic, but I just can't see it. Any help would be appreciated.

tia


Solution

  • You're using single quotes, and as explained in this Stack Overflow post, everything inside single quotes is treated literally without exception.

    You can observe the same behavior in a simple shell script:

    #!/bin/sh
    MY_DIR='some path'
    
    echo "Case 1"
    echo '$MY_DIR/my/path'
    
    echo "Case 2"
    echo $MY_DIR/my/path
    
    echo "Case 3"
    echo "$MY_DIR/my/path"
    

    Execution results:

    > sh test.sh
    Case 1
    $MY_DIR/my/path
    Case 2
    some path/my/path
    Case 3
    some path/my/path
    

    To fix the issue, simply remove the single quotes from assignments or echo commands, and everything should work correctly.