0

I am trying to create a variable in gitlab-ci.yaml based on the name of the branch.

Suppose I am pushing to a branch named 3.2.7

Here is the situation:

include:
  - template: "Workflows/Branch-Pipelines.gitlab-ci.yml"

variables:
  PRODUCTION_BRANCH: "master"
  STAGING_BRANCH: (\d)\.(\d)\.(\d)

.deploy_rules:
  rules:
    - if: '$CI_COMMIT_BRANCH =~ /$STAGING_BRANCH/'
      variables:
        SERVER_PORT: 3007  # TODO: should be 300d ; d is the second digit

I want to generate 3002 inline using regex matching.

How can I do this?

I have done some research and seems I have to use sed but I am not sure if it is the best way to do it and how to do it.

TO MAKE THE PROBLEM SIMPLER

include:
  - template: "Workflows/Branch-Pipelines.gitlab-ci.yml"

variables:
  TEST_VAR: sed -E 's/(\d)\.(\d)\.(\d)/300\2/gm;t;d' <<< $CI_COMMIT_BRANCH

stages:
  - temp

temp:
  stage: temp
  script:
    - echo $TEST_VAR

Should be echoing 3002 but it is echoing sed -E 's/(\d)\.(\d)\.(\d)/300\2/gm;t;d' <<< 3.2.7

1
  • Any reason you can't just write the regex pattern directly into the rule? Commented Dec 14, 2021 at 17:29

1 Answer 1

2

You can't use variables in the regex pattern. You just have to write the regex verbatim, it cannot be directly parameterized. You also cannot use sed or other Linux utilities in variables: or other parts of your yaml. You're bound to the limitations of YAML specification and features provided by GitLab.

However, there is an option available to you that will fit your stated use case.

Dynamic variables

TEST_VAR: sed -E 's/(\d).(\d).(\d)/300\2/gm;t;d' <<< $CI_COMMIT_BRANCH

While you can't use sed or other utilities directly in variables: declarations, you can use dotenv artifacts via artifacts:reports:dotenv to set variables dynamically.

For example, a job can use sed or whatever other utilities you like to create variables which will be used by the rest of the pipeline.

stages:
  - temp

create_variables:
  stage: .pre
  script:
    - TEST_VAR="$(sed -E 's/(\d)\.(\d)\.(\d)/300\2/gm;t;d' <<< ${CI_COMMIT_BRANCH})"
    - echo "TEST_VAR=${TEST_VAR}" >> dotenv.txt
  artifacts:
    reports:
      dotenv: dotenv.txt

temp:
  stage: temp
  script:
    - echo $TEST_VAR

Here, the .pre stage is used, which is a special stage that is always ordered before every other stage. The dotenv artifact from the create_variables job will dynamically create variables for the jobs in subsequent stages that receive the artifact.

Sign up to request clarification or add additional context in comments.

4 Comments

And I can set the variable during the stage instead of creating a pre stage
Hi @AminBa In general, it's best to ask just 1 question at a time. I would suggest you open a new question on how to use perl to achieve your goal. And unfortunately, I'm not familiar enough with Perl to provide a solution for this case. However, if this answer answers your initial question about setting the GitLab CI variables, please consider voting and accepting.
I think it is better if I do it like this: ` str="3.1.2" if [[ $str =~ 'master' ]]; then echo "3000" elif [[ 'str =~ /(\d)\.(\d)\.(\d)/' ]]; then echo "match: '${BASH_REMATCH[1]}'" else echo "no match found!" fi ` Howerver BASH_REMATCH[1] does not work. Any idea?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.