How to use a Building Block multiple times

This tutorial will show you how you can use a Building Block multiple times with the example of the Redis-HA Building Block.

Include Job Templates globally

Since Gitlab does not allow to include the same template in multiple Gitlab CI definitions, you have to include it once in your main .gitlab-ci.yml. Eg.:

.gitlab-ci.yml

include:
  - project: syseleven/building-blocks/helmfiles/redis-ha
    file: JobTemplate.yaml
    ref: 1.5.0

  - /other-app/.gitlab-ci.yml
  - /other-building-block/.gitlab-ci.yml

Alternatively you can create a seperate .redis-ha-gitlab-ci.yml and include it in your main .gitlab-ci.yml.

Extending Jobs

Then you have to extend the included jobs from the Building Block with Gitlab CI to overwrite the default BASEDIR and NAMESPACE Variables to be able to deploy it multiple times with different values. We will create two deployments, redis-ha-foo and redis-ha-bar.

First deployment redis-ha-foo:

redis-ha-foo/.gitlab-ci.yml

.syseleven-redis-ha-foo:
  extends: .syseleven-redis-ha
  variables:
    NAMESPACE_REDIS_HA: redis-ha-foo
    BASEDIR: $CI_PROJECT_DIR/redis-ha-foo

syseleven-redis-ha-foo-diff:
  extends:
    - .syseleven-redis-ha-diff
    - .syseleven-redis-ha-foo

syseleven-redis-ha-foo-deploy:
  extends:
    - .syseleven-redis-ha-deploy
    - .syseleven-redis-ha-foo

And the second deployment redis-ha-bar:

redis-ha-bar/.gitlab-ci.yml

.syseleven-redis-ha-bar:
  extends: .syseleven-redis-ha
  variables:
    NAMESPACE_REDIS_HA: redis-ha-bar
    BASEDIR: $CI_PROJECT_DIR/redis-ha-bar

syseleven-redis-ha-bar-diff:
  extends:
    - .syseleven-redis-ha-diff
    - .syseleven-redis-ha-bar

syseleven-redis-ha-bar-deploy:
  extends:
    - .syseleven-redis-ha-deploy
    - .syseleven-redis-ha-bar

Include these two deployments to your main .gitlab-ci.yml:

.gitlab-ci.yml

include:
  - project: syseleven/building-blocks/helmfiles/redis-ha
    file: JobTemplate.yaml
    ref: 1.5.0

  - /other-app/.gitlab-ci.yml
  - /other-building-block/.gitlab-ci.yml
  - /redis-ha-foo/.gitlab-ci.yml
  - /redis-ha-bar/.gitlab-ci.yml

After comitting, you should get two Redis-HA deployments in different namespaces. If you want to deploy the same Building Block multiple times in the same namespace, you have to change default variables.