Blog Engineering Refactoring a CI/CD template to a CI/CD component
March 4, 2024
3 min read

Refactoring a CI/CD template to a CI/CD component

CI/CD components are the next generation of CI/CD templates, enhancing pipeline creation and maintenance. Learn how to transition from templates to components.

cluster - cover

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:

  1. Create a component project if you don’t have one.
  2. Copy your existing templates to the ‘templates’ directory in the component project.
  3. 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.
  4. Create a new section at the top of the configuration for the input parameters and meta data using the spec keyword.
  5. 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.
  6. Follow the best practices for components.
  7. 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:

existing template

And this is the refactored component code:

Converted component

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).

Product tour of CI/CD catalog

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.

We want to hear from you

Enjoyed reading this blog post or have questions or feedback? Share your thoughts by creating a new topic in the GitLab community forum. Share your feedback

Ready to get started?

See what your team could do with a unified DevSecOps Platform.

Get free trial

New to GitLab and not sure where to start?

Get started guide

Learn about what GitLab can do for your team

Talk to an expert