5

I've got a CI script setup something like this with 3 files :

# file Vars
.def-vars:
    STAGING_SSH_DEST: mysite.com
    PROJECT_ROOT: myRoot
# file gitlab-ci
variables:
    extends: .def-vars
    STAGING_SSH_DEST: myrealsite.com
    PROJECT_ROOT: /myRealRoot

deploy-stage:
    extends: .deploy
    variables:
        SSH_DESTINATION: $STAGING_SSH_DEST
# file deploy
.deploy:
    variables:
        SSH_DESTINATION: mysite.com
        RSYNC_DESTINATION: $SSH_DESTINATION:$PROJECT_ROOT

I have my files and variables split up like this to increase the re-usability of the scripts.

The idea was that since I have multi site destinations, staging prod, I want to be able to pass the ssh destination to each and have the job figure out the rsync on its own. Problem is, the variable expansion is not working the way I'd think it would.

In the deploy script I added a print and got the following :

$ echo $SSH_DESTINATION            # This is the variable name local to job
   myrealsite.com                  # Yep! printed the passed in value
$ echo $RSYNC_DESTINATION          # $SSH_DESTINATION:$PROJECT_ROOT
   $STAGING_SSH_DEST:/myRealRoot   # That is the name of the variable passed in

The root and SSH_DESTINATION print just fine. When appending the two, the former seems to be expanded one too few times.

I've had the idea to just create the rsync variable within the script section but I'd like to avoid this as I want to be able to override the rsync variable without editing the .deploy job.

How can this be accomplished?

2 Answers 2

2

There is an issue with Gitlab CI variables, preventing you to expand variables correctly with your extends setup. Your options to solve this are:

  • Use this solution with the help of before_script, posted inside aforementioned issue. Some limitations are there, but for simple stuff it is working pretty good.
 before_script:
   - export VAR1="${CI_PIPELINE_ID}"
   - export VAR2="test-${VAR1}"
  • Do some kind of preparation via .env artifacts and downstream pipelines. This one is tougher to setup, but it will allow to create dynamic tasks (and pipelines), for example spawning multi-stage deploy after successful build.
Sign up to request clarification or add additional context in comments.

Comments

1

"Setting a variable within another variable" should be easier with GitLab 15.6 (November 2022)

Support for special characters in CI/CD variables

Previously, it was difficult to use the $ character in a CI/CD variable because $ normally signifies the start of another variable.
GitLab would interpret it as a variable and try to expand it.

In this release, we are introducing the variable: expand: keyword which will allow you to mark a variable as “raw”.
A raw variable can contain any special characters and is not expanded when passed to the GitLab runner.

https://about.gitlab.com/images/15_6/special_character_support.png -- Support for special characters in CI/CD variables

See Documentation and Issue.

And:

Example of variables:expand:

variables:
  VAR1: value1
  VAR2: value2 $VAR1
  VAR3:
    value: value3 $VAR1
    expand: false

    The result of VAR2 is value2 value1.
    The result of VAR3 is value3 $VAR1.

Comments

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.