admin 管理员组

文章数量: 1086019

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

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

Share Improve this question asked Mar 27 at 14:05 Bob RamseyBob Ramsey 6271 gold badge8 silver badges16 bronze badges 2
  • @Mullo, that's interesting because the gitlab example specifically uses single quotes 'text "${var}"'. My six, just single quotes, and nine, variable inside double quotes inside single quotes, works as expected. I've tried TEMP: '"${PWD}"', TEMP: '$PWD' and TEMP: "$PWD", and TEMP: "${PWD}" without any luck. The only real luck I've had is this: - echo $PWD/my/dist Which echoes /build/my/repo/my/dist but if I try assigning that to a variable, it doesn't work. – Bob Ramsey Commented Mar 27 at 16:49
  • Yes, but GitLab docs write it about script section. And for rules there is following text Unlike variables in script sections, variables in rules expressions are always formatted as $VARIABLE. So looks like you need to write something like TEMP: $PWD – Mullo Commented Apr 4 at 11:11
Add a comment  | 

1 Answer 1

Reset to default 0

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.

本文标签: using gitlab variables in gitlabciymlStack Overflow