red house 87
red house 87

Reputation: 2415

Gitlab CI - Compute variable name based off another variable

I am trying to use a variable that is compiled partly from another variable (based off the branch name). I have environmental variables configured in Gitlab like so:

Key: s3_bucket_development
Value: https://dev.my-bucket.com

Key: s3_bucket_production
Value: https://prod.my-bucket.com

The branch that I am deploying on is called development and the idea is to be able to call the s3_bucket_development variable dynamically depending on the branch it is currently on.

So far I have managed to get this far:

eval echo $s3_bucket_${CI_COMMIT_REF_NAME}

Unfortunately the above only outputs development to the console, not the value of $s3_bucket_development which should be https://dev.my-bucket.com

Not sure what i've done wrong here?

Copied a bit from this link here but does not seem to work

Upvotes: 0

Views: 1125

Answers (1)

Kaffe Myers
Kaffe Myers

Reputation: 464

$ s3_bucket_development=test
$ CI_COMMIT_REF_NAME=development
$ eval echo \$s3_bucket_${CI_COMMIT_REF_NAME}
test

You just need to escape the first $ so it is not parsed as a variable too soon.


Also, as this is specific to gitlab ci, I'm thinking you might be solving the issue in the wrong way. In gitlab, you can set gitlab variables and define what to show where.

  variables:
    s3_bucket: $s3_bucket
  environment:
    name: $CI_COMMIT_REF_NAME

Upvotes: 2

Related Questions