GitLab recently introduced CI/CD components as the next generation of the traditional CI/CD templates, and a novel approach to constructing CI/CD pipelines. CI/CD components offer reusable pipeline configurations that can be customized using input parameters.
Although GitLab continues to support templates, they come with certain drawbacks that are addressed by the introduction of components. Therefore, we highly recommend refactoring existing templates into CI/CD components.
This article will guide you through the steps of converting your current GitLab CI/CD templates into reusable CI/CD components. Prior familiarity with how to create CI/CD components is a prerequisite, which you can learn about in this blog post: Introducing the GitLab CI/CD Catalog Beta.
How to convert a template to a component
These are the steps to convert a CI/CD template to a CI/CD component:
- Create a component project if you don’t have one.
- Copy your existing templates to the ‘templates’ directory in the component project.
- For each template, review the jobs listed in it and assess whether you prefer to distribute them across different components or retain some or all within the same component. While it's possible to include multiple jobs in a single component, it's advisable to create components that perform minimal tasks. This approach enhances ease of reuse and flexibility.
- Create a new section at the top of the configuration for the input parameters and meta data using the
spec
keyword. - Replace any custom CI/CD variables and any other hard-coded values with inputs to maximize flexibility for consumption. Consider parameterizing elements such as stage, image, job name/job prefix, etc.
- Follow the best practices for components.
- Improve the configuration, for example by enabling merge request pipelines or making it more efficient.
Here is a code example of a job in an existing template:
And this is the refactored component code:
Once your components are ready, you can publish them to the CI/CD catalog so others will be able to find and consume them.
Take a product tour
We've prepared a brief product tour so you can quickly dive into the CI/CD catalog and see it in action (use the "Next" button to progress through the demo).
Additional resources
Please refer to the official documentation on CI/CD components for more on how to convert templates to components.
You can explore an additional practical example, demonstrating the steps to convert GitLab Go templates to CI/CD components.
Then, you can watch the following video where Fabio Pitino, GitLab Principal Engineer, demonstrates the process of refactoring GitLab AWS templates to CI/CD components.