admin管理员组

文章数量:1345096

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.

本文标签: yamlGitlab CIVariables vardefault is not working as expected (Shell Parameter Expansion)Stack Overflow