Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat variable names in GitLab

Tags:

yaml

gitlab-ci

We use a Gitlab Project in a team. Each developer has his own Kubernetes cluster in the cloud and an own branch within GitLab. We use GitLab-CI to automatically build new containers and deploy them to our Kubernetes clusters.

At the moment we have a .gitlab-ci.yml looks something like this:

variables:
    USERNAME: USERNAME
    CI_K8S_PROJECT: ${USERNAME_CI_K8S_PROJECT}
    REGISTRY_JSON_KEY_FILE: ${USERNAME_REGISTRY_JSON_KEY_FILE}
    [...]

stages:
  - build
  - deploy
  - remove

build-zeppelin:
  stage: build
  image: docker:latest
  variables:
    image_name: "zeppelin"
  only:
    - ${USERNAME}@Gitlab-Repo
  tags:
    - cloudrunner
  script:
    - docker login -u _json_key -p "${REGISTRY_JSON_KEY_FILE?}" https://eu.gcr.io
    - image_name_fqdn="eu.gcr.io/${CI_K8S_PROJECT?}/${image_name?}:latest"
    - docker build -t ${image_name_fqdn?} .
    - docker push ${image_name_fqdn?} 
    - echo "Your new image is '${image_name_fqdn?}'. Have fun!"

[...]

So in the beginning we reference the important information by using a USERNAME-prefix. This works quite well, but is problematic, since we need to correct them after every pull request from another user.

So we search for a way to keep the gitlab-ci file the same to every developer while still referencing some gitlab-variables different for every developer.


Things we thought about, that don't seem to work:

Use multiple yml files and import them into each other => not supported.

Try to combine Gitlab Environment variables as Prefix:

CI_K8S_PROJECT: ${${GITLAB_USER_ID}_CI_K8S_PROJECT}

or

INDIVIDUAL_CI_K8S_PROJECT: ${GITLAB_USER_ID}_CI_K8S_PROJECT
CI_K8S_PROJECT: ${INDIVIDUAL_CI_K8S_PROJECT}
like image 689
nik Avatar asked Jan 18 '17 15:01

nik


1 Answers

We found a solution using indirect expansion (bash feature):

before_script:
 - variableName=${GITLAB_USER_ID}_CI_K8S_PROJECT
 - export wantedValue=${!variableName}

But we also recognised, that our setup was somehow stupid: It does not make sense to have multiple branches for each user and use prefixed variables, since this leads to problems such as the above and security concerns, since all variables are accessible to all users.

It is way easier if each user forks the root project and simply creates a merge request for new features. This way there is no renaming/prefixing of variables or branches necessary at all.

like image 87
nik Avatar answered Sep 18 '22 07:09

nik