(1/5) If you do work with #YAML, you might think its just a bunch of lists and dicts used to store data (configuration). Recently I've been building fairly complex #Gitlab #pipeline to build and deploy #Python app to multi cluster #OpenShift deployment.

Let me gou throuh a ways how you can reuse the code:

(2/5) The easiest are the obvious keywords like `before_script` and `after_script`.

If you want to run commands before (or after) all jobs, all the time, then the easiest way is to simply extract the common code as a before_script or after_script key at the beginning of the config file.

While this does not give you a lot of flexibility, it's a very quick and straightforward way to reuse commands.

(3/5) As a programmer you tend to think of a functions.

And #YAML has #anchors, that are a lot like functions. You can define a block of configuration somewhere and create a reference to it using &. Then, you can use it with *. It is also possible to overload them... I found #Gitlab documentation really helpful: https://docs.gitlab.com/ee/ci/yaml/#anchors

While anchors can be quick to get started with, they do have their downsides. The main one is they only work within the same file.

CI/CD YAML syntax reference | GitLab

GitLab product documentation.

(4/5) If you want to reuse code across several files, then you can use the `extends` keyword. This works similar to inheritance - you define a "template" job that other jobs can then extend.

Some things to keep in mind regarding extends:

* if the key is a hash it will get merged
* if the key is an array, it will get overridden

(5/5) With `include` you can load external file and if you put this all together with enviroment variables you got a powerfull tool in your hand.

You can create a template that contains hidden jobs (starting with .) that can be extended, it is OK to use #anchors in the sameplate as thay are used within the same file.

This allows you to include the same pipeline for each cluster and define everything just by enviroment variables.

#YAMLisCool