Blog Product How to include file references in your CI/CD components
Published on: October 16, 2024
4 min read

How to include file references in your CI/CD components

Learn how to include scripts and dependencies in your CI/CD components to minimize duplications and simplify maintenance. This tutorial takes you step-by-step through the process.

ci/cd component - cover

I’m frequently asked whether included CI/CD components can reference additional files stored outside of the pipeline repository. While including components in your configuration is straightforward since they’re just YAML, many users want to know if those included components can access and execute additional files referenced by the components, like shell scripts or other dependencies.

This challenge has been a common topic of discussion in threads across the GitLab Forum and Reddit.

Now for the good news: CI/CD components not only allow you to reuse pipeline configurations, saving time and effort, but you can also go a step further. With the new CI/CD Steps programming language, you can directly reuse centralized automation scripts and dependencies in your pipelines. You'll gain even greater flexibility, making your pipelines more powerful and adaptable than ever.

By storing your scripts in a central location and wrapping them in CI/CD Steps, you can easily call these steps from your CI/CD components. This eliminates the need to duplicate scripts across multiple repositories and CI/CD configurations, streamlining your workflow and reducing redundancy.

Before we dive into the step-by-step guide, let’s briefly explore what CI/CD components and CI/CD Steps are.

What are CI/CD components?

CI/CD components are reusable units of pipeline configurations that get included in a pipeline when it’s created. The components bring additional jobs into the pipeline, however they can’t bring additional files as such reusable scripts.

What are CI/CD Steps?

CI/CD Steps are reusable units of a job. Each step defines structured inputs and outputs that can be consumed by other steps. Steps can come from local files, GitLab.com repositories, or any other Git source. Steps offer a structured alternative to shell scripts for running jobs. They are modular, can be composed, tested, and easily reused, providing greater flexibility and maintainability.

What are the differences between CI/CD Steps and CI/CD components?

  • Component and step definitions look very similar but they take effect at different phases in pipeline execution.

  • Components are used when a pipeline is created while steps are used when individual jobs are running.

  • When a step is running, the whole repository is being downloaded into the job environment along with extra files.

A step-by-step guide

Here is how CI/CD Steps and Components work together to access additional files.

CI/CD Steps flow diagram

This diagram illustrates the process flow: Jobs defined within components are imported into the pipeline configuration (.gitlab-ci.yml) when the pipeline is created. During the pipeline's execution, a job’s steps are executed, and the entire Git repository is downloaded to the Step runner within the job’s context. This ensures that references to dependencies function correctly.

1. Define a component with run keyword that runs CI/CD Steps

Run is a new keyword that supports running steps, see the example code below. You can use this guide to learn more on how to create Components.

template-yml

2. Create a step.yml file in the project where your scripts and dependencies are located.

In this code example, format.sh exists in the same directory as the step.yml.

step.yml

While the job is running, the Step runner will download the entire Git repository where the step is defined. The ${{ step_dir }} step expression references the directory of the locally cached step files, allowing you to access other files from the repository. In the example above, the “format” step invokes the format.sh script.

3. Make sure that any files accessed by the step are located in the same repository as the step.yml file.

4. Include the component in your CI/CD configuration.

See this example code:

.gitlab-ci.yml

Code example: You can find the entire code demonstrated in this blog in this GitLab Group.

Important note: The CI/CD Steps feature is currently Experimental, and the syntax may change as we continue to iterate and refine it based on user feedback. Any feedback should be provided via this issue.

Learn more

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

Find out which plan works best for your team

Learn about pricing

Learn about what GitLab can do for your team

Talk to an expert