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 |1 Answer
Reset to default 0You'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
版权声明:本文标题:using gitlab variables in gitlab-ci.yml - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744083127a2530627.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
script
section. And forrules
there is following textUnlike variables in script sections, variables in rules expressions are always formatted as $VARIABLE
. So looks like you need to write something likeTEMP: $PWD
– Mullo Commented Apr 4 at 11:11