最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

yaml - Gitlab CI - Variables ${var:-default} is not working as expected (Shell Parameter Expansion) - Stack Overflow

matteradmin3PV0评论

Using Shell Parameter Expansion inside the variables block of .gitlab-ci.yaml is not resolving the default value.

Example:

my_job:
  extends: .template
  variables:
    OPTS: >-
      -tag=${CI_COMMIT_TAG:-NO_TAG}
      -url=${CI_PIPELINE_URL}

The script part is looking something like this (and is provided via extends)

.template:
   script: 
     - cmd ${OPTS}

The Problem is that -tag is always empty when CI_COMMIT_TAG is not set, but it should be "NO_TAG" in this example.

A possible Solution would be to set OPTS in script or before_script but that is something I want to avoid here because of the extends and only variables should get set.

I tried finding a Solution online to somehow escape the ${parameter:-word} part in the variable part to let bash "parse" it instead of the pipeline. But with no Success.

Is there a way to make this work, or is my only option to set the "OPTS" variable inside a script?

Using Shell Parameter Expansion inside the variables block of .gitlab-ci.yaml is not resolving the default value.

Example:

my_job:
  extends: .template
  variables:
    OPTS: >-
      -tag=${CI_COMMIT_TAG:-NO_TAG}
      -url=${CI_PIPELINE_URL}

The script part is looking something like this (and is provided via extends)

.template:
   script: 
     - cmd ${OPTS}

The Problem is that -tag is always empty when CI_COMMIT_TAG is not set, but it should be "NO_TAG" in this example.

A possible Solution would be to set OPTS in script or before_script but that is something I want to avoid here because of the extends and only variables should get set.

I tried finding a Solution online to somehow escape the ${parameter:-word} part in the variable part to let bash "parse" it instead of the pipeline. But with no Success.

Is there a way to make this work, or is my only option to set the "OPTS" variable inside a script?

Share asked yesterday MarcelMarcel 1,78017 silver badges35 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Is there a way to make this work

Well, yes - update gitlab source code to support it. Gitlab is not shell. Gitlab supports only ${VAR} or $VAR or %VAR%. See https://docs.gitlab/ci/variables/where_variables_can_be_used/#expansion-mechanisms .

or is my only option to set the "OPTS" variable inside a script?

Yes, do that.

You could also set default

variables:
  CI_COMMIT_TAG: NO_TAG
my_job:
  extends: .template
  variables:
    OPTS: -tag=${CI_COMMIT_TAG} -url=${CI_PIPELINE_URL}

But this will conflict with rules.

Post a comment

comment list (0)

  1. No comments so far